var linkURL = "";
var telaAtiva = "";
var GLOBAL_carregou = 0;

// Função chamada no window.onload da página que chama esse script
function abrirHomeLojaCom() {
	verificarLembrete();

}

// FUNCOES DO SISTEMA DE LEMBRETE ##################################################### //
var GLOBAL_lembreteQuantidade = 0; // Recebe a quantidade de lembrete novos existentes
var GLOBAL_verificandoLembrete = 0; // Armazena a informação de verificação de Lembrete, para não haver repetições de consulta
var TloopVerificarLembrete = 0;
var TloopPiscarLembrete = 0;
var TloopAbrirLembreteTabela = 0;
var abrirLembreteTabelaHeight = 0; // Altura atual da tabela
var abrirLembreteTabelaCont = 0; // Armazena a informação de que a tabela está abrindo ou fechando
var GLOBAL_lembreteBackgroundNum = 0; // Armazena o numero da imagem de fundo
var GLOBAL_alturaNavegador = 0; // Recebe a altura do navegador, para saber qual o tamanho da tabela

function verificarLembrete() {
	if (top.GLOBAL_verificandoLembrete == 0) {
		try {
			document.getElementById("iconeLembreteOff").style.display = "block";

		} catch(e) {}

		try {
			if (document.getElementById("iconeLembreteOff").style.display == "block") {
				top.GLOBAL_verificandoLembrete = 1;
				verificarLembreteLoop();

			}

		} catch(e) {}

	}

}

function ajaxResponse(funcaoResposta, retorno) {
	eval(funcaoResposta + "(retorno)");

}

function verificarLembreteFinal(resposta) {
	tratarLembrete(retorno);

	clearTimeout(TloopVerificarLembrete);

	TloopVerificarLembrete = setTimeout("verificarLembreteLoop()", 60000);

}

function tratarLembrete(retorno) {
	var quantidadeLembrete = 0;
	var texto = "";

	if (retorno == "nada") {
		GLOBAL_lembreteQuantidade = 0;

	} else {
		if (retorno.substring(0, 2) == "ok") {
			quantidadeLembrete = retorno.substring(3, retorno.length);
			GLOBAL_lembreteQuantidade = quantidadeLembrete;

			clearTimeout(TloopPiscarLembrete); // Pára a piscagem e inicia novamente
			alterarIconeLembrete(); // Chama a função que faz piscar o icone de lembrete

			texto = "Você tem " + quantidadeLembrete;
			texto += " lembrete";
			if (quantidadeLembrete > 1) {
				texto += "s";

			}
			texto += ". Clique para ler.";

			try {
				top.document.getElementById("iconeLembreteOn").title = texto;
				top.document.getElementById("iconeLembreteOff").title = texto;

			} catch(e) {}

		}

	}

}

function alterarIconeLembrete() {
	if (GLOBAL_lembreteQuantidade > 0) {
		try {
			if (top.document.getElementById("iconeLembreteOn").style.display == "block") {
				top.document.getElementById("iconeLembreteOn").style.display = "none";
				top.document.getElementById("iconeLembreteOff").style.display = "block";

			} else {
				top.document.getElementById("iconeLembreteOff").style.display = "none";
				top.document.getElementById("iconeLembreteOn").style.display = "block";

			}

		} catch(e) {}

		TloopPiscarLembrete = setTimeout("alterarIconeLembrete()", 1000);

	} else {
		top.document.getElementById("iconeLembreteOn").style.display = "none";
		top.document.getElementById("iconeLembreteOff").style.display = "block";

		clearTimeout(TloopPiscarLembrete);

	}

}

function verificarLembreteLoop() {
	var linkURL = "/verificarLembrete/index.php?acao=verificarQuantidade&" + ajaxTime();
	var funcaoResposta = "verificarLembreteFinal";

	ajaxRequest(linkURL, funcaoResposta); // Envia a requisição

}

// ------------------------------------------------------------- //

