/**
 * Popup manager
 * @author maikel
 */
function popupMgr(settings)
{
   // sizes
   this.popupWidth   = settings.popup.width || 400;
   this.popupHeight  = settings.popup.height || 400;
   
   // styling
   this.overlayColor    = settings.overlay.color || '#cccccc';
   this.overlayOpacity  = settings.overlay.opacity || 50;
   
   // divs
   this.divOverlay   = null;
   this.divPopup     = null;
   
   // popup callback
   this.popupCallback = settings.popup.callback || function(popupMgr, closeCall, data) {};
   
   // setup divs
   this.setupDivs(settings);
}

popupMgr.prototype.setupDivs = function(settings)
{
   // create divs
   this.setupDivOverlay(settings.overlay);
   this.setupDivPopup(settings.popup);
   
   // setup onload hook
   var oldonload = window.onload;
   var divPopup = this.divPopup;
   var divOverlay = this.divOverlay;
   
   var onloadFunc = function()
   {
      // link divs to DOM
      window.document.body.appendChild(divPopup);
      window.document.body.appendChild(divOverlay);
   }
   
   if (typeof window.onload != 'function')
      window.onload = onloadFunc;
   else
   {
      window.onload = function()
      {
         if (oldonload)
            oldonload();
         onloadFunc();
      }
   }
}

popupMgr.prototype.setupDivOverlay = function(settings)
{
   // create overlay div
   this.divOverlay = document.createElement('div');
   var s = this.divOverlay.style;
   
   // styling
   s.zIndex          = 998;
   s.width           = '100%';
   s.height          = '100%';
   s.position        = 'absolute';
   s.top             = '0px';
   s.left            = '0px';
   s.backgroundColor = this.overlayColor;
   s.display         = 'none';
   
   // adding opacity
   s.opacity         = this.overlayOpacity / 100;
   s.MozOpacity      = this.overlayOpacity / 100;
   s.filter          = 'alpha(opacity=' + this.overlayOpacity + ')';
   
   // set remaining stylings
   for (key in settings)
   {
      if (key == 'zIndex' || key == 'width' || key == 'height' || key == 'position' || 
          key == 'top' || key == 'left' || key == 'backgroundColor' || key == 'display' ||
          key == 'opacity' || key == 'filter' || key == 'MozOpacity')
          continue;
      
      eval("\
         if (typeof(s."+key+") != \'undefined\') \
            s."+key+" = \'"+ settings[key] +"\'; \
      ");
   }
}

popupMgr.prototype.setupDivPopup = function(settings)
{
   // create popup div
   this.divPopup = document.createElement('div');
   var s = this.divPopup.style;
   
   // styling
   s.zIndex          = 999;
   s.width           = this.popupWidth + 'px';
   s.height          = this.popupHeight + 'px';
   s.position        = 'absolute';
   s.top             = '0px';
   s.left            = '0px';
   s.display         = 'none';
   
   // set remaining stylings
   for (key in settings)
   {
      if (key == 'zIndex' || key == 'width' || key == 'height' || key == 'position' || 
          key == 'top' || key == 'left' || key == 'display' || key == 'callback')
          continue;
      
      eval("\
         if (typeof(s."+key+") != \'undefined\') \
            s."+key+" = \'"+ settings[key] +"\'; \
      ");
   }
}
popupMgr.prototype.fullsizeOverlay = function()
{
   var yWithScroll = 0;
   var xWithScroll = 0;
   if (document.height && window.scrollMaxY)
   {
      // Firefox
      yWithScroll = document.height + window.scrollMaxY;
      xWithScroll = document.width + window.scrollMaxX;
   }
   else if (document.body.scrollHeight > document.body.offsetHeight)
   {
      // all but Explorer Mac
      yWithScroll = document.body.scrollHeight;
      xWithScroll = document.body.scrollWidth;
   }
   else
   {
      // works in Explorer 6 Strict, Mozilla (not FF) and Safari
      yWithScroll = document.body.offsetHeight;
      xWithScroll = document.body.offsetWidth;
   }
   
   //this.divOverlay.style.width   = xWithScroll + 'px';
   //this.divOverlay.style.height  = yWithScroll + 'px';
}


popupMgr.prototype.deadcenterPopup = function()
{
   // First, determine how much the visitor has scrolled
   var scrolledX = 0;
   var scrolledY= 0;
   if( self.pageYOffset )
   {
      scrolledX = self.pageXOffset;
      scrolledY = self.pageYOffset;
   }
   else if( document.documentElement && document.documentElement.scrollTop )
   {
      scrolledX = document.documentElement.scrollLeft;
      scrolledY = document.documentElement.scrollTop;
   }
   else if( document.body )
   {
      scrolledX = document.body.scrollLeft;
      scrolledY = document.body.scrollTop;
   }
   
   // Next, determine the coordinates of the center of browser's window
   var centerX = 0;
   var centerY = 0;
   if( self.innerHeight )
   {
      centerX = self.innerWidth;
      centerY = self.innerHeight;
   }
   else if( document.documentElement && document.documentElement.clientHeight )
   {
      centerX = document.documentElement.clientWidth;
      centerY = document.documentElement.clientHeight;
   }
   else if( document.body )
   {
      centerX = document.body.clientWidth;
      centerY = document.body.clientHeight;
   }
   
   // Setup the popup coordinates
   var leftOffset = scrolledX + (centerX - this.popupWidth) / 2;
   var topOffset = scrolledY + (centerY - this.popupHeight) / 2; 
   var s = this.divPopup.style;
   s.top    = topOffset + 'px';
   s.left   = leftOffset + 'px';
}

popupMgr.prototype.showPopup = function(data)
{
   // prepare divs
   this.fullsizeOverlay();
   this.deadcenterPopup();
   this.divPopup.innerHTML = '';
   
   // prepare close call
   var obj = this;
   var closeCall = function()
   {
      obj.closePopup()
   }
   
   // custom callback
   this.popupCallback(this, closeCall, data);
   
   // display divs
   this.divOverlay.style.display = 'block';
   this.divPopup.style.display = 'block';
   this.isLoaded();
}

popupMgr.prototype.isLoaded = function ()
{
   var image = document.getElementById("popupImage");
   if (image.complete) 
      this.postShowPopup();
   else
      setTimeout("popup.isLoaded()", 100); //popup is instance of popupMgr
}

popupMgr.prototype.postShowPopup = function()
{
   //Get image & resize pop-up
   var image = document.getElementById("popupImage");
   this.popupWidth  = image.width;
   this.popupHeight = image.height;
   this.divPopup.style.width  = this.popupWidth+'px';
   this.divPopup.style.height = this.popupHeight+'px';
   
   this.deadcenterPopup();
}

popupMgr.prototype.closePopup = function()
{
   // hide divs
   this.divOverlay.style.display = 'none';
   this.divPopup.style.display = 'none';
}


