

/* ********************************************************************************* *
* Classe de LightBox														 		 *
* ********************************************************************************** */

function LightBox(div, varObj, background){
	this.init(div, varObj, background);
}

LightBox.prototype = {
	
	
	/* variáveis */
	width:			null,
	height:			null,
	top:			null,
	left:			null,
	varObj:			null,
	div:			null,
	_div:			null,
	selects: 		null,
	flashs: 		null,
	_flashs: 		null,
	background:		null,
	bgColor:		null,
	bgOpacidade: 	null,
	bgDiv:			null,
	borderDiv:		null,
	scrollbar:		null,
	overflowX:		null,
	overflowY:		null,
	_background: true,
	
	init:function(div, varObj, background){
		this.setSelects();
		this.setFlashs();
		this._setDiv(div);
		
		/* seta as variaveis */
		if( varObj != null )
			this.varObj = varObj;
			
		/* verifica se tem background */
		if ( background != null )
			this._background = background;
			
		this.scrollbar		= false;
		this.bgColor 		= "black";
		this.bgDiv			= "white";
		this.bgOpacidade 	= 85;
		this.overflowX		= false;
		this.overflowY		= false;
	},
	
	
	
	
	
	/* ***************************************************************************** *
	* Set's 																		 *
	* ****************************************************************************** */
	/* selects */
	setSelects:function(){
		this.selects = document.getElementsByTagName("select");
	},
	
	setFlashs:function(){
		this.flashs = document.getElementsByTagName("object");
		this._flashs = document.getElementsByTagName("embed");
	},
	
	setBgColor:function(color){
		this.bgColor = color;
	},
	
	setBgOpacidade:function(opacidade){
		this.bgOpacidade = opacidade;
	},
	
	setBgDiv:function(bgDiv){
		this.bgDiv = bgDiv;	
	},
	
	setBorderDiv:function(borderDiv){
		this.borderDiv = borderDiv;
	},
	
	setOverFlow:function(x, y, width, height){
		if( x != null )
			this.overflowX = x;
		if( y != null )
			this.overflowY = y;
		if( width != null )
			this.width = width;
		if( height != null )
			this.height = height;
	},
	
	_setDiv:function(div) {
		this._div = div;
	},
	
	setDiv:function(){
		this.div = document.getElementById(this._div);
	},	
	
	/* background */
	setBackground:function(){
		this.background.style.background 	= this.bgColor;
		
		this.background.style.width	  	= "100%";
		this.background.style.height	= this.getHeight()+"px";

		
		this.background.style.position		= "absolute";
		this.background.style.top			= "0%";
		this.background.style.left			= "0%";
		this.background.style.opacity		= "."+this.bgOpacidade;
		this.background.style.filter 		= "alpha(opacity="+this.bgOpacidade+")";
		this.background.style.display		= "none";
		this.background.style.textalign		= "center";
		if( this.varObj ) {
			this.background.onclick = new Function(this.varObj+".close()");	
		} else {
			this.background.onclick = "";	
		}
		
			
	},
	
	
	
	/* cria o background */
	createBackground:function(){
		
		var bg = document.getElementById("__background");
		if( bg == null ){
			var newBg 	= document.createElement("div");
			var body	= document.getElementsByTagName("body")[0];
		
			newBg.setAttribute("id"		, "__background");
			newBg.setAttribute("style"	, "text-align:center;");
			
			body.appendChild(newBg);
			
			this.background = newBg;
		}
		else
			this.background = bg;
	},	
	
	/* esconde os selects -- IE bug -- */
	hideSelects:function(){
		for( var i=0; i<this.selects.length; i++ ){
			this.selects.item(i).style.visibility = "hidden";
		}
	},
	
	/* mostra os selects novamente -- IE bug -- */
	_hideSelects:function(){
		for( var i=0; i<this.selects.length; i++ ){
			this.selects.item(i).style.visibility = "";
		}
	},
	
	/* esconde os falashes -- IE bug -- */
	hideFlashs:function(){
		
		for( var i=0; i<this.flashs.length; i++ ){
			this.flashs.item(i).style.visibility = "hidden";
		}
		for( var i=0; i<this._flashs.length; i++ ){
			this._flashs.item(i).style.visibility = "hidden";
		}
	},
	
	/* mostra os selects novamente -- IE bug -- */
	_hideFlashs:function(){
		for( var i=0; i<this.flashs.length; i++ ){
			this.flashs.item(i).style.visibility = "";
		}
		for( var i=0; i<this._flashs.length; i++ ){
			this._flashs.item(i).style.visibility = "";
		}
	},	
	
	/* mostra o lightbox */
	show:function(hide){	
		
		if( (hide==undefined) || (hide!=false) || (hide==null) ){
			this.hideSelects();
			this.hideFlashs();
		}
		
		if (this._background) {
			this.createBackground();
			this.setBackground();
			this.showBackground();
		}
		
		this.setDiv();
		
		this.showDiv();
		this.resize();
		this.control();
	},	
	
	/* fecha o lightbox */
	/* flag => TRUE, mostra novamente a barra de rolagem, FALSE, nao mostra */
	/* hide => TRUE, mostra o flash e imput novamente, FALSE nao mostra */
	
	close:function(flag, hide){
		if ( (flag==undefined) || (flag==null))
			flag = true; 
			
		this.hideDiv();
		if (this._background)
			this.hideBackground();
		
		if( (hide==undefined) || (hide!=false) ){
			this._hideSelects();
			this._hideFlashs();
		}
		
		if (flag)
			this._control();
	},
	
	
	/* mostra o background */
	showBackground:function(){
		this.background.style.display = "block";
	},
	
	/* esconde o background */
	hideBackground:function(){
		this.background.style.display = "none";
	},
	
	/* mostra a div */
	showDiv:function(){
		
		this.div.style.position		= "absolute";
		this.div.style.overflowX	= "auto";
		this.div.style.overflowY	= "auto";
		this.div.style.zIndex 		= "1002";
		this.div.style.display 		= "block";
		this.div.style.padding 		= "10px";
		
		if( this.bgDiv != null )
			this.div.style.background = this.bgDiv;
		if( this.borderDiv != null )
			this.div.style.border = this.borderDiv;
			
		if( this.width )
			this.div.style.width = this.width;
		if( this.height )
			this.div.style.height = this.height;		
			
		if (this.varObj != null) {
			var body = document.getElementsByTagName("body")[0];
			body.setAttribute("onresize", this.varObj+".resize()");
		}
		
	},	
	
	resize:function(){
		this.div.style.left = "50%";
		this.div.style.top	= "50%";
		
		var height = parseInt("-"+(this.div.offsetHeight/2));
		height    += document.body.scrollTop;
		height    += document.documentElement.scrollTop;
		
		
		this.div.style.marginLeft = "-"+(parseInt(this.div.offsetWidth/2))+"px";
		this.div.style.marginTop 	= height+"px";
			
		this.background.style.width	  	= "100%";
		this.background.style.height	= this.getHeight()+"px";	
	},
	
	
	/* esconde a div */
	hideDiv:function(){
		this.div.style.display = "none";
	},
	
	/* controle do lightbox após 'aberto' show */
	control:function(){		
		if( this.overflowX )
			document.body.style.overflowX = "hidden";
		if( this.overflowY )
			document.body.style.overflowY = "hidden";
	
	},
	
	_control:function(){		
		if( (this.overflowX) || (this.overflowX == null) )
			document.body.style.overflowX = "auto";
		if( (this.overflowY) || (this.overflowY == null) )
			document.body.style.overflowY = "auto";		
	},
	
	
	getWidth:function() {
		
		var x = 0;
		
		if( window.innerWidth )
			x = window.innerWidth;
		else {
			if( document.documentElement )
				x = document.documentElement.clientWidth;
			else {
				if( document.body )
					x = document.body.clientWidth;
				else
					x = window.screen.width;
			}
		}
		
		return x;
		
	},

	
	
	getHeight:function(){
		
		var y = 0;
		
		if( window.innerHeight && (y < window.innerHeight) )
			y = window.innerHeight;
		
		if( document.documentElement && (y < document.documentElement.clientHeight) )
			y = document.documentElement.clientHeight;
		
		if( document.body && (y < document.body.clientHeight) )
			y = document.body.clientHeight;
		
		if( window.screen.height && (y < window.screen.height) && ( document.documentElement.clientHeight < document.body.clientHeight ) )
			y = window.screen.height;
		
		return y;
	}  
}
