﻿/**
* 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({
	open : function () {
		activewindow = true;
		this._centerWindow(this.container);
		this._fade('open', this.container);
	},
	
	openNoFade : function() {
		activewindow = true;
		this._centerWindow(this.container);
		this._showLayer(this.container);
	},
	
	close : function () {
		this._fade('close', this.container);
		activewindow = false;
	},
	
	closeNoFade : function() {
		this._hideLayer(this.container);
		this._fade('close', this.container);
		activewindow = false;
	},
	
	_fade : function fadeBg(userAction,whichDiv){
		if(userAction=='close'){
			new Effect.Opacity('bg_fade',
					   {duration:.3,
					    from:0.8,
					    to:0,
					    afterFinish:this._makeInvisible,
					    afterUpdate:this._hideLayer(whichDiv)});
		}else{
			new Effect.Opacity('bg_fade',
					   {duration:.3,
					    from:0,
					    to:0.8,
					    beforeUpdate:this._makeVisible,
					    afterFinish:this._showLayer(whichDiv)});
		}
		
	},
	
	_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;

		if(typeof window.innerHeight != 'undefined') {
			var top = Math.round(document.body.offsetTop + ((window.innerHeight - $(element).getHeight()))/2);
			
			if(top < 0)
			{
				top = 0;
			}
			
			$(element).style.top = top + 'px';
			$(element).style.left = Math.round(document.body.offsetLeft + ((window.innerWidth - $(element).getWidth()))/2)+'px';
		} else {
			var top = Math.round(document.body.offsetTop + ((document.documentElement.offsetHeight - $(element).getHeight()))/2);
			
			if(top < 0)
			{
				top = 0;
			}
			
			$(element).style.top = top + 'px';
			$(element).style.left = Math.round(document.body.offsetLeft + ((document.documentElement.offsetWidth - $(element).getWidth()))/2)+'px';
		}
	},
	
	initialize : function(containerDiv) {
		this.container = containerDiv;
	
		if($('bg_fade') == null) {
			var screen = new Element('div', {'id': 'bg_fade'});
		
			/* Fixa bredd och höjd på bakgrunds faden   */
			if(Prototype.Browser.IE)
			{
 	 			var newHeight = Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight);
  				var newWidth = Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth);
				screen.style.height = newHeight + 'px';
				screen.style.width = newWidth + 'px';
			} 
			else if(Prototype.Browser.WebKit) 
			{
  				screen.style.height = (document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight) + 'px';
				screen.style.width = (document.body.scrollWidth > document.body.offsetWidth ? document.body.scrollWidth : document.body.offsetWidth) + 'px';
			} 
			else 
			{
  				screen.style.height = window.innerHeight + window.scrollMaxY + 'px';
				screen.style.width = window.innerWidth + window.scrollMaxX + 'px';
			}
			/********************************************/
			
			document.body.appendChild(screen);
		}
		
		//new Draggable(this.container);
		this._hideLayer(this.container);
	}
});

