var gId;
var gIniNews = 0; 
var gFinNews = 3;

gNoticias = new Array();


function Noticia (id, tit, txt, img, dvi, vin, fec, hor, act) {
	this.id = id;
	this.titulo = tit;
	this.texto = txt;
	this.imagen = img;
	this.descvinculo = dvi;
	this.vinculo = vin;
	this.fecha = fec;
	this.hora = hor;
	this.activo = act; 
}


function _Initialize(sLang, sId, sHistorico) {

	gLang = sLang; // lenguaje de la pagina de noticias, 1 = español, 2 = ingles
	gId = sId; 	// inicializa la variable global de noticia, si no está definida muestra de a tres, si esta definida muestra de a 1
//	alert(sId);
	_obtenerNoticias(sHistorico);
	
}


function _obtenerNoticias(sHistorico) {

	gHistorico = sHistorico;
	
	var sFields = "F_CODNOT|F_TITNOT|F_TXTNOT|F_IMGNOT|F_DVINOT|F_VINNOT|F_FECNOT|F_HORNOT|F_ACTNOT";
	var sTables = "3#t_noticias";
	var sClause = "WHERE F_ACTNOT = 1 ORDER BY F_FECNOT DESC";
		gNoticias = new Array();
		gIniNews = 0; 

		
	if (gHistorico == 1) {
		gFinNews = 18;

		if (gLang == 1) { 		
			var sClause = "WHERE F_ACTNOT = 1 AND (F_LNGNOT = 1 OR F_LNGNOT = 3) ORDER BY F_FECNOT DESC";
			sLeerMas = 'Leer +';

		}
		if (gLang == 2) {
			var sClause = "WHERE F_ACTNOT = 1 AND (F_LNGNOT = 2 OR F_LNGNOT = 3) ORDER BY F_FECNOT DESC";
			sLeerMas = 'Read +';

		}
		
		var sHTML =	"Actual |";
		document.getElementById("tdBriefNot").innerHTML = sHTML;
		document.getElementById("tdBriefNot").onclick=Function('_obtenerNoticias(0)');
		document.getElementById("tdBriefNot").style.cursor='pointer';


		var sHTML =	" Hist&oacute;rico";
		document.getElementById("tdArchiveNot").innerHTML = sHTML;
		document.getElementById("tdArchiveNot").style.cursor='text';

	}
	if (gHistorico == 0) {

		gFinNews = 3;
		if (gLang == 1) {
 		
			var sClause = "WHERE F_ACTNOT = 1 AND (F_LNGNOT = 1 OR F_LNGNOT = 3) ORDER BY F_FECNOT DESC LIMIT 0,11";
			sLeerMas = 'Leer +';

		}
		if (gLang == 2) {
			var sClause = "WHERE F_ACTNOT = 1 AND (F_LNGNOT = 2 OR F_LNGNOT = 3) ORDER BY F_FECNOT DESC LIMIT 0,11";
			sLeerMas = 'Read +';

		}
		var sHTML =	"Actual | ";
		document.getElementById("tdBriefNot").innerHTML = sHTML;
		document.getElementById("tdBriefNot").style.cursor='text';

		var sHTML =	" Hist&oacute;rico";
		document.getElementById("tdArchiveNot").innerHTML = sHTML;
		document.getElementById("tdArchiveNot").onclick=Function('_obtenerNoticias(1)');
		document.getElementById("tdArchiveNot").style.cursor='pointer';

	}

	_GetRecords(sFields, sTables, sClause, null, "_obtenerNoticiasRequest");

}


