/*
	Sistema de leitura de notícias para o site da HOTLink
	------------------------------------------------------

	Autor		: Eduardo Bello
	Data		: 20/03/2006
	Descrição	: Lê os arquivos XML de notícias, formatando e exibindo-os na div correspondente.


	Log de atualizações:
	-------------------

	Data		-	Autor				-	Descrição
	----------		-------------------		------------------------------------------------------

*/



// Declaração das variáveis do script de notícias	
var ntSecao 		= "esportes";

var ntEstado   		= "PE";
var ntConteudo 		= "";
var ntXMLObj		= null;
var ntVetSecao		= new Array();
var ntTmpCount		= 0;
var ntTotalFiles 	= 0;
var ntProsseguir    = false;
var ntXMLType = 0;



// Vetores dos arquivos XML de notícias
ntVetPE	= new Array();
ntVetPE["gerais"] 	= new Array("JC Online#noticias_pe_gerais_jc.xml", "Diario de Pernambuco#noticias_pe_gerais_dp.xml","Folha de Pernambuco#noticias_pe_gerais_fp.xml", "Agência Estado#noticias_br_gerais_ag.xml");
ntVetPE["esportes"] = new Array("Diario de Pernambuco#noticias_pe_esportes_dp.xml", "Folha de Pernambuco#noticias_pe_esportes_fp.xml");
ntVetPE["economia"] = new Array("Folha de Pernambuco#noticias_pe_economia_fp.xml", "Agência Estado#noticias_br_economia_ag.xml");
ntVetPE["tecnologia"] = new Array("Folha de Pernambuco#noticias_pe_tecnologia_fp.xml", "Agência Estado#noticias_br_tecnologia_ag.xml");

/*
ntVetPE["esportes"] = new Array("Diario de Pernambuco#noticias_pe_esportes_dp.xml");
ntVetPE["economia"] = new Array("Agência Estado#noticias_br_economia_ag.xml");
ntVetPE["tecnologia"] = new Array("Agência Estado#noticias_br_tecnologia_ag.xml");
*/

ntVetPB	= new Array();
ntVetPB["gerais"] 	= new Array("Correio da Paraíba#noticias_pb_gerais_cpb.xml", "Agência Estado#noticias_br_gerais_ag.xml", "Reuters#noticias_br_gerais_reuters.xml", "BBC Brasil#noticias_br_gerais_bbc.xml");
ntVetPB["esportes"] = new Array("Correio da Paraíba#noticias_pb_esportes_cpb.xml", "Agência Estado#noticias_br_esportes_ag.xml");
ntVetPB["economia"] = new Array("Correio da Paraíba#noticias_pb_economia_cpb.xml", "Agência Estado#noticias_br_economia_ag.xml");
ntVetPB["tecnologia"] = new Array("Agência Estado#noticias_br_tecnologia_ag.xml");
										
ntVetBA	= new Array();
ntVetBA["gerais"] 	= new Array("A Tarde#noticias_ba_gerais_at.xml", "Agência Estado#noticias_br_gerais_ag.xml", "Reuters#noticias_br_gerais_reuters.xml", "BBC Brasil#noticias_br_gerais_bbc.xml");
ntVetBA["esportes"] = new Array("Agência Estado#noticias_br_esportes_ag.xml");
ntVetBA["economia"] = new Array("Agência Estado#noticias_br_economia_ag.xml");
ntVetBA["tecnologia"] = new Array("Agência Estado#noticias_br_tecnologia_ag.xml");

ntVetRN	= new Array();
ntVetRN["gerais"] 	= new Array("Diário de Natal#noticias_rn_gerais_dn.xml", "Agência Estado#noticias_br_gerais_ag.xml", "Reuters#noticias_br_gerais_reuters.xml", "BBC Brasil#noticias_br_gerais_bbc.xml");
ntVetRN["esportes"] = new Array("Tribuna do Norte#noticias_rn_esportes_dn.xml", "Agência Estado#noticias_br_esportes_ag.xml");
ntVetRN["economia"] = new Array("Agência Estado#noticias_br_economia_ag.xml");
ntVetRN["tecnologia"] = new Array("Agência Estado#noticias_br_tecnologia_ag.xml");

