
var i=0// variable que lleva el control de las noticias
var j=1;// variable que toma el tope de las noticias
var seg=10;//segundos entre noticia y noticia
var estado=1;// 1-> play  0-> pausa
var TiempoID = null
var TiempoEjecutandose = false
var decimas, segundos


function DetenerTiempo ()
{
   	if(TiempoEjecutandose)
   		clearTimeout(TiempoID)

   	TiempoEjecutandose = false
}

function InicializarTiempo ()
{
	decimas = 0
	segundos = 0
}

function MostrarTiempo ()
{

	segundos++

	if ( segundos > 59 )
		segundos = 0

	//document.getElementById("tiempo").innerHTML=segundos;
	if(segundos == seg)
	{
		segundos=0;
		botones(1);
	}
  	TiempoID = setTimeout("MostrarTiempo()", 1000)
	TiempoEjecutandose = true

	return true
}

function nuevoAjax()
{
	/* Crea el objeto AJAX. Esta funcion es generica para cualquier utilidad de este tipo, por
	lo que se puede copiar tal como esta aqui */
	var xmlhttp=false;
	try
	{
		// Creacion del objeto AJAX para navegadores no IE
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			// Creacion del objet AJAX para IE
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(E) { xmlhttp=false; }
	}
	if (!xmlhttp && typeof XMLHttpRequest!="undefined")
	{
		xmlhttp=new XMLHttpRequest();
	}

	return xmlhttp;
}


function botones(nomboton){

	if(nomboton=='pau' || nomboton=='ant' || nomboton=='sig')
	{
		estado=0;
		document.getElementById("pau").style.display="none"; document.getElementById("ply").style.display="block";
		//document.getElementById("pau").style.display=="none"; document.getElementById("ply").style.display="inline";
		DetenerTiempo();
	}
	if(nomboton=='ply')
	{
		estado=1;
		document.getElementById("ply").style.display="none"; document.getElementById("pau").style.display="block";
		//document.getElementById("ply").style.display=="none"; document.getElementById("pau").style.display="inline";
		IniciarTiempo();
	}
	if(nomboton=='ant')
		Noticia('ant');
	if(nomboton=='sig')
		Noticia('sig');
	if(estado==1)
		Noticia();
}


function Noticia(valorA)
{
	if(valorA=='ant')
	{	i=i-1;
		if(i==0) i=j-1;
	}
	else if(valorA!='ant')
	{	i=i+1;
		if(i==j) i=1;
	}

	var ajax=nuevoAjax();
	ajax.open("POST", "proceso_ajax.php", true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("CODIGO="+j);
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
			var respuesta=new Array(3);
				respuesta=ajax.responseText.split("&INICIO");

				if(respuesta[1]!="vacio")
					document.getElementById("noticia").innerHTML=respuesta[1];
				else
					document.getElementById("noticia").innerHTML=='No hay noticia';
		}
	}
	j++;
	if (j>6){ // 6 es el maximo de noticias que esta trayendo el SQL
		j=1;
	}
}


function IniciarTiempo()
{
	DetenerTiempo();
	InicializarTiempo();

	MostrarTiempo();

		Noticia();
}


/*
function Noticia(valorA)
{
	if(valorA=='ant')
	{	i=i-1;
		if(i==0) i=j-1;
	}
	else if(valorA!='ant')
	{	i=i+1;
		if(i==j) i=1;
	}

    var cod=document.getElementById("cod").value;
    var campo1=document.getElementById("c1");
    var campo2=document.getElementById("c2");

	var ajax=nuevoAjax();
	ajax.open("POST", "archivo.xml?", true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("CODIGO="+j);
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
		var respuesta=ajax.responseXML;
            campo1.value=respuesta.getElementsByTagName("cod"+j)[0].childNodes[0].data;
            campo2.value=respuesta.getElementsByTagName("fecha"+j)[0].childNodes[0].data;
		}
	}
	j++;
	if (j>6){ // 6 es el maximo de noticias que esta trayendo el SQL
		j=1;
	}

}*/

