// -------------------------------------------------------------------------------------------
// Funcions per a noticies.

// -------------------------------------------------------------------------------------------
var aId      = new Array("");
var aData    = new Array("");
var aHora    = new Array("");
var aAutor   = new Array("");
var aTitol   = new Array("");
var aPrefix  = new Array("");
var aIcona   = new Array("");
var aResum   = new Array("");
var aLinies  = new Array("");
var aLinia   = new Array("");
var aFotos   = new Array("");
var aPeu     = new Array("");
var aTextPla = new Array("");

var nNotsxPag = 5;
var nNotActual = 1;

// -------------------------------------------------------------------------------------------
function getXMLHTTPRequest() {
	try {
	req = new XMLHttpRequest(); /* p.ex. Firefox */
	} catch(e) {
		try {
		req = new ActiveXObject("Msxml2.XMLHTTP");  /* algunes versione d'IE */
		} catch (e) {
			try {
			req = new ActiveXObject("Microsoft.XMLHTTP");  /* algunes versions d'IE */
			} catch (e) {
				req = false;
			} 
		} 
	}
	return req;
}

var http = getXMLHTTPRequest();
  
function getNoticies() {
	document.getElementById("tNoticies").innerHTML = '&nbsp;';
	var myurl = '../scripts/noticies.xml';
	http.open("GET", myurl, true);
	http.onreadystatechange = useHttpResponse;
	http.send(null);
}

function useHttpResponse() {
	if (http.readyState == 4) {
		if(http.status == 200) { 
			aId     = http.responseXML.getElementsByTagName("id");
			aData   = http.responseXML.getElementsByTagName("data");
			aHora   = http.responseXML.getElementsByTagName("hora");
			aAutor  = http.responseXML.getElementsByTagName("autor");
			aTitol  = http.responseXML.getElementsByTagName("titol");
			aPrefix = http.responseXML.getElementsByTagName("prefix");
			aIcona  = http.responseXML.getElementsByTagName("icona");
			aResum  = http.responseXML.getElementsByTagName("resum");
			aLinies = http.responseXML.getElementsByTagName("linies");
			aFotos  = http.responseXML.getElementsByTagName("fotos");
			aPeu    = http.responseXML.getElementsByTagName("peu");
			nNotActual = aData.length-1;
			if (origen_noticies == 1) printLlistaNoticies();
			//	if (origen_noticies == 2) printNoticia(quina_noticia);
			if (origen_noticies == 2) printNoticia(id_noticia);
		}
	}
	else{
		var text = '<img src="../icones/espera.gif" alt="Esperi ..." width="32" border="0">';
		document.getElementById("tNoticies").innerHTML = text;
	}
}

function printLlistaNoticies() {
	var enllac = '';
	var quants = nNotActual - nNotsxPag + 1;
	if (quants < 0) quants = 0;
	txt = '<table width="100%" cellspacing="0" cellpadding="0" border="0">';
	for(var i=nNotActual; i>=(quants); i--){
		enllac = '';
		txt += '<tr><td class="fontTexte" colspan="3"><hr></td></tr>';
		txt += '<tr><td class="fontTexte" colspan="3">&nbsp;</td></tr>';
		txt += '<tr><td width="15%" rowspan="2">';
		txt += '	<a href="noticia.php?not='+aId[i].childNodes[0].nodeValue+'">';
		txt += '		<img src="../icones/';
			txt += aPrefix[i].childNodes[0].nodeValue + aIcona[i].childNodes[0].nodeValue + '.jpg';
			txt += '" alt="" width="100" height="75" border="0">';
		txt += '	</a></td>';
		txt += '	<td width="5%" rowspan="2">&nbsp</td>';
		//	txt += '	<td width="80%" class="fontTexte"><a href="javascript:void(printNoticia(\''+aData[i].childNodes[0].nodeValue+'\'))">';
		txt += '	<td width="80%" class="fontTexte"><a href="noticia.php?not='+aId[i].childNodes[0].nodeValue+'">';
		txt += '		'+aTitol[i].childNodes[0].nodeValue;
		txt += '		</a>';
		txt += '		<br>';
		txt += '		'+aData[i].childNodes[0].nodeValue;
		txt += '	</td>';
		txt += '</tr>';
		txt += '<tr>';
		txt += '	<td class="fontTexte" align="justify">';
		txt += '		'+aResum[i].childNodes[0].nodeValue;
		txt += '	</td>';
		txt += '</tr>';
		txt += '<tr><td class="fontTexte" colspan="3">&nbsp;</td></tr>';
	}
	txt += '<tr><td class="fontTexte" colspan="3"><hr></td></tr>';
	txt += '<tr><td class="fontTexte" colspan="3">&nbsp;</td></tr>';
	txt += '</table>';
	document.getElementById("tNoticies").innerHTML = txt;

	var nQuantesPagines = (aData.length / nNotsxPag);
	if (nQuantesPagines != parseInt(aData.length / nNotsxPag)){
		nQuantesPagines = 1 + parseInt(aData.length / nNotsxPag);
	}
	else{
		nQuantesPagines = parseInt(aData.length / nNotsxPag);
	}
	quina_pagina = 1 + (aData.length - nNotActual - 1) / nNotsxPag;
	txt = 'Pàgina : ';
	for(var i=1; i<=(nQuantesPagines); i++){
		if (i == quina_pagina ){
			txt += '-' + i + '- | ';
		}
		else{
			txt += '<a href="javascript:void(paginaNumero(\'' + i + '\'))">' + i + '</a> | ';
		}
	}
	document.getElementById("tNumeracio").innerHTML = txt;
}

