var http_request = false;
var isOracoesInicial = false;
var xmlDoc;
var xmlOracoes;
var totalDestaques = new Number();
var gstrPathImagem = "img/destaquesHome/";
var indiceXML = 0;
var destAtual_01;
var destAtual_02;

function iniciarImagensHome(i)
{
	//mostrarCarregando();
	isOracoesInicial = true;
	makeRequest('oracoes.xml', false);
}

function avancarImagemHome()
{
	indiceXML = (indiceXML + 1 == totalDestaques) ? 0 : indiceXML + 1;
	indiceXML = (indiceXML == destAtual_02) ? indiceXML + 1 : indiceXML;
	carregardestaques(indiceXML);
}

function retornaImagemHome()
{
	indiceXML = (indiceXML - 1 < 0) ? totalDestaques - 1 : indiceXML - 1;
	indiceXML = (indiceXML == destAtual_01) ? indiceXML - 1 : indiceXML;
	carregardestaques(indiceXML, 'left');
}


function makeRequest(url, parameters)
{
	http_request = false;

	if (window.XMLHttpRequest) 
	{ // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} 
	else if (window.ActiveXObject) 
	{ // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) 
	{
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = carregarChamadas;
	http_request.open('GET', url, true);
	if(!parameters)
	{
		parameters = null;
	}
	http_request.send(parameters);
}

function carregarChamadas() 
{
	if (http_request.readyState == 4) 
	{
		if (http_request.status == 200) 
		{
			xmlDoc = http_request.responseXML.documentElement;
			xmlOracoes = xmlDoc.getElementsByTagName('destaque');
			totalDestaques = xmlOracoes.length;
			//alert(totalDestaques);
			carregardestaques(indiceXML);
			if(isOracoesInicial)
			{
				indiceXML++;
				carregardestaques(indiceXML);
				isOracoesInicial = false;
			}
		}
		else 
		{
			alert('Ocorreu um erro com o request !');
		}
		//ocultarCarregando();
	}

}

function carregardestaques(lobjIndice, direction)
{
	var objImagem01 	= document.getElementById('imagem01');
	var objHref01 		= document.getElementById('href01');
	var objTitulo01 	= document.getElementById('titulo01');
	var objTexto01 		= document.getElementById('texto01');

	var objImagem02 	= document.getElementById('imagem02');
	var objHref02 		= document.getElementById('href02');
	var objTitulo02 	= document.getElementById('titulo02');
	var objTexto02 		= document.getElementById('texto02');
	
	if(!direction || direction == 'right' )
	{
		/* CASO NÃO INFORME A DIREÇÃO ELE DEFINE COMO PADRÃO A DIREITA */
		destAtual_01 = (destAtual_02 == null) ? 0 : destAtual_02;
		destAtual_02 = lobjIndice;
		objImagem01.src 		= objImagem02.src;
		objTitulo01.innerHTML 	= objTitulo02.innerHTML;
		objTitulo01.href		= objTitulo02.href;
		objTexto01.innerHTML 	= objTexto02.innerHTML;
		objTexto01.href			= objTexto02.href;
	
		objImagem02.src 		= gstrPathImagem + getNodeValue(xmlOracoes[lobjIndice],'imagem');
		
		objTitulo02.innerHTML 	= getNodeValue(xmlOracoes[lobjIndice],'titulo');
		objTitulo02.href		= getNodeValue(xmlOracoes[lobjIndice],'link');
		
		objTexto02.innerHTML 	= getNodeValue(xmlOracoes[lobjIndice],'texto');
		objTexto02.href			= getNodeValue(xmlOracoes[lobjIndice],'link');

	} else {
		
		/* CASO INFOMRE SE NÃO FOR RIGHT (DIREITA) POR PADRÃO SERÁ LEFT */
		destAtual_02 = (destAtual_01 == null) ? totalDestaques - 1 : destAtual_01;
		destAtual_01 = lobjIndice;

		objImagem02.src 		= objImagem01.src;
		objTitulo02.innerHTML 	= objTitulo01.innerHTML;
		objTitulo02.href		= objTitulo01.href;
		objTexto02.innerHTML 	= objTexto01.innerHTML;
		objTexto02.href			= objTexto01.href;
	
		objImagem01.src 		= gstrPathImagem + getNodeValue(xmlOracoes[lobjIndice],'imagem');
		
		objTitulo01.innerHTML 	= getNodeValue(xmlOracoes[lobjIndice],'titulo');
		objTitulo01.href		= getNodeValue(xmlOracoes[lobjIndice],'link');
		
		objTexto01.innerHTML 	= getNodeValue(xmlOracoes[lobjIndice],'texto');
		objTexto01.href			= getNodeValue(xmlOracoes[lobjIndice],'link');
	}
}

function getNodeValue(obj,tag)
{
	return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
}


function mostrarCarregando()
{
	var objdestaque01 = document.getElementById('destaque01');
	var objdestaque02 = document.getElementById('destaque02');
	var objloading = document.getElementById('loading');

	objdestaque01.style.display = 'none';
	objdestaque02.style.display = 'none';
	objloading.style.display = '';
}

function ocultarCarregando()
{
	var objdestaque01 = document.getElementById('destaque01');
	var objdestaque02 = document.getElementById('destaque02');
	var objloading = document.getElementById('loading');

	objdestaque01.style.display = '';
	objdestaque02.style.display = '';
	objloading.style.display = 'none';
}
