var xml = null;
var noticias = new Array();

function fnCarregaNoticias(pUF){
	xml = xmlLoader('/sys/xml/comunicados/destaques.xml');
	var noticiasXml = xml.getElementsByTagName('noticia');
	
	for(i=0; i<noticiasXml.length; i++){
		var id = noticiasXml[i].attributes.getNamedItem('id').value;
		var uf = noticiasXml[i].attributes.getNamedItem('uf').value;
		var regioes = uf.toLowerCase().split(',');
		
		if(uf != '' && !in_array(pUF.toLowerCase(),regioes)){
			continue;
		}
		var titulo = noticiasXml[i].getElementsByTagName('titulo')[0].childNodes[0].nodeValue;
		var chamada = noticiasXml[i].getElementsByTagName('chamada')[0].childNodes[0].nodeValue;
		var href = noticiasXml[i].getElementsByTagName('href')[0].childNodes[0].nodeValue;
		var target = (noticiasXml[i].getElementsByTagName('href')[0].attributes.getNamedItem('target') != null) ? noticiasXml[i].getElementsByTagName('href')[0].attributes.getNamedItem('target').value : '';
		var dataNode = noticiasXml[i].getElementsByTagName('data');
		var dia = dataNode[0].getElementsByTagName('dia')[0].childNodes[0].nodeValue;
		var mes = dataNode[0].getElementsByTagName('mes')[0].childNodes[0].nodeValue;
		var ano = dataNode[0].getElementsByTagName('ano')[0].childNodes[0].nodeValue;
		
		var noticia = new Noticia(id,titulo,chamada,'',dia,mes,ano,href,target,uf);
		noticias.push(noticia);
	}
}

