/* ---------------------------------------------------------------
   Author : Remi Palard
   october 2006
   remi.palard@gmail.com  
   
   Contributor : Michael Daniaux
   december 2006
   load/unload media print css file
--------------------------------------------------------------- */

function popupLayer(oOptions) {
	var layerName;
	var draggable;
	var closeOnClick;
	var reference;
	var openedLayer;
	var centered;
	var deltaX;
	var deltaY;
	var fade;	
	var oOptions;
	var _page;
	var _window;
	
	// Set vars
	this.oOptions = augment({
		draggable: true,
		closeOnClick : false,
		centered: false,
		deltaX: 0,
		deltaY: 0,
		reference: false
	}, oOptions );	
	
	this.draggable = this.oOptions.draggable;
	this.closeOnClick = this.oOptions.closeOnClick;
	this.reference = this.oOptions.reference;		
	this.centered = this.oOptions.centered;
	this.deltaX = this.oOptions.deltaX;	
	this.deltaY = this.oOptions.deltaY;	
	this.fade = 'fadeLayer';
		
	/* --------------------- */
	
	popupLayer.prototype.init = function(id,URL) {
		var pos;
		var ajaxCall;			
		
		// Css files
		loadDynamicCSSPrintFile("/static/css/renom09/r09_print_layer.css");
		
		// Set opened Layer
		this.openedLayer = id;		
		
		// Include	
		ajaxCall = new Ajax.Updater( id, URL, { method: 'get',onComplete: this.setPosition.bind(this) });    
	
		/* Display */
		Element.show(this.fade);
		Element.show(id);				
	
		// Draggable
		if (this.draggable) this.layerName = new Draggable(id, {starteffect:false,reverteffect:false,endeffect:false,revert:false});	
	}
	
	/* --------------------- */
	
	popupLayer.prototype.close = function() {

		// Css files		
		unloadDynamicCSSPrintFile("r09_print_layer.css");

		// Close
		Element.hide(this.openedLayer);
		Element.hide(this.fade);
		
		// Clean-up
		$(this.openedLayer).innerHTML = '';
		if (this.closeOnClick) $(this.fade).onclick = "";
		window.onscroll = "";
		window.onresize = "";	
		
		// Draggable
		if (this.draggable) this.layerName.destroy();
		
		// RT150890 - NOT TESTED YET
		//alert(this.openedLayer);
		if (this.openedLayer == 'benefitsConfirm') {
			//alert('refreshing');
			location.reload(true);
		}
		
	}	
	
	/* --------------------- */
	
	popupLayer.prototype.getPageSize = function() {
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xMaxScroll = document.body.scrollWidth;
			yMaxScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xMaxScroll = document.body.scrollWidth;
			yMaxScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xMaxScroll = document.body.offsetWidth;
			yMaxScroll = 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;
		}	

		// Page + dark side of the window 
		this._page = new Object();
		this.test = xMaxScroll;

		// The visible part of the window 		 
		this._window = new Object(); 		
		
		this._page.width = xMaxScroll;
		this._page.height = yMaxScroll;		
		this._window.width = windowWidth;
		this._window.height = windowHeight;		
	}

	/* --------------------- */
	
	popupLayer.prototype.getPageScroll = function() {
		var yScroll;
  	
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
		}
  	
		return yScroll;
	}	

	/* --------------------- */
	
	popupLayer.prototype.setPosition = function() {	
		var layerSize = new Object();
		var posX, posY;		
	
		// Get dimension
		layerSize = Element.getDimensions(this.openedLayer);
		this.getPageSize();	
		
		// Default position : top 30px
		posX = (this._window.width - layerSize.width) /2;		
		posY = this.getPageScroll() + 30;		
			
		// Centered position
		if (this.centered) {
			posY = (this._window.height - layerSize.height) /2;
			posX = (this._window.width - layerSize.width) /2;
			
			// Set min
			posX = (posX < 0) ? 0 : posX;
			posY = (posY < 0) ? 0 : posY;			
		}
		
		// Reference
		if ($(this.reference)) {		
			var el = $(this.reference);				
			posX = el.offsetLeft;
      posY = el.offsetTop;    
     	Element.scrollTo(el);			
		}
		
		// Manual adjustment
		posX = posX + this.deltaX;
		posY = posY + this.deltaY;		

        // If xRez debug is enabled...
        if(isXRezDebug) {
    		posX = 100;
    		posY = 100;
        }

		// Fade layer                         
		$(this.fade).style.height = this._page.height + 'px';			
		$(this.fade).style.width = this._page.width + 'px';		
		
		// Close		
		if (this.closeOnClick) $(this.fade).onclick = this.close.bindAsEventListener(this);			
				
		// Set value
		$(this.openedLayer).style.position = 'absolute';
		$(this.openedLayer).style.top = posY + 'px';		
		$(this.openedLayer).style.left = posX + 'px'; 		
				
		// Events
		window.onresize = this.setPosition.bindAsEventListener(this);				
	}		
	
	/* --------------------- */
	
}

/* --------------------------------------------------------------- */