var registro=new Array(50);
var i=0// variable que lleva el control de las noticias
var j=1;// variable que toma el tope de las noticias
var seg=5;//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 IniciarTiempo()
{
	DetenerTiempo();
	InicializarTiempo();
	MostrarTiempo();
}

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 Buscar_Codigos()
{
	var k=0;
	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)
		{	
			while(ajax.responseXML.getElementsByTagName('cod'+j)[0].firstChild.data!="FIN")
			{
				registro[j]=ajax.responseXML.getElementsByTagName('cod'+j)[0].firstChild.data;
				j=j+1;
			}
		}
	}
	
	return j;
}

function botones(nomboton){
	if(nomboton=='pau' || nomboton=='ant' || nomboton=='sig')
	{	
		estado=0;
		document.getElementById("pau").style.display="none"; document.getElementById("ply").style.display="block";
		DetenerTiempo();
	}
	if(nomboton=='ply')
	{	
		estado=1; 
		document.getElementById("ply").style.display="none"; document.getElementById("pau").style.display="block";
		IniciarTiempo();
	}
	if(nomboton=='ant')
		Noticia('ant');
	if(nomboton=='sig')
		Noticia('sig');
	if(estado==1)
		Noticia();
}

Buscar_Codigos();

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 valor=registro[i];

	var ajax=nuevoAjax();
	ajax.open("POST", "proceso_ajax.php?", true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("id="+valor);
	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';	
		}
	}
}