

function noticias(){ 
	this.req = false; 
	this.HTMLAsociado = "COMPONENTES/noticias.html";
	this.HTMLAsociado1 = "COMPONENTES/noticias_lateral.html";
	this.fundirCapaPrevia = false;
	this.contador = 0;
	this.urlBBDD	= "gestor/PHP/Noticias/noticias.php";
	this.casos = new Array();
	this.cargarHTML="";
	
	this.meses = new Array("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");

	
	}
	
	noticias.prototype.feed = function(){
		if (this.req.readyState == 4) {
			var resultados = new Array();	
			var xml = this.req.responseXML;
			for(var i=0;i<xml.getElementsByTagName("noticia").length;i++){
				var objetoNoticia = new Object();
				objetoNoticia.titulo = (xml.getElementsByTagName("titulo_es")[i].firstChild)? xml.getElementsByTagName("titulo_es")[i].firstChild.data : " ";
				objetoNoticia.fecha 	= (xml.getElementsByTagName("fecha")[i].firstChild)? xml.getElementsByTagName("fecha")[i].firstChild.data : " ";
				objetoNoticia.descripcion 	= (xml.getElementsByTagName("texto_es")[i].firstChild)? xml.getElementsByTagName("texto_es")[i].firstChild.data : " ";
				//alert(objetoNoticia.objetivo)
				this.casos.push(objetoNoticia);
			}
			this.iniciarMuestreo();
			}
		}
	
	noticias.prototype.cargarEnCapa = function(capa){ this.capaCarga = capa; }
	
	noticias.prototype.init = function(longitud){
		
		this.cargarHTML = this.HTMLAsociado;
		if(longitud != "largo"){ this.cargarHTML = this.HTMLAsociado1;}
			
			if(this.fundirCapaPrevia){
				new Effect.Appear(this.capaCarga, {duration:0.3,ref:this, from:1, to:0, afterFinish:  function(){this.ref.cargar(this.ref.cargarHTML,"mostrarDatos");}});
				}
			else{
				this.cargar(this.cargarHTML,"mostrarDatos"); 
				}
		}
	
	noticias.prototype.mostrarDatos = function(){
		if (this.req.readyState == 4) {
			var contenedor = this.capaCarga;
			contenedor.innerHTML = " ";
			contenedor.innerHTML = this.req.responseText;
			this.cargar(this.urlBBDD + "?alea="+Math.random(),"feed");	
			}
		}
		
	noticias.prototype.iniciarMuestreo = function(){
			this.cargarCaso();
			var self = this; 
			setInterval(function(){ 
			self.cargarCaso(); 
			}, 20000); 
		}
		
		
		
	noticias.prototype.cargarCaso = function(){
		var fecha = this.analizarFecha(this.casos[this.contador].fecha);
		var contenedor = this.capaCarga;
			contenedor.getElementsByTagName("span")[0].innerHTML=fecha.getDate()+" de "+this.meses[fecha.getMonth()-1]+ " de " +fecha.getFullYear();
			contenedor.getElementsByTagName("span")[1].innerHTML=this.casos[this.contador].titulo;
			//contenedor.getElementsByTagName("span")[0].innerHTML=this.casos[this.contador].resultados.substring(0,160)+" [...]";
			if(this.contador<this.casos.length-1){this.contador++}
			else{this.contador = 0};
			new Effect.Appear(this.capaCarga, {duration:1, from:0, to:1});
			
		}
	
	noticias.prototype.analizarFecha = function(fecha){
		var fechaJs = new Date();
			fechaJs.setFullYear(fecha.split("-")[0]);
			fechaJs.setMonth(fecha.split("-")[1]);
			var dia = fecha.split("-")[2];
			fechaJs.setDate(dia.substr(0,2));
		
		return fechaJs;
		}
	
	
		
	noticias.prototype.cargar = function(url, metodo){
		if(window.XMLHttpRequest && !(window.ActiveXObject)) { 
			try {this.req = new XMLHttpRequest(); } catch(e) {this.req = false;}
		} else if(window.ActiveXObject) { 
			try {this.req = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try {this.req = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {this.req = false;} } }
			if(this.req) { 
				var loader = this;
				this.req.onreadystatechange = function() { 
				eval("loader."+metodo+".call(loader)")
				}
			this.req.open("GET", url, true);
			this.req.send("");
			}
		}
		
	
