/* displays a floating window on top of the page, hide the page with a gray overlay,
window may move in depending on browser.

functions marked with an (L) are taken from leightbox
(http://www.eight.nl/files/leightbox/):
   created by: Chris Campbell
   website: http://particletree.com
   Date: 2/1/2006
   adapted by: Simon de Haan
   website: http://blog.eight.nl
   date: 21/2/2006

arguments:
floatWin: div which will be displayed
closeButton: button for clsing floating window
*/
function FloatingWindow (floatWin, closeButton) {

   var self = this;
   var floatWin = $(floatWin);
   var closeButton = $(closeButton);
   var closeButtonOver = $(closeButton.getAttribute('id') + '_2');
   var floatWinHeight = parseInt(Element.getHeight(floatWin));

   var floatWinMarginTop = parseInt(Element.getStyle(floatWin, 'margin-top'));
   var animateRange = floatWinMarginTop + floatWinHeight;

   /* some browsers can't display the animation */
   var animate = true;
   if ( sniffer.msie || sniffer.konqueror || sniffer.safari || sniffer.icab ) animate = false;

   if (!$('overlay'))
      addLightboxMarkup();

   /* for ie fix from leightbox */
   yPos = 0;

   this.open = function () {

      /* ie fixes, all of them are taken from leightbox */
      if (sniffer.msie) {
         getScroll();
         prepareIE('100%', 'hidden');
         setScroll(0,0);
         hideSelects('hidden');
      }

      /* clicking on the overlay will close window as well, show overlay */
      $('overlay').onclick = self.close;
      $('overlay').style.display = 'block';
      /* show window with animation or without */
      if (animate) {
         $(floatWin).style.top = -(animateRange) + 'px';
      } else {
         $(floatWin).style.top = '0px';
      }
      floatWin.style.visibility = 'visible';
      if (animate)
         new Effect.MoveBy(floatWin, animateRange, 0, {duration: 1})
   }

   this.close = function () {
      if (animate) {
         /* move out window, afterwards make invisible window and overlay */
         new Effect.MoveBy(floatWin, -animateRange, 0, {duration: 1,
            afterFinish: function(effect) {
               floatWin.style.visibility = 'hidden';
               $('overlay').style.display = 'none';
               /* make sure close button registers mouseout, khtml fix */
               mouseOver.out();
            }
         })
      } else {
         /* make invisible window and overlay */
         floatWin.style.visibility = 'hidden';
         $('overlay').style.display = 'none';
         $('overlay').onclick = function (event) { };
         /* make sure close button registers mouseout, khtml fix */
         mouseOver.out();
         /* ie fixes, all of them are taken from leightbox */
         if (sniffer.msie) {
            setScroll(0,this.yPos);
            prepareIE("auto", "auto");
            hideSelects("visible");
         }
      }
   }

   /* init close button */
   var mouseOver = new mouseOverImage(closeButton, closeButtonOver, null);
   closeButton.onclick = self.close;
   closeButtonOver.onclick = self.close;


   /* (L) */
   function hideSelects(visibility) {
      selects = document.getElementsByTagName('select');
      for(i = 0; i < selects.length; i++) {
         selects[i].style.visibility = visibility;
      }
   }
   /* (L) */
   function setScroll(x, y) {
      window.scrollTo(x, y);
   }
   /* (L) */
   function prepareIE(height, overflow) {
      bod = document.getElementsByTagName('body')[0];
      bod.style.height = height;
      bod.style.overflow = overflow;

      htm = document.getElementsByTagName('html')[0];
      htm.style.height = height;
      htm.style.overflow = overflow; 
   }
   /* (L) */
   // Taken from lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
   function getScroll() {
      if (self.pageYOffset) {
         this.yPos = self.pageYOffset;
      } else if (document.documentElement && document.documentElement.scrollTop){
         this.yPos = document.documentElement.scrollTop; 
      } else if (document.body) {
         this.yPos = document.body.scrollTop;
      }
   }

}


/* (L) */
// Add in markup necessary to make this work. Basically two divs:
// Overlay holds the shadow
// Lightbox is the centered square that the content is put into.
function addLightboxMarkup() {
   var bod = document.getElementsByTagName('body')[0];
   var overlay = document.createElement('div');
   overlay.id = 'overlay';
   bod.insertBefore(overlay, bod.firstChild);
}