ntVetSE	= new Array();
ntVetSE["gerais"] 	= new Array("Agência Sergipe de Notícias#noticias_se_gerais_agsergipe.xml", "Agência Estado#noticias_br_gerais_ag.xml", "Reuters#noticias_br_gerais_reuters.xml");
ntVetSE["esportes"] = new Array("Agência Estado#noticias_br_esportes_ag.xml");
ntVetSE["economia"] = new Array("Agência Estado#noticias_br_economia_ag.xml");
ntVetSE["tecnologia"] = new Array("Agência Estado#noticias_br_tecnologia_ag.xml");

ntVetMA	= new Array();
ntVetMA["gerais"] 	= new Array("Imirantes#noticias_ma_gerais_im.xml", "O Imparcial#noticias_ma_gerais_oi.xml", "Agência Estado#noticias_br_gerais_ag.xml", "Reuters#noticias_br_gerais_reuters.xml");
ntVetMA["esportes"] = new Array("Agência Estado#noticias_br_esportes_ag.xml");
ntVetMA["economia"] = new Array("Agência Estado#noticias_br_economia_ag.xml");
ntVetMA["tecnologia"] = new Array("Agência Estado#noticias_br_tecnologia_ag.xml");

									
ntMainVet = new Array();
ntMainVet["PE"] = ntVetPE;
ntMainVet["PB"] = ntVetPB;
ntMainVet["BA"] = ntVetBA;
ntMainVet["RN"] = ntVetRN;
ntMainVet["SE"] = ntVetSE;
ntMainVet["MA"] = ntVetMA;



// Função para inicializar o objeto XML (var ntXMLObj)
function ntStartXML() {

	if (ntXMLObj==null) {
		try{ 
			ntXMLObj = new ActiveXObject("Msxml2.DOMDocument.4.0");
			ntXMLType = 6;
		} catch(e){}	
	}
	
	if (ntXMLObj==null) {
		try{ 
			ntXMLObj = new ActiveXObject("Msxml2.DOMDocument.3.0");
			ntXMLType = 2;
		} catch(e){}	
	}
	
	if (ntXMLObj==null) {	
		try{
			ntXMLObj = new XMLHttpRequest();
			ntXMLType = 4;
		}catch(e){}
	};
	
	if (ntXMLObj==null) {
		try { 
			ntXMLObj = new ActiveXObject("Msxml2.XMLHTTP");
			ntXMLType = 1;
		} catch(e){}
	};
	
	if (ntXMLObj==null) {	
		try{
			ntXMLObj = new ActiveXObject("Microsoft.XMLHTTP");
			ntXMLType = 3;
		}catch(e){}
	}
	
	if (ntXMLObj==null) {
		try{ 
			ntXMLObj = new ActiveXObject("MSXML.DOMDocument");
			ntXMLType = 5;
		} catch(e){}	
	}
	
};		

// Função para mudar a seção das notícias
function ntMudaSecao(novaSecao) {
	if (novaSecao != ntSecao) {
		var newSec = document.getElementById("ntImg"+novaSecao);
		var oldSec = document.getElementById("ntImg"+ntSecao);
		
		newSec.src = "imgs/tabsnoticias/dest-"+novaSecao+".gif";
		oldSec.src = "imgs/tabsnoticias/"+ntSecao+".gif";
		
		ntSecao = novaSecao;
		ntMostraNoticias(ntEstado, ntSecao);
	};
};

// Função para mudar o Estado das notícias
function ntMudaEstado(novoEstado) {
	if (novoEstado != ntEstado) {
		var newEst = document.getElementById("ntTd"+novoEstado);
		var oldEst = document.getElementById("ntTd"+ntEstado);
		
		newEst.className 	= "noticiadestaq";
		oldEst.className	= "";
		
		ntEstado = novoEstado;
		
		ntMostraNoticias(ntEstado, ntSecao);
	};

};

