//SCRIPTS GERAIS

//==========================================================================================
// AJAX

// Verifica qual HttpRequest deve ser acionado e cria o objeto
var xmlTempo = "";

function GetXmlHttpObject() {
	
	var objXMLHttp = null
	
	if (window.XMLHttpRequest) {
		objXMLHttp = new XMLHttpRequest()
	}
	else if (window.ActiveXObject) {
		objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP")
	}
	
	return objXMLHttp

}

//Busca informações no servidor
function findTempo(cidade) {
	
	xmlTempo = GetXmlHttpObject();

	var urlFinal = "includes/engines/ajaxTempo.asp?Cidade=" + cidade
	
	xmlTempo.onreadystatechange = showTempo;
	xmlTempo.open("GET",urlFinal,true)
	xmlTempo.send(null)

}

//==========================================================================================
// previsão do tempo

var timeTempo = 0;

function loadTempo() { 
	
	var detalhes = document.getElementById("barra").getElementsByTagName("ul")[0];
	if (detalhes.style.display != "block") { findTempo(); }
	
	if(timeTempo == 0) { timeTempo = window.setInterval("loadTempo()", 8000); }

}
	
//onload
//(navigator.appName.search("Microsoft") == -1) ? window.addEventListener("load", loadTempo, false) : window.attachEvent("onload", loadTempo) ;


function showTempo() {
		
	var target = document.getElementById("barra").getElementsByTagName("dl")[0];
	
	if (xmlTempo.readyState == 4 || xmlTempo.readyState == "complete") {
		
		var response = xmlTempo.responseXML.documentElement;
		
		if (response != null) {

			var nome = response.getElementsByTagName('nome')[0].firstChild.nodeValue;
			var tempo = response.getElementsByTagName('tempo')[0].firstChild.nodeValue;
			var maxima = response.getElementsByTagName('maxima')[0].firstChild.nodeValue;
			var minima = response.getElementsByTagName('minima')[0].firstChild.nodeValue;
	
			target.getElementsByTagName("dt")[0].innerHTML = nome;
			target.getElementsByTagName("dd")[0].innerHTML = minima + "º | " + maxima + "º";
			target.getElementsByTagName("dd")[0].style.backgroundImage = "url(../imagens/geral/engine/tempo/" + tempo + ".gif)";
			
			//---------------------------------------------------------------------------------//
			//detalhes
			
			var detalhes = document.getElementById("barra").getElementsByTagName("ul")[0];
			var previsao = response.getElementsByTagName('previsao');
			
			if (detalhes.hasChildNodes()) {	for (var j = 0; j < 3; j++) { detalhes.removeChild(detalhes.firstChild) } }
			
			for (var i = 1; i < previsao.length; i++) {
	
				var infoDia = previsao[i].getElementsByTagName('dia')[0].firstChild.nodeValue;
				var infoImg = previsao[i].getElementsByTagName('tempo')[0].firstChild.nodeValue;
				var infoMin = previsao[i].getElementsByTagName('minima')[0].firstChild.nodeValue;
				var infoMax = previsao[i].getElementsByTagName('maxima')[0].firstChild.nodeValue;
				
			
				var novoStrong = document.createElement('strong');
					novoStrong.appendChild(document.createTextNode(infoDia))
				
				var novoLi = document.createElement('li');
					novoLi.appendChild(novoStrong)
					novoLi.appendChild(document.createTextNode(infoMin + "º | " + infoMax + "º"))
					novoLi.style.backgroundImage = "url(../imagens/geral/engine/tempo/" + infoImg + ".gif)"; 
					
				detalhes.appendChild(novoLi);
				
			}
			
			//---------------------------------------------------------------------------------//
		
		}
		else { loadTempo(); }
		
	}

}

function tempoDetalhe() { 

	var detalhes = document.getElementById("barra").getElementsByTagName("ul")[0];
	
	detalhes.style.display = (detalhes.style.display != "block") ? "block" : "none" ;
	
}

//==========================================================================================
// emissoras

function emissora(quem) {
	
	var checkId = quem.value;
	
	document.location.href = "emissora.asp?Id=" + checkId;
	
}

//==========================================================================================
// destaque

function destaqueOn() {
	
	var target = document.getElementById("swf");
	target.style.height = "140px";
	
}

function destaqueOff() {
	
	var target = document.getElementById("swf");
	target.style.height = "104px";
	
}

//==========================================================================================
// Busca

function buscaIn() {

	var target = document.getElementById("busca");
	
	if (target.value == "Busca no portal") { target.value = "" }
	
}

function busca() {
	
	var target = document.getElementById("busca");
	
	if (target.value != "" && target.value != "Busca no portal") { document.location.href = "busca.asp?Palavra=" + target.value; }
	
}