function abrirLembrete() {
	if (GLOBAL_lembreteBackgroundNum == 0) {
		var linkURL = "/verificarLembrete/index.php?acao=getBackground&" + ajaxTime();
		var funcaoResposta = "abrirLembreteFim";

		ajaxRequest(linkURL, funcaoResposta); // Envia a requisição

	} else {
		abrirLembreteFim("ok-" + GLOBAL_lembreteBackgroundNum);

	}

}

function abrirLembreteFim(retorno) {
	var htmlConteudoB = conteudoBCarregando();
	var arSize = windowSize();
	var alturaNavegador = arSize[1];
	var navegador = navegadorNome();

	if (retorno == "nada") {
		GLOBAL_lembreteBackgroundNum = 3;

	} else {
		if (retorno.substring(0, 2) == "ok") {
			GLOBAL_lembreteBackgroundNum = retorno.substring(3, retorno.length);

		}

	}

	GLOBAL_alturaNavegador = alturaNavegador - 4;

	document.getElementById("iconeLembreteID").align = "right";
	document.getElementById("iconeLembreteID").innerHTML = "<table border=0 cellspacing=0 cellpadding=0 id=iconeLembreteInternoID height=" + GLOBAL_alturaNavegador + "><tr height='20'><td id=iconeLembreteConteudoAID valign=top></td></tr><tr><td valign=top id='iconeLembreteConteudoBID'>" + htmlConteudoB + "</td></tr></table>";
	document.getElementById("iconeLembreteID").style.display = "block";

	if (navegador == "chrome") {
		document.getElementById("iconeLembreteID").style.width = "268px";

	} else {
		document.getElementById("iconeLembreteID").style.width = "266px";

	}

	document.getElementById("iconeLembreteInternoID").style.width = "220px";
	document.getElementById("iconeLembreteInternoID").style.border = "#000000 1px dotted";

	if (GLOBAL_lembreteBackgroundNum == 0) {
		document.getElementById("iconeLembreteInternoID").style.backgroundImage = "url('/verificarLembrete/images/3.jpg')";

	} else {
		document.getElementById("iconeLembreteInternoID").style.backgroundImage = "url('/verificarLembrete/images/" + GLOBAL_lembreteBackgroundNum + ".jpg')";

	}

	document.getElementById("iconeLembreteInternoID").style.backgroundPosition = "bottom right";
	document.getElementById("iconeLembreteInternoID").style.backgroundRepeat = "no-repeat";

	html = montarConteudoLembreteTabelaInterna();
	document.getElementById("iconeLembreteConteudoAID").innerHTML = html;

	abrirLembreteTabelaGerenciamento();

}

function montarConteudoLembreteTabelaInterna() {
	var html = "";

	html += "<table border='0' cellspacing='0' cellpadding='2' width='100%'>";
	html += "	<tr>";
	html += "		<td>";
	html += "			<table border='0' cellspacing='1' cellpadding='2' width='100%'>";
	html += "				<tr>";

	for (i = 1; i < 10; i++) {
		html += "				<td style='border: #666666 1px solid' bgcolor='#FFFFFF' class='transparencia30' align='center'>";
		html += "					<a href='javascript:;' OnClick='setBackgroundLembreteTabela(" + i + ")'><b>" + i + "</b></a>";
		html += "				</td>";

	}

	html += "				</tr>";
	html += "			</table>";
	html += "		</td>";
	html += "		<td width='44'>";
	html += "		</td>";
	html += "	</tr>";
	html += "</table>";

	return html;

}

function conteudoBCarregando() {
	var html = "";

	html = "<BR><BR><BR><center><img src='/lojacom/lib/images/botao_carregando/4.gif'></center>";

	return html;

}

function getAnotacao() {
	var linkURL = "/verificarLembrete/index.php?acao=getAnotacao&" + ajaxTime();
	var funcaoResposta = "getAnotacaoFinal";

	ajaxRequest(linkURL, funcaoResposta); // Envia a requisição

}

function getAnotacaoFinal(resposta) {
	document.getElementById("iconeLembreteConteudoBID").innerHTML = resposta;

}

