
/* *********************************************
	LIGHT BOX - scott@genii.com.au
	December 11th 2008
********************************************* */

function lightbox()
{
	this.colour        = "#000000";
	this.opacity       = "85";
	//	target is text
	this.target  	   = false;
	this.targetResize  = false;
	this.targetHeight  = false;
	//	is so we can destory the object later
	this.lb	       = false;
	this.ol        = false;
	this.wrapper   = false;
	this.box	   = false;
	this.baseIndex = 10;
	this.classname = "lightbox";
	
	this.overlayDestroy = true;
	
	this.create = function()
	{
		//	create new lightbox container
		this.lb = document.createElement("div");
		this.lb.className = this.classname; 
		this.lb.style.display = "none";
		//	create target
		if( this.target !== false && typeof(this.target) != "object" ){
			this.target = dom.byId(this.target);
		}
		//	create opacity overlay
		this.ol = overlay(this);
		//	CREATE CONTENT AREA
		//	create inner content box element
		this.box = document.createElement("div");
		this.box.style.margin    = "auto"; 
		this.box.style.textAlign = "left";
		this.box.style.position  = "relative";
		this.box.style.backgroundColor = "#fff"; /* default background colour*/
		this.box.style.display = "none";
		this.box.onclick = function(e){
				dom.stopProp(e);
			}
		//	create wrapper overlay for content
		this.opacity = "101";
		this.colour  = "";
		
		this.wrapper  = overlay(this);
		this.wrapper.appendChild(this.box);		
		
		//	append elements to light box 	
		this.lb.appendChild(this.ol);
		this.lb.appendChild(this.wrapper);
		
		//	add to the DOM
		if( this.target ) this.targetHeight = this.target.offsetHeight;
		dom.addNode(( this.target ? this.target : document.body),this.lb , {add:"before"});

		return this.box;
	};


	this.show = function(fade)
	{
		this.lb.style.display  = "block";
		this.box.style.display = "block";
		//	resize the internal content as lightbox needs to be inserted
		if( this.target && this.targetResize ){
			var t = this.target;
			if( this.box.offsetHeight > this.targetHeight ){
				t.style.height = this.box.offsetHeight +"px";	
			}else{
				t.style.height = this.targetHeight +"px";	
			}
		}
		
	};
	
	//	setup this way so an opacity can be used separately
	var overlay = function(self)
	{
		var ol = document.createElement("div");
		ol.style.position 		 = "absolute";
		
		ol.style.backgroundColor = self.colour;
		ol.style.zIndex 		 = self.baseIndex + 1;
		ol.style.textAlign       = "center";
				
		if( self.target ){
			var t = self.target;
			ol.style.height   = dom.getHeight(t, true);
			ol.style.width 	  = dom.getWidth( t, true);
			ol.style.left =  "0";
		}else{
			ol.style.left  = "0";
			ol.style.width = "100%";
			if( document.body.offsetHeight > document.documentElement.clientHeight ){
				//there is an over hang...scroll bar maybe?
				ol.style.height = document.body.offsetHeight + 15 +"px";
			}else{
				ol.style.height = document.documentElement.clientHeight +"px";
			}
		}
		//	destroy when on overlay
		if( self.overlayDestroy ){
			ol.onclick = function(e){
				self.deactivate();
				dom.stopProp(e);
			}
		}
		//	set opacity 
		ol.style.opacity 	  = (self.opacity/101).toFixed(2);  
		ol.style.MozOpacity   = (self.opacity/101).toFixed(2);
		ol.style.KhtmlOpacity = (self.opacity/100).toFixed(2);
		ol.style.filter = "alpha(opacity=" + self.opacity + ")";
		//sfx.setOpacity(ol, self.opacity);
		return ol;
	};
	
	//	add HTML text directly to the lightbox
	this.html = function(html)
	{
		dom.parseJS(dom.parseHTML(this.box, html));
	};
	
	
	this.deactivate = function()
	{
		dom.delElement(this.lb);
		this.lb = false;
		//	 set the original height back to the target
		if( this.target && this.targetResize ){
			var t = this.target;
			t.style.height = this.targetHeight +"px";
		}
	};
};
