/*
 * Aplos window Build a popup window that can contain arbitrary and refreshable content
 */
var AplosWin = {
   
   visible: false,
   
   boxHtml : '<div id="apwin" class="apwin" style="display: none"> \
      <div class="apbar"> \
         <div class="aptitle"></div> \
         <div class="apclose" onclick="AplosWin.destroy()" onmouseover="AplosWin.hOn(this)" onmouseout="AplosWin.hOff(this)"></div> \
      </div> \
      <div class="apcontent"> \
      </div> \
   </div>',

   open : function(body, title, options) {
      var bx = $('apwin');
      if (!bx) {
         // create window
         $(document.body).insert(AplosWin.boxHtml);
         new Draggable('apwin', {
            handle : 'apbar',
            snap : function(x, y) {
               return [ (x >= 0 ? x : 0), (y >= 0 ? y : 0) ];
            }
         });
         bx = $('apwin');
         bx.down('.aptitle').update(title);
         this.size(bx);
         this.transplant(bx, body);
         this.size(bx, true);
      } else {
         this.update(body, title);
      }
      
      // (re)set any options
      this.onClose = null;
      if(options) {
         if(options.onClose) {
            this.onClose = options.onClose;
         }
      }
      
      this.visible = true;
   },
   
   update : function(body, title) {
      var bx = $('apwin');
      if (title) {
         bx.down('.aptitle').update(title);
      }
      if (body) {
         this.transplant(bx, body);
         this.size(bx, true);
      }
   },
   
   transplant : function(bx, body) {
      // put content into temp area to determine appropriate size
      var tmp = $('tmpcontent');
      if (tmp) {
         tmp.update();
      } else {
         $(document).body.insert('<div id="tmpcontent" class="apwin"></div>');
         tmp = $('tmpcontent');
      }
      tmp.insert('<div class="apcontent"><div class="cbody">' + body + '</div></div>');
   
      // determine max width/height and set accordingly
      var cbody = tmp.down('.cbody');
      var mw = cbody.getWidth();
      var mh = cbody.getHeight();
      var w = mw + 30;
      var h = mh + 50;
      bx.contentWidth = w; // mw;
      bx.contentHeight = h; // mh;
      bx.width = w;
      bx.height = h;
   
      // transplant temp content
      var content = bx.down('.apcontent');
      content.hide();
      content.update();
      content.insert(cbody.remove());
   },
   
   resize : function() {
      var bx = $('apwin');
      // put content into temp area to determine appropriate size
      var tmp = $('tmpcontent');
      if (tmp) {
         tmp.update();
      } else {
         $(document).body.insert('<div id="tmpcontent" class="apwin"></div>');
         tmp = $('tmpcontent');
      }
      tmp.insert('<div class="apcontent"><div class="cbody">' + bx.down('.apcontent').down('.cbody').innerHTML + '</div></div>');
   
      // determine max width/height and set accordingly
      var cbody = tmp.down('.cbody');
      var mw = cbody.getWidth();
      var mh = cbody.getHeight();
      var w = mw + 30;
      var h = mh + 50;
      bx.contentWidth = w; // mw;
      bx.contentHeight = h; // mh;
      bx.width = w;
      bx.height = h;
      
      this.size(bx, true);
   },
   
   size : function(bx, morph) {
      var content = bx.down('.apcontent');
   
      var w = (bx.width || parseInt(bx.getStyle('width'))) + 20;
      var h = bx.height || parseInt(bx.getStyle('height'));
   
      // ensure w/h aren't bigger than viewport+reasonable gap
      var gap = 60;
      var maxWidth = document.viewport.getWidth() - gap;
      var maxHeight = document.viewport.getHeight() - gap;
      var contentW = bx.contentWidth || 270;
      var contentH = bx.contentHeight || 170;
      var shrinkW = w > maxWidth;
      var shrinkH = h > maxHeight;
      if (shrinkW || shrinkH) {
         if (shrinkW) {
            contentW = contentW - (w - maxWidth);
            contentH = contentH + 20;
            w = maxWidth;
         }
         if (shrinkH) {
            contentH = contentH - (h - maxHeight);
            contentW = contentW + 20;
            h = maxHeight;
         }
         content.setStyle( {
            width : contentW + 'px',
            overflowX : 'auto',
            height : contentH + 'px',
            overflowY : 'auto'
         });
      } else {
         content.setStyle( {
            width : contentW + 'px',
            overflowX : '',
            height : contentH + 'px',
            overflowY : ''
         });
      }
   
      var l = document.viewport.getScrollOffsets().left + (document.viewport.getWidth() / 2 - (w / 2));
      var t = document.viewport.getScrollOffsets().top + (document.viewport.getHeight() / 2 - (h / 2));
      if (!l || l <= 0)
         l = 0;
      if (!t || t <= 0)
         t = 0;
   
      if (morph) {
         new Effect.Morph('apwin', {
            style : {
               left : l + 'px',
               top : t + 'px',
               width : w + 'px',
               height : h + 'px'
            },
            duration : 0.3,
            afterFinish : function() {
               content.show();
            }
         });
      } else {
         bx.setStyle( {
            left : l + 'px',
            top : t + 'px',
            width : w + 'px',
            height : h + 'px'
         });
         bx.show();
      }
   },
   
   destroy : function() {
      $('apwin').parentNode.removeChild($('apwin'));
      if(this.onClose) {
         this.onClose();
      }
      this.visible = false;
   },
   
   hOn : function(el) {
      $(el).setStyle( {
         backgroundImage : 'url(/images/closehov.gif)'
      });
   },
   
   hOff : function(el) {
      $(el).setStyle( {
         backgroundImage : 'url(/images/close.gif)'
      });
   },
   
   isVisible: function() {
      return this.visible;
   }
}

/*
 * Aplos menu Copyright 2006-2007 javascript-array.com
 */
var AplosMenu = {
   timeout: 500,
   
   closetimer: 0,
   
   ddmenuitem: 0,
   
   // open hidden layer
   open: function(id) {
      // cancel close timer
   this.cancelclosetime();
   
      // close old layer
      if(this.ddmenuitem) this.ddmenuitem.style.visibility = 'hidden';
   
      // get new layer and show it
      this.ddmenuitem = document.getElementById(id);
      this.ddmenuitem.style.visibility = 'visible';
   },
   
   // close showed layer
   close: function() {
      if(this.ddmenuitem) this.ddmenuitem.style.visibility = 'hidden';
   },
   
   // go close timer
   closetime: function() {
      this.closetimer = window.setTimeout('AplosMenu.close()', AplosMenu.timeout);
   },
   
   // cancel close timer
   cancelclosetime: function() {
      if(this.closetimer)
      {
         window.clearTimeout(this.closetimer);
         this.closetimer = null;
      }
   }
}

// close layer when click-out
document.onclick = AplosMenu.close;