// Organiza e mostra as notícias na div correspondente
function ntMostraNoticias(est, sec) {		
	ntConteudo = "";		
	ntVetSecao = ntMainVet[est][sec];
	if ( (ntXMLType==1) || (ntXMLType==3) || (ntXMLType==4) ) { // XMLHttpRequest
		ntTmpCount = 0; // Contador de Fontes
		
		ntTmpVet   = ntVetSecao[ntTmpCount].split('#');  // Divide o elemento em 2 usando o separador #
		ntLerXML_HttpRequest(ntTmpVet[1]); // ntTmpVet[0]->Descrição da fonte , ntTmpVet[1]->Nome do arquivo XML
	} else { // Msxml DOMDocument (IE)
		for (var ntTmpCount=0; ntTmpCount<ntVetSecao.length; ntTmpCount++) {
			ntXMLObj.async = false;
			ntTmpVet   = ntVetSecao[ntTmpCount].split('#'); // ntTmpVet[0]->Descrição da fonte , ntTmpVet[1]->Nome do arquivo XML
			ntXMLObj.load("includes/"+ntTmpVet[1]); // Lê o arquivo XML
			
			if (ntXMLObj.parseError.errorCode != 0) {
			   var myErr = ntXMLObj.parseError;
			   //alert("Erro lendo XML (" + ntTmpVet[1] + "): " + myErr.reason);
			   break;
			} else {
				// Adiciona o conteúdo
				ntConteudo += "<span class='tt-jornal'>"+ ntTmpVet[0] +"</span><br>";
				ntConteudo += ntProcessaXML(ntXMLObj.getElementsByTagName("noticias"));
				ntConteudo += "<hr>";
			};	
		};		
		document.getElementById("ntDiv").innerHTML = ntConteudo;
	};
	
};


// Função que ativa a leitura do XML para browers que utilizam XMLHttpRequest
function ntLerXML_HttpRequest(arqXML) {				
	ntStartXML();
	ntXMLObj.onreadystatechange = ntStateHttpRequest;	
	ntXMLObj.open ('GET', "includes/"+arqXML, true);
	ntXMLObj.send ('');		
};

// Função apenas para browers que utilizam XMLHttpRequest (leia-se todos menos os IE 6-)
function ntStateHttpRequest() {
	if(ntXMLObj.readyState == 4){ // Full Loaded
		if ( (ntXMLObj.status==200) || (ntXMLObj.status==0)) { // Sucesso
			// ntConteudo deve ser zerado antes da leitura das notícias

			ntTmpVet 	= ntVetSecao[ntTmpCount].split('#'); // ntTmpVet[0]->Descrição da fonte , ntTmpVet[1]->Nome do arquivo XML
			
			ntConteudo += "<span class='tt-jornal'>"+ ntTmpVet[0] +"</span><br>";
			conreturn = ntProcessaXML(ntXMLObj.responseXML.getElementsByTagName("noticias"));
			if (conreturn=="") {
				conreturn = ntProcessaXML(ntXMLObj.getElementsByTagName("noticias"));
			}
			ntConteudo += conreturn; // Lê e formata a tag XML
			ntConteudo += "<hr>";
			ntTmpCount++;
			if (ntTmpCount < ntVetSecao.length) { // Se ainda tiver fonte a ser lida no vetor global ntVetSecao
				ntTmpVet 	= ntVetSecao[ntTmpCount].split('#');
				ntLerXML_HttpRequest(ntTmpVet[1]); // Recursividade indireta.	
			} else {
				document.getElementById("ntDiv").innerHTML = ntConteudo; // Atribui-se o conteúdo total lido
				ntTmpCount = 0;
			};
			

		} else {
			alert("Não leu o XML com sucesso ("+ntXMLObj.status+")");
		};
	};		
};

// Função que processa o XML e formata os links das notícias
function ntProcessaXML(obj){
	var vetNoticias, qtdNoticias;
	var retCont = "";
	vetNoticias = obj[0].getElementsByTagName('noticia');
	qtdNoticias = vetNoticias.length;
	for (var nn=0; nn<qtdNoticias; nn++) {
			try {
				retCont += vetNoticias[nn].getElementsByTagName('hora')[0].firstChild.data;
			}  catch(e){};
			retCont += ' - ';				
			retCont += '<a href="' + vetNoticias[nn].getElementsByTagName('url')[0].firstChild.nodeValue + '" target=_blank>';
			try {
				retCont += vetNoticias[nn].getElementsByTagName('titulo')[0].firstChild.nodeValue + "</a><br>";
			} catch(e) { };
	};
	return retCont;
	
};	

ntStartXML();
