// JavaScript Document - Lightbox
// ------------------------------------------------- //
// |||||||| DESENVOLVIDO POR KLAYTON FARIA ||||||||| //
// ------------------------------------------------- //
/* Detalhamento: 
- box (Dá o start)
- add (Cria elemento div e come�a a m�gica)
- abrir  (Faz efeito de entrada do box e insere o player)
- limpar  (Limpa o box antes de fecha-lo)
- fechar (Faz efeito de fechar box e logo em seguida remove o elemento div onde rolou toda a m�gica)
- enviar (Enviar oferta)
- detalhes (Ir para detalhe do produto)
- opacidade(opacity, id) Fade in e Fade Out
*/

// Vari�veis para inserção e parametrização do player
var objPlayer = null;
var conteudo_divPlayer = null;
var titulo = null;
var prod = null;
var trailer_url = null;

// Vari�veis essenciais para constru��o do box  
var tempo_animacao_in = 0;
var tempo_animacao_out = 0;
var lagura_box = 480;
var div_corpo = null;
var largura = 0;
var altura = 0;
var velocidade = 0;

//  Funções
function box(pro, descr, trailer) {
	titulo = descr;
	prod = pro;
	trailer_url = trailer;
	
	var busca_div = null;
	busca_div = document.getElementById('corpo');
	if (busca_div != null) {
		void(0);
	} else {
		add();
	}

}

function inserePlayer() {
 
 objPlayer =  '<form id="form1" runat="server" style="height:100%;"><div id="silverlightControlHost">';
 objPlayer += '<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="98%" height="380">';
 objPlayer += '<param name="source" value="/videos/playerSaraiva.xap"/>';
 objPlayer += '<param name="onerror" value="onSilverlightError" />';
 objPlayer += '<param name="background" value="transparent" />';
 objPlayer += '<param name="initParams" value="url='+ trailer_url +',urlImagem=null,autoPlay=play" />' 
 objPlayer += '<a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;"><img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/></a>';
 objPlayer += '</object>';
 objPlayer += '<iframe style="visibility:hidden;height:0;width:0;border:0px"></iframe></div></form>'; 
	conteudo_divPlayer = "<div id='div_titulo'></div><div id='player_detalhe'>" + objPlayer + "</div><div id='botoes'><div id='div_detalhes'><a href='javascript:detalhes()'><img src='/digital/img/detalhes.jpg' border='0' /></a></div><div id='div_envie'><a href='javascript:enviar()'><img src='/digital/img/envie.jpg' border='0' /></a></div></div>";
	document.getElementById('div_player').innerHTML = conteudo_divPlayer;
	document.getElementById('div_titulo').innerHTML = titulo;	
}

function add() {
	div_corpo = document.createElement('div');
	div_corpo.id = "corpo";
	div_corpo.className = "corpo";
	document.body.insertBefore(div_corpo, document.body.firstChild);	
	document.getElementById('corpo').style.visibility = "visible";		
	div_corpo.innerHTML = '<div id="cell"><div id="box" class="Box-corpo"><div id="conteudo" class="Box-conteudo"><div id="fecha" class="fecha"><a href="javascript:limpar();" class="box_link1">[X]</a></div><div id="div_player"></div></div></div></div>';
	abrir();	
}

function abrir() {
	if (largura <= lagura_box) {
		if (largura < 0) {
			largura = 0;
			altura = 0;
		}
		document.getElementById('box').style.visibility = "visible";
		document.getElementById('box').style.width = largura + "px";
		document.getElementById('box').style.height = altura + "px";
		largura = largura + 60;
		altura = altura + 63;
		velocidade = setTimeout("abrir()", tempo_animacao_in);		
	} else {
		clearTimeout(velocidade);		
		document.getElementById('fecha').style.visibility = "visible";
		inserePlayer();
	};
}

function limpar() {
	document.getElementById("div_player").innerHTML = "";
	stoFechar = setTimeout("fechar()", 60);
}

function fechar() {
	clearTimeout(stoFechar);
	document.getElementById('fecha').style.visibility = "hidden";	
	if (largura >= 0) {
		document.getElementById('box').style.width = largura + "px";
		document.getElementById('box').style.height = altura + "px";
		largura = largura - 120;
		altura = altura - 126;
		velocidade = setTimeout("fechar()", tempo_animacao_out);
		if (largura <= 60) {			
			document.getElementById('box').style.visibility = "hidden";			
		}
	} else {		
		clearTimeout(velocidade)
		document.body.removeChild(document.getElementById('corpo')); // Removendo elemento div Principal... 'Corpo'		 
	};
}

function enviar() {
	window.open("/produto/produto.dll/oferta?pro_id=" + prod, "oferta", "width=460,height=520,scrollbars");
}
function detalhes() {
	window.open("/produto/produto.dll/detalhe?pro_id=" + prod, "_self");
}