function _obtenerNoticiasRequest(aResult) {

	if (aResult) {
		for (x=0; x<aResult.length; x++) {
			var sCodNot = aResult[x][0];
			if (aResult[x][1] == null) {sTitNot = "";} else {sTitNot = aResult[x][1];}
			if (aResult[x][2] == null) {sTxtNot = "";} else {sTxtNot = aResult[x][2];}
			if (aResult[x][3] == null) {sImgNot = "";} else {sImgNot = aResult[x][3];}
			if (aResult[x][4] == null) {sDviNot = "";} else {sDviNot = aResult[x][4];}
			if (aResult[x][5] == null) {sVinNot = "";} else {sVinNot = aResult[x][5];}
			var sFecNot = aResult[x][6];
			var sHorNot = aResult[x][7];
			
			gNoticias[gNoticias.length] = new Noticia(sCodNot, sTitNot, sTxtNot, sImgNot, sDviNot, sVinNot, sFecNot, sHorNot);
		}
	}

	if (gId == null) {
		_mostrarNoticiasTres(0);	
		var sHTML = "<img src= 'img/iconRight.png' onclick = '_mostrarNoticiasTres(3);' style = 'cursor:pointer;'/>";
		document.getElementById('btnDer').innerHTML = sHTML;
		document.getElementById('btnDerUp').innerHTML = sHTML;
		var sHTML = "<img src= 'img/iconLeft.png' onclick ='_mostrarNoticiasTres(-3);' style = 'cursor:pointer;'/>";
		document.getElementById('btnIzq').innerHTML = sHTML;
		document.getElementById('btnIzqUp').innerHTML = sHTML;

	}
	else {
		_mostrarNoticiasUno(0);
		var sHTML = "<img src= 'img/iconRight.png' onclick = '_mostrarNoticiasUno(1);' style = 'cursor:pointer;'/>";
		document.getElementById('btnDer').innerHTML = sHTML;
		document.getElementById('btnDerUp').innerHTML = sHTML;
		var sHTML = "<img src= 'img/iconLeft.png' onclick ='_mostrarNoticiasUno(-1);' style = 'cursor:pointer;'/>";
		document.getElementById('btnIzq').innerHTML = sHTML;	
		document.getElementById('btnIzqUp').innerHTML = sHTML;
	
	}
}



function _mostrarNoticiasTres(sVal) {
	
	if (gHistorico == 0) {
		var sHTML = "<div class='NewsText'><table style = 'text-align:right;'><tr><td width='525px' id = 'btnIzq'></td><td id = 'btnDer'></td></tr></table></div>"
		document.getElementById('btnIzqDerBottom').innerHTML = sHTML;
		var sHTML = "<img src= 'img/iconRight.png' onclick = '_mostrarNoticiasTres(3);' style = 'cursor:pointer;'/>";
		document.getElementById('btnDerUp').innerHTML = sHTML;
		document.getElementById('btnDer').innerHTML = sHTML;
		var sHTML = "<img src= 'img/iconLeft.png' onclick ='_mostrarNoticiasTres(-3);' style = 'cursor:pointer;'/>";
		document.getElementById('btnIzqUp').innerHTML = sHTML;
		document.getElementById('btnIzq').innerHTML = sHTML;


		if (sVal == -3) {
			if (gIniNews > 0) {
				gIniNews += sVal; gFinNews += sVal;
			}
		}
		if (sVal == 3) {
			if ((gFinNews < 12) && (gNoticias.length > gIniNews+3)) {
				gIniNews += sVal; gFinNews += sVal;
			}
		}
	
		if (gNoticias.length > gIniNews) {
			sDesde = gIniNews + 1;
			if (gNoticias.length > gFinNews) {
				sHasta = gFinNews;
			}
			else {
				sHasta = gNoticias.length;
			}
		}
	}
	else {

		gIniNews = 0;
		gFinNews = gNoticias.length;
		sDesde = 1;
		sHasta = gNoticias.length;

		document.getElementById('btnIzqDerBottom').innerHTML = "";

		document.getElementById('btnIzqUp').innerHTML = "";

		document.getElementById('btnDerUp').innerHTML = "";

	
	}

	document.getElementById('Id').innerHTML = sDesde+" a "+sHasta+" de "+gNoticias.length;
	_borrarNoticias();

	var sHTML = "<DIV>";

	for (x=gIniNews; x<gFinNews; x++) {
		if (gNoticias[x] != undefined) {
//alert(gNoticias[x].titulo);
			var sFecha = gNoticias[x].fecha.substr(8,2)+"/"+gNoticias[x].fecha.substr(5,2)+"/"+gNoticias[x].fecha.substr(0,4);
			sHTML += "<table><tr><td width = '250px' style = 'font-size:12px; font-weight:bold; color:#006633;'><href='noticia.php?x="+x+"&gHistorico="+gHistorico+"'>"+gNoticias[x].titulo+"</td><td class = 'tdFechaHora'>"+sFecha+"</td><td class = 'tdFechaHora'>"+gNoticias[x].hora+"</td></tr></table>";

			if (gNoticias[x].texto != null) {aTxt = gNoticias[x].texto.split("#");}
			if (gNoticias[x].descvinculo != null) {aDvi = gNoticias[x].descvinculo.split("#");} else {aDvi = "";}
			if (gNoticias[x].vinculo != null) {aVin = gNoticias[x].vinculo.split("#");}else {aVin = "";}
			 			
			sHTML += "<table style = 'width:527px;'><tr><td style = 'width:182px;'>";		
			// primer parrafo
			if (gNoticias[x].imagen != 'empty')  {
				sHTML += "<img style='float:left;padding: 0px 12px 12px 0px;' src= 'user/img/"+gNoticias[x].imagen+"'/>";
			}
			
			if (gNoticias[x].id == '22') {
				sHTML += "</td><td style = 'font-size:12px; font-weight:bold;background-color:#C2EBCE;'><table><tr><td>";
			} else {
				sHTML += "</td><td class = 'bgParrafo NewsText'><table><tr><td>";
			}		

			if (aTxt[0] != "") {
				sHTML += "<p>"+aTxt[0]+"</p>";	
			}
//alert(aTxt[0]);
			if ((gLang == 1) && (aTxt[0]!= "")) {
				sHTML += "</td></tr><tr><td><a href='noticia.php?x="+x+"&gHistorico="+gHistorico+"'><strong>"+sLeerMas+"</strong></a></td></tr></table></td></tr></table>";	
			}
			else if ((gLang == 2) && (aTxt[0]!= "")){
				sHTML += "</td></tr><tr><td><a href='noticia_Ing.php?x="+x+"'><strong>"+sLeerMas+"</strong></a></td></tr></table></td></tr></table>";	
			}
	

			sHTML += "<table>";
			// for para cadena de descripciones de vinculos
			for (y=0; y<aDvi.length; y++) {
				if ((aDvi[y] != "")&& (aDvi[y] != 'empty')) {
					sHTML += "<tr><td><img style='float:left;padding: 0px 12px 12px 0px;' src= 'img/iconWorld.png'/><a href='"+aVin[y]+"'>"						   					+aDvi[y]+" </a></td></tr>";
				}
			}
			sHTML += "</table>";
			sHTML += "<table><tr><td colspan = '5' class = 'tdDotted'></td></tr></table>";
		}
	}
	sHTML += "</div>";

	document.getElementById("tdNews").innerHTML = sHTML;

}