function fnMontaComunicados(pag,mes,ano){
	$('#painel_comunicados *').remove();
	
	var contador = 0;
	for(i=0; i<noticias.length; i++){
		if(mes.toString() != '0' && mes.toString() != noticias[i].mes)
			continue;
			
		if(ano.toString() != '0' && ano.toString() != noticias[i].ano)
			continue;
			
		contador++;
	}
	
	var numeroDeNoticias = contador; //-- pega numero de noticias
	var noticiasPorPag = 5; //-- defini o numero de noticias por pagina
	var numeroDePag = Math.ceil(numeroDeNoticias / noticiasPorPag); //-- calcula o numero de paginas
	
	var pagFim = (pag * noticiasPorPag);
	var pagInicio = pagFim - noticiasPorPag;
	
	var cont = 0;
	for(i in noticias){
		
		if(mes.toString() != '0' && mes.toString() != noticias[i].mes)
			continue;
			
		if(ano.toString() != '0' && ano.toString() != noticias[i].ano)
			continue;
			
		if(cont < pagInicio || cont >= pagFim){
			cont++;
			continue;
		}
		
		var dl = document.createElement('dl');
		var dt = document.createElement('dt');
		var dd = document.createElement('dd');
		var span = document.createElement('span');
		var strong = document.createElement('strong');
		var a_dt = document.createElement('a');
		var a_dd = document.createElement('a');
		
		//-- Aplicando css nos elementos DOM
		dl.setAttribute('class','comunicados');
		dl.className = 'comunicados';
		span.setAttribute('class','data');
		span.className = 'data';
		
		var href = '';
		if(noticias[i].href != '')
			href = noticias[i].href;
		else
			href = '../portal/a-vivo-comunicados-comunicado.php?id=' + noticias[i].id + '&ano=' + noticias[i].ano;
			
		a_dt.setAttribute('href',href);
		a_dt.href = href;
		a_dd.setAttribute('href',href);
		a_dd.href = href;
		
		if(noticias[i].target != ''){
			a_dt.setAttribute('target',noticias[i].target);
			a_dt.target = noticias[i].target;
			a_dd.setAttribute('target',noticias[i].target);
			a_dd.target = noticias[i].target;
		}
		
		//-- Definindo a herança dos elementos
		dl.appendChild(span);
		dl.appendChild(dt);
		dl.appendChild(dd);
		dt.appendChild(a_dt);
		a_dt.appendChild(strong);
		dd.appendChild(a_dd);
	
		//-- Setando os valores das noticias
		span.innerHTML = noticias[i].dia + '/' + (noticias[i].mes) + '/' + noticias[i].ano;
		strong.innerHTML = noticias[i].titulo;
		a_dd.innerHTML = noticias[i].descricao;
		
		document.getElementById('painel_comunicados').appendChild(dl);
		
		cont++;
	}
	
	
	if(contador > 5){
		//-- Monta paginacao
		var div_wrapPaginacao = document.createElement('div');
		var div_paginacao = document.createElement('div');
		
		//-- colocando as classes
		div_wrapPaginacao.setAttribute('class','wrap-paginacao');
		div_wrapPaginacao.className = 'wrap-paginacao';
		div_paginacao.setAttribute('class','paginacao');
		div_paginacao.className = 'paginacao';
		
		var htmlPag = '';
		htmlPag += ' <a class="btPaginacao" href="javascript:fnMontaComunicados('+ (pag-1 > 1 ? pag-1 : 1) +',\''+ mes +'\',\''+ ano +'\'); void(0);">Anterior</a> ';
		for(i=1; i<=numeroDePag; i++){
			if(pag == i)
				htmlPag += ' <a class="numberPaginacao '+ (i == numeroDePag ? 'sem_border' : '') +' " href="javascript:fnMontaComunicados('+ i +',\''+ mes +'\',\''+ ano +'\'); void(0);"><strong>'+ i +'</strong></a> ';
			else
				htmlPag += ' <a class="numberPaginacao '+ (i == numeroDePag ? 'sem_border' : '') +' " href="javascript:fnMontaComunicados('+ i +',\''+ mes +'\',\''+ ano +'\'); void(0);">'+ i +'</a> ';
		}
		htmlPag += ' <a class="btPaginacao" href="javascript:fnMontaComunicados('+ (pag+1 < numeroDePag ? pag+1 : numeroDePag) +',\''+ mes +'\',\''+ ano +'\'); void(0);">Próxima</a> ';
		
		div_paginacao.innerHTML = htmlPag;
		div_wrapPaginacao.appendChild(div_paginacao);
		$('#painel_comunicados_paginacao *').remove();
		document.getElementById('painel_comunicados_paginacao').appendChild(div_wrapPaginacao);
	}else{
		//-- se a pagina possui menos de 5 itens, entao nao monta paginacao
		$('#painel_comunicados_paginacao *').remove();
	}
	
	$('#painel_comunicados dl.comunicados').mouseover(function(){ $(this).addClass('bgEscuro'); });
	$('#painel_comunicados dl.comunicados').mouseout(function(){ $(this).removeClass('bgEscuro'); });
}

function Noticia(pId,pTitulo,pDescricao,pTexto,pDia,pMes,pAno,pHref,pTarget,pUF){
	this.id = pId;
	this.titulo = pTitulo;
	this.descricao = pDescricao;
	this.texto = pTexto;
	this.dia = pDia;
	this.mes = pMes;
	this.ano = pAno;
	this.href = pHref;
	this.target = pTarget;
	this.uf = pUF;
}

function getNoticia(id, ano){
	try{
		xml = xmlLoader('/sys/xml/comunicados/noticias_' + ano + '.xml');
		var noticiasXml = xml.getElementsByTagName('noticia');
		
		var noticia = new Noticia();
		for(i=0; i<noticiasXml.length; i++){
			if(noticiasXml[i].attributes.getNamedItem('id').value == id){
				noticia.titulo = noticiasXml[i].getElementsByTagName('titulo')[0].childNodes[0].nodeValue;
				noticia.texto = noticiasXml[i].getElementsByTagName('texto')[0].childNodes[0].nodeValue;
				return noticia;
			}
		}
	}catch(e){
		window.location.href="../portal/a-vivo-comunicados.php";
	}
	return null;
}
