var OverlayHandler = Class.create();
OverlayHandler.prototype = {
  
  // By default we center the overlay in the parent node, unless this has been specified
  initialize: function(options) {
    this.setOptions(options);
    this.lightOverlay = $(this.options.lightOverlay);
    this.darkBackground = $(this.options.darkBackground);
    this.pageBody = this.options.pageBody ? $(this.options.pageBody) : this.darkBackground.parentNode;
  },
  
  setOptions: function(options) {
    this.options = {
      lightOverlay: 'lightOverlay',
      darkBackground: 'darkBackground',
      pageBody: null,                      // Page body used for positioning the overlay widht and height
      useAjaxResponse: false                  // Use the AjaxResponse handling formatting
    }
    Object.extend(this.options, options || {});
  },
  
  processAjaxResponse: function(transport,options) {
    if ( options && options.useAjaxResponse) {
      new AjaxResponse({
        onSuccess: function(result) { this.fillAndShowOverlay(result) }.bind(this), 
        login: function() { this.displayUrl('/login?lo=overlay') }.bind(this)
      } ).process(transport)
    } else {
      this.fillAndShowOverlay(transport.responseText,options);
    }
  },
  
  displayUrl: function (URL,options) {
    // backHandler.allowNavFromOverlay = false;
    new Ajax.Updater ( this.options.lightOverlay, URL,
    {
      method: 'get',
      evalScripts: true,
      onComplete: function(transport) { this.showOverlay(options) }.bind(this)
    });
  },
  
  repaintWithUrl: function(URL){
    new Ajax.Updater ( this.options.lightOverlay, URL,
    {
      method: 'get',
      evalScripts: true
    });
  },
  
  displayUrlQuickFade: function(URL,options) {
    this.displayUrl(URL,{effects: {duration: 0.05}});
  },
  
  // Options currently only has the options for the effects
  closeOverlay: function (options) {
    this.restoreScrollPositions();
    // scroll(0,0);   // if not, you get display errors in webkit-type browsers
    // Could not remember the bug that this was causing for webkit so removed the line.
    this.setVisibilityOfSelects("visible"); //For fucking ie bug
    if (typeof options == 'undefined') options = {}
    Effect.Fade(this.lightOverlay,options.effects);
    this.darkBackground.style.display="none"
    // Just here for tracking
    // No longer tracking the close overlay 2008-10-07
    // new Ajax.Request("/cs/clov");
  },
  
  instantCloseOverlay: function () {
    this.setVisibilityOfSelects("visible"); //For fucking ie bug
    this.lightOverlay.style.visibility="hidden";
    this.darkBackground.style.display="none"
  },
  
  fillAndShowOverlay: function (content, options) {
    this.lightOverlay.innerHTML = content;
    this.showOverlay(options);
  },
    
  showOverlay: function(options) {
    this.saveScrollPositions();
    this.setVisibilityOfSelects("hidden"); //For fucking ie bug
    if ( !options ) options = {};
    this.lightOverlay.style.visibility='hidden';
    this.lightOverlay.style.display='block';
    this.setOverlayPosition(options.where);
    this.setBackgroundHeight();
    // Because of a bug in effect.js, if we set opacity to 0, it will be ignored
    this.lightOverlay.setOpacity(0.0001);
    this.lightOverlay.style.visibility='visible'; 
    this.darkBackground.style.display="block"
    Effect.Appear(this.lightOverlay,options.effects);
  },
  
  setOverlayPosition: function (where) {
    if ( !where ) where = {} ;
    if ( where == 'center' ) {
      windowHandler.centerInWindow(this.lightOverlay);
    } else if ( where.event == 'event') {
      windowHandler.showHere(this.lightOverlay,event,where.event)
    } else {
      this.setOverlayXPos();
      this.setOverlayYPos()
    }
  },
  
  setOverlayXPos: function(){    
    var wo = this.lightOverlay.offsetWidth;
    // alert(windowHandler.windowSize().width + ',' + windowHandler.windowSize().height);
    // alert(this.pageBody.offsetWidth + ',' + this.pageBody.offsetHeight);
    var wb = this.pageBody.offsetWidth;
    var l;
    if (wb > wo) {
      l = ((wb-wo)/2)
    } else {
      l = 5
    }
    this.lightOverlay.style.left = l + "px";
  },
  
  // Set the position of the overlay 
  // There are several strategies supported
  setOverlayYPos: function() {
    var po = window.pageYOffset || document.documentElement.scrollTop;
    // alert("po = "+po);
    var t = po + 20;
    this.lightOverlay.style.top = t + "px";
    // console.log(this.lightOverlay.style.top);
    // alert(this.lightOverlay.style.top)
  },
  
  setBackgroundHeight: function(){
    // var docD = windowHandler.documentDimensions();
    // $('darkBackground').style.width = docD.width;
    // $('darkBackground').style.height = docD.height;
    // // $('screen').style.height=$('container').offsetHeight;
    // // $('screen').style.top=-$('container').offsetHeight;
    // $('darkBackground').style.display = 'block';
    // $('darkBackground').style.left=0;
    // $('darkBackground').style.top=0;

    var oh = this.lightOverlay.offsetTop + this.lightOverlay.offsetHeight;
    var bh = this.pageBody.offsetHeight;
    var fh = Math.max(oh,bh,windowHandler.windowSize().height);
    // console.log("oh="+oh+", bh="+bh+", window heigth="+windowHandler.windowSize().height+", fh="+fh )
    this.darkBackground.style.height = fh + 'px'
  },
  
  setVisibilityOfSelects: function(state){
    var sels = document.getElementsByTagName("select");
    for ( var i=0; i < sels.length; i++ ){
      sels[i].style.visibility = state
    }
    // but not for items hat are on the overlay itself
    var overlay_sels = this.lightOverlay.getElementsByTagName("select");
    for ( var i=0; i < overlay_sels.length; i++ ){
      overlay_sels[i].style.visibility = 'visible';
    }    
  },
  
  overlayUp: function() {
    return this.lightOverlay.style.visibility == 'visible' && this.lightOverlay.style.display != 'none';
  },
  
  preOverlayScrollX: 0,
  preOverlayScrollY: 0,
  
  saveScrollPositions: function() {
    var scroll = windowHandler.scrollXY();
    this.preOverlayScrollY = scroll.ypos;
    this.preOverlayScrollX = scroll.xpos;
    // console.log("saved scroll "+this.preOverlayScrollX + ", " + this.preOverlayScrollY);
  },
  
  restoreScrollPositions: function() {
    // console.log("setting scroll to "+this.preOverlayScrollX + ", " + this.preOverlayScrollY);
    scroll(this.preOverlayScrollX,this.preOverlayScrollY);
  }
}