/**
* 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({
	initialize : function(containerDiv) {
		var options = Object.extend({
			top: false,
			left: false
		}, arguments[1] || {});		
		this.options = options;
		
		this.container = containerDiv;
		if($('bg_fade') == null) {
			var screen = new Element('div', {'id': 'bg_fade'});
			document.body.appendChild(screen);
		}
		//new Draggable(this.container);
		this._hideLayer(this.container);
		
		$('bg_fade').setStyle({opacity: 0.5}) //
		//new Effect.Opacity('bg_fade', {to:0.5}); //set opacity of div on load
	},
	
	open : function () {
		this._centerWindow(this.container);
		this._fade('open', this.container);
	},
	
	close : function () {
		this._fade('close', this.container);
	},
	
	_fade : function fadeBg(userAction,whichDiv){  //kill fade - just blackout straight
		if(userAction=='close'){
			this._makeInvisible();
			this._hideLayer(whichDiv);
		}else{
			this._makeVisible();
			this._showLayer(whichDiv)
		}
	},
	
	_docheight: function getDocHeight() {
		var D = document;
		return 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)
		);
	},
	
	_makeVisible : function makeVisible(){
		$("bg_fade").style.visibility="visible";
	},

	_makeInvisible : function makeInvisible(){
		$("bg_fade").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 topOffset = window.pageYOffset;
		if (topOffset = 'undefined') { topOffset = document.body.scrollTop; }
		if(typeof window.innerHeight != 'undefined') {
			$(element).style.top = Math.round(document.body.offsetTop + topOffset + ((window.innerHeight - $(element).getHeight()))/3)+'px';
			$(element).style.left = Math.round(document.body.offsetLeft + ((window.innerWidth - $(element).getWidth()))/2)+'px';
		} else {
			$(element).style.top = Math.round(document.body.offsetTop + topOffset + ((document.documentElement.offsetHeight - $(element).getHeight()))/3)+'px';
			$(element).style.left = Math.round(document.body.offsetLeft + ((document.documentElement.offsetWidth - $(element).getWidth()))/2)+'px';
		}
		
		if (this.options.top) 
			$(element).style.top = this.options.top;
		if (this.options.left) 
			$(element).style.left = this.options.left;
	
		$("bg_fade").style.height = this._docheight(); //fix so that background DIV also scrolls w/ the page
	},
	
	quickopen : function () {
		this._quickfade('open', this.container);
	},
	
	quickclose : function () {
		this._quickfade('close', this.container);
	},
	
	_quickfade : function fadeBg(userAction,whichDiv){
		if(userAction=='close'){
			this._hideLayer(whichDiv);			
		}else{
			this._showLayer(whichDiv);
		}
	}
	
});

