/**
* Lightbox
*
* This libary is used to create a lightbox in a web application.  This library
* requires the Prototype 1.6 library and Script.aculo.us core, effects, and dragdrop
* libraries.  To use, add a div containing the content to be displayed anywhere on 
* the page.  To create the lightbox, add the following code:
*
*	var test;
*	
*	Event.observe(window, 'load', function () {
*		test = new Lightbox('idOfMyDiv');
*	});
*	
*	Event.observe('lightboxLink', 'click', function () {
*		test.open();
*	});
*
*	Event.observe('closeLink', 'click', function () {
*		test.close();
*	});
*     
*/

var Lightbox = Class.create();

Lightbox.prototype = {
		open : function () {
	this._centerWindow(this.container);
	this._fade('open', this.container);
	},
	
	openIn : function (top,left) {
		this._putWindow(this.container,top,left);
		this._fade('open', this.container);
	},

	close : function () {
		this._fade('close', this.container);
	},
	
	_fade : function fadeBg(userAction,whichDiv){
		if(userAction=='close'){
			new Effect.Opacity(this.bgFade,
					   {duration:.5,
					    from:0,
					    to:0,
					    afterFinish:this._makeInvisible.bind(this),
					    afterUpdate:this._hideLayer(whichDiv)});
		}else{
			new Effect.Opacity(this.bgFade,
					   {duration:.5,
					    from:0,
					    to:0,
					    beforeUpdate:this._makeVisible.bind(this),
					    afterFinish:this._showLayer(whichDiv)});
		}
	},
	_makeVisible : function makeVisible(){
        this.bgFade.style.zIndex = "102";
		this.bgFade.style.visibility="visible";
        this.bgFade.style.left = "0px";
        this.bgFade.style.top = "0px";

        this.bgFade.style.width = document.body.offsetWidth +"px";
        var D = document;
        this.bgFade.style.height= Math.max(
                Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
                Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
                Math.max(D.body.clientHeight, D.documentElement.clientHeight)
            );
	},

	_makeInvisible : function makeInvisible(){
		this.bgFade.style.visibility="hidden";
	},

	_showLayer : function showLayer(userAction){

		$(userAction).style.display="block";
	},
	
	_hideLayer : function hideLayer(userAction){
		$(userAction).style.display="none";
	},
	
	_centerWindow : function centerWindow(element) {
		var windowHeight = parseFloat($(element).getHeight())/2; 
		var windowWidth = parseFloat($(element).getWidth())/2;
        var scrollTop= self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);

		if(typeof window.innerHeight != 'undefined') {
			$(element).style.top = Math.round(document.body.offsetTop + ((window.innerHeight - $(element).getHeight()))/2)+'px';
			$(element).style.left = Math.round(document.body.offsetLeft + ((window.innerWidth - $(element).getWidth()))/2)+'px';
		} else {
			$(element).style.top = Math.round(document.body.offsetTop + ((document.documentElement.offsetHeight - $(element).getHeight()))/2)+'px';
			$(element).style.left = Math.round(document.body.offsetLeft + ((document.documentElement.offsetWidth - $(element).getWidth()))/2)+'px';
		}
		h = (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight));
         $(element).style.top = Math.round(scrollTop  + h/2- windowHeight)+'px';
	},
	
	_putWindow : function centerWindow(element,top,left) {
		var windowHeight = parseFloat($(element).getHeight())/2; 
		var windowWidth = parseFloat($(element).getWidth())/2;
        var scrollTop= self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);

      
		if(typeof window.innerHeight != 'undefined') {
			if(top==null) {
				$(element).style.top = Math.round(document.body.offsetTop + ((window.innerHeight - $(element).getHeight()))/2)+'px';
			}
			else {
				$(element).style.top = top+'px';
			}
			if(left==null) {
				$(element).style.left = Math.round(document.body.offsetLeft + ((window.innerWidth - $(element).getWidth()))/2)+'px';
			}
			else {
				$(element).style.left = left+'px';
			}
		} else {
			if(top==null) {
				$(element).style.top = Math.round(document.body.offsetTop + ((document.documentElement.offsetHeight - $(element).getHeight()))/2)+'px';
			}
			else {
				$(element).style.top = top+'px';
			}
			if(left==null) {
				$(element).style.left = Math.round(document.body.offsetLeft + ((document.documentElement.offsetWidth - $(element).getWidth()))/2)+'px';
			}
			else {
				$(element).style.left = left+'px';
			}
		}
		
		
		h = (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight));
        if(top==null) { 
        	$(element).style.top = Math.round(scrollTop  + h/2- windowHeight)+'px';
        }
	},
	
	initialize : function(containerDiv, draggable) {
		this.container = containerDiv;
		this._hideLayer(this.container);
		document.body.insertBefore(this.container, document.body.firstChild);
		var bgFade = $('bg_fade');
		if(!bgFade) {
			bgFade = $(document.createElement('div'));
			bgFade.id = 'bg_fade';
			bgFade.addClassName('bg-fade');
			bgFade.innerHTML="<!--[if lte IE 6.5]><iframe></iframe><![endif]-->";

			document.body.insertBefore(bgFade, document.body.firstChild);
		}
		this.bgFade = bgFade;
		if(draggable) {
			new Draggable(this.container);
		}
	}
}
