/* modal overlay creator */
var overlay = {
  _overlay: null,  

  create: function() {
    if (!this._overlay) {

      this._overlay = {
        background: $(document.createElement("div")),
        content:    $(document.createElement("div"))
      }
      
      Element.insert(document.body, this._overlay.background);
      Element.insert(document.body, this._overlay.content);
      
      this._overlay.background.addClassName("overlay-background");
      this._overlay.content.addClassName("overlay-content");
     
      Ajax.Responders.register({ onComplete: this._update.bind(this) });
      Event.observe(window, 'resize', this._update.bind(this));
                  
    }
    
    this.show();
    this._update();    

    document.fire("overlay:create");
    
    return this._overlay.content;
  },
    
  remove: function() {
    if (this._overlay) {
      this.clear();
      this.hide();

      Element.remove(this._overlay.content);
      Element.remove(this._overlay.background);

      this._overlay = null;

      Ajax.Responders.unregister({ onComplete: this._update.bind(this) });
      Event.stopObserving(window, 'resize', this._update);
      
      document.fire("overlay:remove");
    }
  },
  
  clear: function() {
    this._overlay.content.update('');    
  },
  
  hide: function() {
    if (!this._overlay) return;
    this._overlay.content.hide();
    this._overlay.background.hide();
  },
  
  show: function() {
    if (!this._overlay) return;
    this._overlay.content.show();
    this._overlay.background.show();    
  },
  
  _update: function() {
    /* update the height over the overlay if resized */
    if (this._overlay) {
      
      var pageHeight    = this.getPageSize()[1];
      var contentHeight = $$('.overlay-content')[0].getHeight();
      
      var adjustedHeight = (contentHeight >= pageHeight) ? contentHeight : pageHeight;

      // alert(pageHeight + ", " + contentHeight);

      document.body.setStyle({ height: adjustedHeight + 'px' });
      this._overlay.background.setStyle({ height: adjustedHeight + 'px' });
      
    }
  },
  
  /* via lightbox2 */
  getPageSize: function() {

    var xScroll, yScroll;

  	if (window.innerHeight && window.scrollMaxY) {	
  		xScroll = window.innerWidth + window.scrollMaxX;
  		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
  		if(document.documentElement.clientWidth){
  			windowWidth = document.documentElement.clientWidth; 
  		} else {
  			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 = xScroll;		
  	} else {
  		pageWidth = windowWidth;
  	}

  	return [pageWidth, pageHeight];
  }
  
}