function setBackgroundLembreteTabela(num) {
	GLOBAL_lembreteBackgroundNum = num;
	document.getElementById("iconeLembreteInternoID").style.backgroundImage = "url('/verificarLembrete/images/" + num + ".jpg')";

	setBackground(num);

}

function abrirLembreteTabelaGerenciamento() {
	if (abrirLembreteTabelaCont >= 0) { // Se a tabela estiver fechando, não poderá ser alterado, somente se estiver abrindo, ou parada
		if (abrirLembreteTabelaHeight == 0) {
			abrirLembreteTabelaCont = +10;
			abrirLembreteTabelaLoop();

		} else {
			abrirLembreteTabelaCont = -10;
			abrirLembreteTabelaLoop();

		}

	}

}

function abrirLembreteTabelaLoop() {
	if ((abrirLembreteTabelaCont > 0 && abrirLembreteTabelaHeight < GLOBAL_alturaNavegador) || (abrirLembreteTabelaCont < 0 && abrirLembreteTabelaHeight > 0)) {

		abrirLembreteTabelaHeight = abrirLembreteTabelaHeight + abrirLembreteTabelaCont;
		document.getElementById("iconeLembreteInternoID").style.height = abrirLembreteTabelaHeight;

		TloopAbrirLembreteTabela = setTimeout("abrirLembreteTabelaLoop()", 0);

	} else {
		if (abrirLembreteTabelaCont < 0) {
			document.getElementById("iconeLembreteID").style.display = "none";

		} else {
			getAnotacao();

		}

		abrirLembreteTabelaCont = 0;
		clearTimeout(TloopAbrirLembreteTabela);

	}

}

function setBackground(num) {
	var linkURL = "/verificarLembrete/index.php?acao=setBackground&num=" + num + "&" + ajaxTime();
	var funcaoResposta = "setBackgroundFinal";

	ajaxRequest(linkURL, funcaoResposta); // Envia a requisição

}

function setBackgroundFinal(resposta) {
	// nao executa nada, pois é apenas função de resposta, e como o background já foi setado no banco, não precisa fazer nada aqui
}

function marcarLembrete(id) {
	var linkURL = "/verificarLembrete/index.php?acao=setStatus&id=" + id + "&" + ajaxTime();
	var funcaoResposta = "getAnotacao";

	var htmlConteudoB = conteudoBCarregando();
	document.getElementById("iconeLembreteConteudoBID").innerHTML = htmlConteudoB;

	ajaxRequest(linkURL, funcaoResposta); // Envia a requisição

}

// FUNCOES DE NAVEGAÇÃO ############################################################### //
// Essa função gerencia a altura dos Frames, entre outras coisas do Painel
function fecharCarregando(altura, estruturaScroll) {

	try { // Isso aqui funciona só no Painel
		desativarTransparencia();

	} catch(e) {}

	try {
		if (alturaFrame > 0) {
			altura = alturaFrame;
			alturaFrame = 0;

		}

	} catch(e) {}

	// Isso aqui altera a altura dos frames da página
	top.document.getElementById("framePainel").style.height = altura;

	if (estruturaScroll == 1) {
		try { // Isso aqui funciona só no Painel
			top.painel.estrutura.document.body.scroll = "yes";

		} catch(e) {}

	}

}


function moverTableConteudo() {
	try {
		document.getElementById("TD_conteudo_home").style.paddingTop = "20px";
		document.getElementById("TABLE_conteudo_flash").style.display = "none";
		document.getElementById("bonecoID").style.display = "none";	

	} catch(e) {
	}

}

