var PopupBox = function(options) {
  this.init(options);
};

PopupBox.prototype = {
  overlay : null,
  box : null,
  closeLink : null, 

  showBox : function(boxWidth, boxHeight) {
    this.setDimensions(boxWidth, boxHeight);
    if($.browser.msie) $('select').hide();
    $(this.overlay).show();
    this.center();
    return false;
  },
  
  setDimensions : function(width, height) {
    var windowSize = this.getPageDimensions();
    if(width) {
      if(width < windowSize[0]) {
        this.box.style.width = width + 'px';
      } else {
        this.box.style.width = (windowSize[0] - 50) + 'px';
      }
    }
    if(height) {
      if(height < windowSize[1]) {
        this.box.style.height = height + 'px';
      } else {
        this.box.style.height = (windowSize[1] - 50) + 'px';
      }
    }
  },
  
  hideBox : function(){
    if($.browser.msie) $('select').show();
    $(this.overlay).hide();
    $(this.box).fadeOut();
    return false;
  },
  
  // taken from lightbox js, modified argument return order
  getPageDimensions : function(){
    var xScroll, yScroll;
  
    if (window.innerHeight && window.scrollMaxY) {  
      xScroll = document.body.scrollWidth;
      yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
      xScroll = document.body.scrollWidth;
      yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
      xScroll = document.body.offsetWidth;
      yScroll = document.body.offsetHeight;
    }
    
    var windowWidth, windowHeight;
    if (self.innerHeight) {  // all except Explorer
      windowWidth = self.innerWidth;
      windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
      windowWidth = document.documentElement.clientWidth;
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
      windowWidth = document.body.clientWidth;
      windowHeight = document.body.clientHeight;
    }  
    
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
      pageHeight = windowHeight;
    } else { 
      pageHeight = yScroll;
    }
  
    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){  
      pageWidth = windowWidth;
    } else {
      pageWidth = xScroll;
    }
    arrayPageSize = new Array(windowWidth,windowHeight,pageWidth,pageHeight) 
    return arrayPageSize;
  },
  
  center : function(){
    try{
      element = this.box;
    }catch(e){
      return;
    }
    var windowSize = this.getPageDimensions();
    var window_width  = windowSize[0];
    var window_height = windowSize[1];
    
    this.overlay.style.height = windowSize[3] + 'px';
    
    element.style.position = 'absolute';
    element.style.zIndex   = 99;
  
    var scrollY = 0;
  
    if ( document.documentElement && document.documentElement.scrollTop ){
      scrollY = document.documentElement.scrollTop;
    }else if ( document.body && document.body.scrollTop ){
      scrollY = document.body.scrollTop;
    }else if ( window.pageYOffset ){
      scrollY = window.pageYOffset;
    }else if ( window.scrollY ){
      scrollY = window.scrollY;
    }

    $(element).fadeIn();
    var setX = ( window_width  - element.offsetWidth  ) / 2;
    var setY = ( window_height - element.offsetHeight ) / 3 + scrollY;
  
    setX = ( setX < 0 ) ? 0 : setX;
    setY = ( setY < 0 ) ? 0 : setY;
  
    element.style.left = setX + "px";
    element.style.top  = setY + "px";
    
  },
  
  init : function(options) {
    $('<div></div>').attr('id', 'overlay').addClass('hide').css('opacity', .7).appendTo(document.body);
    for (var i in options) {
      this[i] = document.getElementById(options[i]);
    }
    var self = this;
    $(this.box).appendTo(document.body);
    $(this.closeLink).click(function(e) {
      e.preventDefault();
      self.hideBox();
    });
  }
}
