var PopupWindow = {
  hideAll: function()
  {
    popupWindows = document.getElementsByClassName('popup');
    
    popupWindows.each
    (
      function(box)
      {
        Element.hide(box);
      } // function
    ) // each

    if ($('overlay'))
    {
      Element.remove('overlay');
    } // if
  } // hideAll()
} // PopupWindow

PopupWindow.base = Class.create();

PopupWindow.base.prototype = {
  initialize: function(element, options)
  {
    // first off, hide all popup windows
    PopupWindow.hideAll();
  
    this.element = $(element);

    this.options = Object.extend
    ( { popupClassName : 'popup'
      , closeOnOverlayClick : false
      , externalControl : false
      }
    , options || {}
    )

    // create the full-page overlay
    new Insertion.Before(this.element, "<div id='overlay' style='display:none;'></div>");
    Element.addClassName(this.element, this.options.popupClassName);

    // add an popup class to the element so we can close all pop-up quickly
    Element.addClassName(this.element, 'popup');
    
    if (this.options.closeOnOverlayClick)
    {
      Event.observe($('overlay'), 'click', this.hidePopupWindow.bindAsEventListener(this));
    } // if
    
    if (this.options.externalControl)
    {
      Event.observe($(this.options.externalControl), 'click', this.hidePopupWindow.bindAsEventListener(this));
    } // if

    if (this.options.okControl)
    {
      Event.observe($(this.options.okControl), 'click', this.hidePopupWindow.bindAsEventListener(this));
    } // if

    this.showPopupWindow(); 
  }, // initialize()
  
  
  showPopupWindow : function()
  {
    // show the overlay
    Element.show('overlay');

    // center the popup window
    this.center();

    // show the popup window
    new Effect.Appear(this.element, {duration: .2});

    return false;
  }, // showPopupWindow()
  
  
  hidePopupWindow : function(evt)
  {
    // remove the popup window class we added when showing the window
    Element.removeClassName(this.element, this.options.popupClassName);

    // hide the popup window
    Element.hide(this.element);

    // remove the overlay DIV
    Element.remove('overlay');

    return false;
  }, // hidePopupWindow()
    
  center : function()
  {
    var my_width  = 0;
    var my_height = 0;
    
    if (typeof(window.innerWidth) == 'number')
    {
      my_width  = window.innerWidth;
      my_height = window.innerHeight;
    } // if
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
    {
      my_width  = document.documentElement.clientWidth;
      my_height = document.documentElement.clientHeight;
    } // else if
    else if (document.body && (document.body.clientWidth || document.body.clientHeight))
    {
      my_width  = document.body.clientWidth;
      my_height = document.body.clientHeight;
    } // else if

    this.element.style.position = 'absolute';
    this.element.style.zIndex = 99;

    var scrollY = 0;

    if (document.documentElement && document.documentElement.scrollTop)
    {
      scrollY = document.documentElement.scrollTop;
    } // if
    else if (document.body && document.body.scrollTop)
    {
      scrollY = document.body.scrollTop;
    } // else if
    else if (window.pageYOffset)
    {
      scrollY = window.pageYOffset;
    } // else if
    else if (window.scrollY)
    {
      scrollY = window.scrollY;
    } // else if
    
    var elementDimensions = Element.getDimensions(this.element);
    
    var setX = (my_width  - elementDimensions.width) / 2;
    var setY = (my_height - elementDimensions.height) / 2 + scrollY - 250;
    
    setX = (setX < 0) ? 0 : setX;
    setY = (setY < 0) ? 0 : setY;
    
    this.element.style.left = setX + "px";
    this.element.style.top  = setY + "px";
  } // center()
} // PopupWindow


function showPopupWindow(parentId, popupId, focusId)
{
  new PopupWindow.base(popupId);

  document.forms[0][focusId].focus();
} // showPopupWindow()


function hidePopupWindow(popupId)
{
  new Effect.Fade($(popupId), {duration: .2});
} // hidePopupWindow()
