/**
* Classe para uso de lightbox
*
* EXEMPLO:
*
* var lbBox = new LightBox('lbBox', false, true, true, false);
*/
function LightBox(div, close_background, show_background, fade_in, fade_out){
	this.construct(div, close_background, show_background, fade_in, fade_out);
}




LightBox.prototype = {
	
	
	/**
	* variaveis
	*/
	width:				null,
	height:				null,
	top:				null,
	left:				null,
	background:			null,
	obj:				null,
	div:				null,
	close_background: 	false,
	fade_in:			false,
	fade_out:			false,
	vel_fade_in:		10,
	vel_fade_out:		10,
	
	
	/** configuracoes **/
	background_color: 		"#111111",
	background_opacity: 	50,
	
	
	
	
	
	/**
	* construtor
	*/
	construct:function(id, close_background, show_background, fade_in, fade_out){
		this.id 				= ( id!=null ? id : null );
		this.show_background 	= ( show_background==null ? true : show_background );
		this.close_background 	= ( close_background==null ? true : close_background );
		this.fade_in			= ( fade_in==null ? false : fade_in );
		this.fade_out			= ( fade_out==null ? false : fade_out );
	},
	
	
	
	
	/**
	* seta as variaveis necessarias para executar o lightbox
	*/
	setParams:function(hide_selects){
		
		/** ponteiro para $obj **/
		if( this.id!=null )
			this.div = document.getElementById(this.id);
			
		/** verifica se o background ja foi criado, se nao cria **/
		if(this.show_background){
			if( this.background==null )
				this.createBackground();
			this.setParamsBackground();
		}
		
		/** inicializa os parametros da div **/
		this.setParamsDiv();
		
		if( hide_selects )
			this.hideSelects();
		this.hideFlashs();
		
		document.body.style.overflowX = "hidden";
	},
	
	
	
	
	/**
	* cria uma div dentro de uma tag body
	*/
	createBackground:function(){
		
		var bg = document.getElementById("__background");
		
		if( bg==null ){
			var new_bg 	= document.createElement("div");
			var body	= document.getElementsByTagName("body")[0];
			if( body!=null ){
				new_bg.setAttribute("id", "__background");
				
				this.background = new_bg;
				
				body.appendChild(this.background);
			//	body.setAttribute("onresize", this.id+".setParamsBackground();");
				
				
			}
		}
		else
			this.background = bg;
		
		/** verifica se e pra fechar o lightbox quando clicar no bg **/
		if( this.close_background )
			this.background.onclick = new Function(this.id+".close()");
	},
	
	
	
	
	/**
	* altera o style do __background para "block"
	*/
	showBackground:function(){
		
		this.background.style.display = "block";
		
		if( this.fade_in )
			this.backgroundFadeIn();
	
	},
	
	

	
	/**
	* altera o style do __background para "none"
	*/
	hideBackground:function(){
			
		if( this.fade_out )
			this.backgroundFadeOut();
		else {
			this.background.style.display = "none";
			document.body.style.overflowX = "hidden";
		}
	},
	
	
	
	
	/**
	* altera a opacidade do background com efeito de fade IN
	*/
	backgroundFadeIn:function(){
		
		var alpha 		= 0;
		var opacity 	= this.background_opacity;
		var background 	= this.background;
		
		background.style.filter		= "alpha(opacity=0)";	
		background.style.opacity	= ".0";
			
		var f = setInterval( function() {
			if( alpha>=opacity ){
				clearInterval(f);
				background.style.filter = "alpha(opacity="+ this.background_opacity; +")";
 				background.style.opacity = this.background_opacity/100;
 				document.body.style.overflowX = "hidden";
			}
			
			background.style.filter = "alpha(opacity="+ alpha +")";
 			background.style.opacity = alpha/100;
			alpha += 4;
		}, this.vel_fade_in );
		
	},
	
	
	
	
	/**
	* altera a opacidade do background com efeito de fade OUT
	*/
	backgroundFadeOut:function(){
		
		var alpha 		= this.background_opacity;
		var background 	= this.background;
 				
		var f = setInterval( function() {
			if( alpha<=0 ){
				clearInterval(f);
				background.style.filter = "alpha(opacity=0)";
 				background.style.opacity = 0;
				background.style.display = "none";
			}
			background.style.filter = "alpha(opacity="+ alpha +")";
 			background.style.opacity = alpha/100;
			alpha -= 4;
		}, this.vel_fade_out );
		
		
	},
	
	
	
	
	/**
	* altera o style da div para "block"
	*/
	showDiv:function(){
		this.div.style.display = "block";
	},
	
	
	
	
	/**
	* altera o style da div para "none"
	*/
	hideDiv:function(){
		this.div.style.display = "none";
	},
	
	
	
	
	/**
	* seta os parametros para os styles do __background
	*/
	setParamsBackground:function(){
		
		this.background.style.position 		= "absolute";
	//	this.background.style.width 		= this.getWidth()+"px";
		this.background.style.width 		= "100%";
		this.background.style.height 		= this.getHeight()+"px";
		this.background.style.top 			= "0px";
		this.background.style.left 			= "0px";
		this.background.style.right 		= "0px";
		this.background.style.bottom		= "0px";
		this.background.style.background	= this.background_color;
		this.background.style.textalign		= "center";	
		
		if( !this.fade_in ) {
			this.background.style.filter	= "alpha(opacity=" + this.background_opacity + ")";	
			this.background.style.opacity	= "." + this.background_opacity;
		}
		
	},
	
	
	
	
	/**
	* seta os parametros pra os styles da div
	*/
	setParamsDiv:function(){
		
		this.div.style.position		= "absolute";
		this.div.style.overflowX	= "auto";
		this.div.style.overflowY	= "auto";
		this.div.style.zIndex		= "1003";
		this.div.style.padding		= "10px";
		this.div.style.background	= "#ffffff";
		this.div.style.left			= "50%";
		this.div.style.top			= "50%";
		this.div.style.border		= "4px solid #cccccc";
			
		/** posiciona a div no centro da tela **/
		//this.setDivCenterWidth();
		//this.setDivCenterHeight();
		
	},
	
	
	
	setDivCenterWidth:function(){
		var left = parseInt("-" + (this.div.offsetWidth/2) );
		this.div.style.marginLeft = left + "px";
	},
	
	
	
	setDivCenterHeight:function(){
		var top = 0;
		
		top = parseInt("-" + (this.div.offsetHeight/2) );
		top += document.body.scrollTop;
		top	+= document.documentElement.scrollTop;

		this.div.style.marginTop = top + "px";
	},
	
	
	
	/**
	* mostra o lightbox com os parametros definidios
	*/
	show:function(hide_selects){
		
		hide_selects = ( hide_selects == null ? true : hide_selects );
		
		this.setParams(hide_selects);
		this.showDiv();
		this.showBackground();
		this.setParamsDiv();
		this.setDivCenterHeight();
		this.setDivCenterWidth();
		
	},
	
	
	
	
	/**
	* fecha o lightbox
	*/
	close:function(manter_background){
		
		manter_background = ( manter_background==null ? false : manter_background );
		
		if( !manter_background ){
			this.hideBackground();
			this.showSelects();
			this.showFlashs();
		}
		this.hideDiv();
		
	},
	
	
	
	
	/* esconde os selects -- IE bug -- */
	hideSelects:function(){
		var selects = document.getElementsByTagName("select");
		for( var i=0; i<selects.length; i++ ){
			selects.item(i).style.visibility = "hidden";
		}
	},
	
	
	
	
	/* mostra os selects novamente -- IE bug -- */
	showSelects:function(){
		var selects = document.getElementsByTagName("select");
		for( var i=0; i<selects.length; i++ ){
			selects.item(i).style.visibility = "";
		}
	},
	
	
	
	
	/* esconde os falashes -- IE bug -- */
	hideFlashs:function(){
		
		var flashs  = document.getElementsByTagName("object");
		var _flashs = document.getElementsByTagName("embed");
		
		for( var i=0; i<flashs.length; i++ ){
			flashs.item(i).style.visibility = "hidden";
		}
		for( var i=0; i<_flashs.length; i++ ){
			_flashs.item(i).style.visibility = "hidden";
		}
	},
	
	
	
	
	/* mostra os selects novamente -- IE bug -- */
	showFlashs:function(){
		
		var flashs  = document.getElementsByTagName("object");
		var _flashs = document.getElementsByTagName("embed");
		
		for( var i=0; i<flashs.length; i++ ){
			flashs.item(i).style.visibility = "";
		}
		for( var i=0; i<_flashs.length; i++ ){
			_flashs.item(i).style.visibility = "";
		}
	},
	
	
	
	
	/**
	* retorna o tamanho (height) 'utilizavel' do navegador considerando a barra de rolagem
	*/
	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 < document.body.clientHeight ) )
			y = window.screen.height;
	
		if( document.getElementsByTagName("html")[0].offsetHeight && ( y < document.getElementsByTagName("html")[0].offsetHeight ))
			y = document.getElementsByTagName("html")[0].offsetHeight;
		
		
		y += document.body.scrollTop;
		
		/*alert(
				"window.innerHeight: " + window.innerHeight + "\n" +
				"document.documentElement.clientHeight: " + document.documentElement.clientHeight + "\n" +
				"document.body.clientHeight: " + document.body.clientHeight + "\n" +
				"window.screen.height: " + window.screen.height + "\n\n" +
				"window.pageYOffset: " + window.pageYOffset + "\n" +
				"document.documentElement.scrollTop: " + document.documentElement.scrollTop + "\n" +
				"document.body.scrollTop: " + document.body.scrollTop + "\n" +
				"outherHeight: " + window.outherHeight  + "\n"
			);
		*/
		
		if(document.getElementById("site")){
			return(document.getElementById("header").offsetHeight + document.getElementById("content").offsetHeight + document.getElementById("footer").offsetHeight + 20);
		}
		else
			return y;
		
	},
	
	
	
	
	/**
	* retorna o tamanho (height) 'utilizavel' do navegador considerando a barra de rolagem
	*/
	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;
	}
}
