var peticion = null;
var aux = 0;
function creaXhr(){
	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){
			if (!xmlhttp && typeof XMLHttpRequest!='undefined') xmlhttp=new XMLHttpRequest();
		}
	}
	return xmlhttp;	
}

function comprueba(){
	peticion = creaXhr();
	if(aux == 4){
			aux = 0;
	}
	aux = aux+1;	
		peticion.onreadystatechange = procesa;		
		peticion.open("GET","funciones/consultasAjax.php?dato="+aux+"&nocache="+Math.random(),true);
		peticion.send(null);
}

function procesa(){	
	if(peticion.readyState == 4){
		
		if(peticion.status == 200){			
			//animacion();
			var docu = peticion.responseXML;
			var raiz = docu.getElementsByTagName("noticia")[0];
			var titulo = raiz.getElementsByTagName("titulo")[0].firstChild.nodeValue;
			var cuerpo = raiz.getElementsByTagName("cuerpo")[0].firstChild.nodeValue;
			var fecha = raiz.getElementsByTagName("fecha")[0].firstChild.nodeValue;
			var idfecha = raiz.getElementsByTagName("idfecha")[0].firstChild.nodeValue;
			var img = aux;

			$("#fecha").text(fecha);
			$("#titulo").html(titulo);
			$("#imagen").html("<img src=\"imagenes/index/portada"+img+".jpg\" border=\"0\" />");
			$("#texto").html(cuerpo);
			$("a[href^=index01.php?page=paginas/otro/noticias]").attr("href","index01.php?page=paginas/otro/noticias&id_noti="+img+"&fecnoti="+idfecha+"&portada=1&galer=1");
			$("a[href^=galeria.php]").attr("href","galeria.php?carpt="+idfecha+"&&portada=1&&caso=1&&a=1&&b=6&&limite=0");
		}
	}
}

function animacion(){	
	$("#contenedor").fadeOut("slow",comprueba);	
	$("#contenedor").fadeIn("slow");	
}

/*function tratarError(mensaje){ 
   alert('Se ha producido el\nsiguiente error:\n\n'+mensaje);
   return true; 
} 
window.onerror = tratarError;

window.onload = function(){	
	setInterval(comprueba,2000);	
}*/
$(document).ready(function (){
		$("#texto").bind("mouseover",function(e){
			clearInterval(id);
		});
		
		$("#texto").bind("mouseout",function(e){
			animacion();
			id = setInterval(animacion,8000);			
		});
							
		if(aux == 0){
			comprueba();
		}		
		id = setInterval(animacion,8000);
});