function mostrarCarregando() {
	var alturaScroll = document.body.scrollTop;

	// Alterar posicão ------------------------------------------- //
	if (alturaScroll > 0) {
		document.getElementById("TABLE_carregando").style.top = alturaScroll + 10;

	} else {
		document.getElementById("TABLE_carregando").style.top = 10;

	}

	Tlargura = largura - 20;

	// Alterar altura do fundo ----------------------------------- //
	if(navigator.appName.indexOf("Internet Explorer") != -1) {
		document.getElementById("TABLE_fundo").style.width = Tlargura;

	} else if(navigator.appName == "Netscape") {
		document.getElementById("TABLE_fundo").style.width = Tlargura - 20;

	}
	document.getElementById("TABLE_fundo").style.height = altura + alturaScroll + 10;
	// ----------------------------------------------------------- //

	document.getElementById("TABLE_carregando").style.display = "block";
	document.getElementById("TABLE_fundo").style.display = "block";

}

function esconderCarregando() {
	document.getElementById("TABLE_carregando").style.display = "none";
	document.getElementById("TABLE_fundo").style.display = "none";

	document.body.scrollTop = 0;

}

function abrirUrlDireto(filename) { // É chamada na home, quando é passado um link com a variavel url, com a pagina que será aberta
	GLOBAL_carregou = 1;
	abrirUrl(filename);

}

function abrirUrl(filename) {
	if (GLOBAL_carregou == 1) {
		telaAtiva = filename;

		moverTableConteudo();

		d = data("t");

		if (filename.substring(0, 2) == "..") {
			posBarra = filename.lastIndexOf("/");
			filename = filename.substring(0, posBarra) + "/_" + filename.substring(posBarra + 1, filename.length);
			if (filename.indexOf("&") > -1 || filename.indexOf("?") > -1) {
				add = "&d=" + d;

			} else {
				add = "?d=" + d;

			}

			linkURL = "/lojacom/modulos/home/" + filename + "" + add;

		} else {
			linkURL = "/lojacom/modulos/home/_" + filename +  ".php?" + d;

		}

		ajaxGet(linkURL, "resposta");

		mostrarCarregando(); // Mostra a tabela de carregando
		abrirUrlLOOP();

	}

}

function abrirUrlLOOP() {
	if (respostaAjax["resposta"] == "") {
		Tloop = setTimeout("abrirUrlLOOP()", 100);

	} else {
		clearTimeout(Tloop);

		r = respostaAjax["resposta"];

		respostaAjax["verificando"] = 0;
		respostaAjax["resposta"] = "";

		abrirUrlResultado(r);

	}

}

function abrirUrlResultado(respostaAjax) {
	document.getElementById("conteudo").innerHTML = respostaAjax;

	if (telaAtiva == "quemsomos") {
		abrirQuemSomos();

	} else if (telaAtiva == "acesso") {
//		document.formLogin.login.focus();

	} else {
	}

	document.getElementById("logoID").style.display = "block";
	esconderCarregando(); // Esconde a tabela de carregando

	telaAtiva = "";

}

function esconderBarraRolagemGeral(checkbox) {
	if (checkbox.checked == true) {
		window.top.document.body.scroll = "no";
		window.top.document.body.style.overflow = "hidden";

	} else {
		window.top.document.body.scroll = "yes";
		window.top.document.body.style.overflow = "scroll";

	}

}

// #################################################################################### // 
// FUNÇÕES DE SUB-PAGINAS
// #################################################################################### // 


// HOME ############################################################################### // 
	function abrirQuemSomos() {
//		window.open("_quemsomos_grafico.php?acao=abrir", "grafico");

	}

	function mostrarGrafico() {
		document.getElementById("TR_carregando_grafico").style.display = "none";
		document.getElementById("TR_grafico").style.display = "block";

	}

// #################################################################################### // 


/*
		<table border="0" cellspacing="0" cellpadding="0" width="100%" height="100%">
			<tr>
				<td>
					<table border="0" align="center" width="80%">
						<tr height="200" bgcolor="#FFFFFF">
							<td colspan="3" align="center" style="border: #AAAAAA 1px dashed">
								<b class="fontFamily" style="font-size: 14pt"><u>Arquivo não encontrado</u></b>
							</td>
						</tr>
					</table>
				</td>
			</tr>
		</table>

*/

window.onload = abrirHomeLojaCom;