function _mostrarNoticiasUno(sVal) {
	
	
	if (sVal == -1) {
		if (parseInt(gId) > 0) {
			gId--;
		}
	}
	if (sVal == 1) {
		if ((parseInt(gId) < gNoticias.length) && (gNoticias.length > parseInt(gId)+1)) {
			gId++;
		}
	}
//alert(gNoticias.length);
//alert(gId);

	if (gNoticias.length >= gId) {
		window.scrollTo(0, 340);
		document.getElementById('Id').innerHTML = parseInt(gId)+1+" de "+gNoticias.length;
			if (gNoticias[gId].texto != null) {aTxt = gNoticias[gId].texto.split("#");}
			if (gNoticias[gId].descvinculo != null) {aDvi = gNoticias[gId].descvinculo.split("#");}
			if (gNoticias[gId].vinculo != null) {aVin = gNoticias[gId].vinculo.split("#");}
		
		document.getElementById('Date0').innerHTML = gNoticias[gId].fecha;
		
		document.getElementById('Time0').innerHTML = gNoticias[gId].hora;

		var sHTML = "<table><tr><td class = 'tdHeader'>"+gNoticias[gId].titulo+"</td></tr></table>";
		document.getElementById('Header0').innerHTML = sHTML;
		
		var sHTML = "<div><table width = '500px' height = '300px'><tr><td valign = 'top'>";
		
		// for para cadena de parrafos
		for (y=0; y<aTxt.length; y++) {
			if ((gNoticias[gId].imagen != 'empty') && (y == 0)) {
				sHTML += "<img style='float:left;padding: 0px 12px 12px 0px;' src= 'img/"+gNoticias[gId].imagen+"'/>";
			}
			if (aTxt[y] != "") {
				sHTML += "<p>"+aTxt[y]+"</p>";	
			}
		}
		sHTML += "</td></tr></table></div>";
		document.getElementById('Text0').innerHTML = sHTML;
		var sHTML = "<div><table>";
		// for para vinculos
		for (y=0; y<aDvi.length; y++) {
			if ((aDvi[y] != "")&& (aDvi[y] != 'empty')) {
				sHTML += "<tr><td><img style='float:left;padding: 0px 12px 12px 0px;' src= 'img/iconWorld.png'/><a href='"+aVin[y]+"'>"+		 				aDvi[y]+" </a></td></tr>";
			}
		}
		sHTML += "</table></div>";
		document.getElementById('Vinculo0').innerHTML = sHTML;
		
		var sHTML = "";
		sHTML += "<table><tr><td><a href='index.php'>volver</a></td></tr></table>";
		document.getElementById('Volver').innerHTML = sHTML;
	}
}



function _borrarNoticias() {
		document.getElementById('tdNews').innerHTML = "";
}