function paginaNumero(quina_pagina) {
	nNotActual = aData.length - ((quina_pagina - 1) * nNotsxPag) - 1;
	printLlistaNoticies();
}

function printNoticia(id_noticia) {
	//	Busquem l'index de la noticia segons ID.
		for (var i=0; i<aId.length; i++) {
			if (aId[i].childNodes[0].nodeValue == id_noticia) {
				quina_noticia = i;
			}
		}
	//	Titol
		txt = aTitol[quina_noticia].childNodes[0].nodeValue;
		document.getElementById("tNoticies").innerHTML = txt;
	//	Data
		txt = aData[quina_noticia].childNodes[0].nodeValue;
		document.getElementById("tNotData").innerHTML = txt;
	//	Linies
		aLinia = aLinies[quina_noticia].getElementsByTagName("linia");
		txt = '<table width="100%" cellspacing="0" cellpadding="0" border="0">';
		for (var x=0; x<aLinia.length; x++){
			if (aLinia[x].childNodes[0] != null){
				txt += '<tr><td class="fontTexteCentrat">'+aLinia[x].childNodes[0].nodeValue + "</td></tr>";
				txt += '<tr><td class="fontTexte">&nbsp;</td></tr>';
			}
		}
		txt += '</table>';
		document.getElementById("tNotLinies").innerHTML = txt;
	//	Fotos
		txt = '';
		if (aFotos[quina_noticia].childNodes[0] != null){
			txt = aFotos[quina_noticia].childNodes[0].nodeValue;
			aNoms = txt.split(",");
			txt = 'Veure fotos : ';
			for (var x=0; x<aNoms.length; x++){
				txt += '<a href="javascript:void(canvi_foto(\'';
				txt += aPrefix[quina_noticia].childNodes[0].nodeValue + aNoms[x];
				txt += '\'))">'+(x+1)+'</a>&nbsp;&nbsp;';
			}
			document.getElementById("tIndexFotos").innerHTML = txt;
			//	La primera
			var cTxt = '<img src="../fotos/';
			cTxt += aPrefix[quina_noticia].childNodes[0].nodeValue + aNoms[0];
			cTxt += '.jpg" width="600">';
			document.getElementById("tFotos").innerHTML = cTxt;
		}
		else{
			document.getElementById("tIndexFotos").innerHTML = '&nbsp;';
			document.getElementById("tFotos").innerHTML = '&nbsp;';
		}
	//	Peu
		if (aPeu[quina_noticia].childNodes[0] != null){
			txt = aPeu[quina_noticia].childNodes[0].nodeValue;
		}
		else{
			txt = '';
		}
		document.getElementById("tNotPeu").innerHTML = txt;
		//	document.title = aTitol[quina_noticia].childNodes[0].nodeValue + " [meteovilatorta.cat] ";
		document.title = aTitol[quina_noticia].childNodes[0].nodeValue;
		
		location.URL = 'href="noticia.php?not='+aId[quina_noticia].childNodes[0].nodeValue+'"';
		//	Twitter
		//	NO - txt = '<a href="http://twitter.com/share" class="twitter-share-button" data-count="none" expr:data-url="data:post.url" expr:data-text="data:post.title">Piulada</a>';
		//	NO - txt = '<a href="http://twitter.com/share" class="twitter-share-button" data-count="none" data-url="'+location.URL+'" data-text="'+document.title+'">Piulada</a>';
		txt = '<a href="http://twitter.com/share" class="twitter-share-button" data-count="none" expr:data-url="data:post.url" data-text="'+document.title+'">Piulada</a>';
		document.getElementById("tTwitter").innerHTML = txt;
}

function noticiaPrimera() {
	//	Primera noticia segons l'ordre de la llista.
	//	quina_noticia = aData.length - 1;
	//	printNoticia(quina_noticia);
	var adressa = 'noticia.php?not='+aId[0].childNodes[0].nodeValue;
	window.location = adressa;
}

function noticiaAnterior() {
	//	Anterior noticia segons l'ordre de la llista.
	//	quina_noticia = quina_noticia + 1;
	//	if (quina_noticia >= aData.length){
	//		quina_noticia = aData.length - 1;
	//	}
	//	printNoticia(quina_noticia);

	if (quina_noticia >= 0) {
		quina_noticia = quina_noticia - 1;
	}
	var adressa = 'noticia.php?not='+aId[quina_noticia].childNodes[0].nodeValue;
	window.location = adressa;
}

function noticiaSeguent() {
	//	Seguent noticia segons l'ordre de la llista.
	//	quina_noticia = quina_noticia - 1;
	//	if (quina_noticia < 0){
	//		quina_noticia = 0;
	//	}
	//	printNoticia(quina_noticia);
	
	if (quina_noticia < aId.length-1) {
		quina_noticia=quina_noticia+1;
	}
	var adressa = 'noticia.php?not='+aId[quina_noticia].childNodes[0].nodeValue;
	window.location = adressa;
}

function noticiaUltima() {
	//	Ultima noticia segons l'ordre de la llista.
	//	quina_noticia = 0;
	//	printNoticia(quina_noticia);
	
	var adressa = 'noticia.php?not='+aId[aId.length-1].childNodes[0].nodeValue;
	window.location = adressa;
}

