var LISTADO_TITULARES=null;
var MOD_TITULARES=null;

function iniciar_titulares()
{
	if(document.getElementById('listado_titulares'))
	{
		MOD_TITULARES=new Titulares(document.getElementById('listado_titulares'));
		MOD_TITULARES.inicio_cargar_titulares();
	}
}

window.onload=function()
{
	iniciar_titulares();

}

function Titulares(v_contenedor)
{
	this.bloqueado=false;
	this.DOM_contenedor=v_contenedor;
	this.DOM_boton_anterior=document.getElementById('titular_ir_arriba');
	this.DOM_boton_siguiente=document.getElementById('titular_ir_abajo');
	
	this.generar_eventos();
	
	this.desplazamiento=0;
	this.EN_PORTADA=5;
	this.cantidad_mostrar=6;
	this.opacidad=0;
}

Titulares.prototype.generar_eventos=function()
{
	var aquello=this;
	this.DOM_boton_anterior.onclick=function() {aquello.paginar(-1);}
	this.DOM_boton_siguiente.onclick=function() {aquello.paginar(1);}
}

Titulares.prototype.paginar=function(v_dir)
{
	if(!this.bloqueado)
	{
		if(v_dir < 0) 
		{
			this.desplazamiento-=this.cantidad_mostrar;
		}
		else if(v_dir > 0)
		{
			this.desplazamiento+=this.cantidad_mostrar;
		}

		this.cargar_titulares();
	}
}


Titulares.prototype.cargar_titulares=function()
{
	this.bloqueado=true;
	this.aparecer_desaparecer(this.inicio_cargar_titulares, -0.1, 0);
}

Titulares.prototype.inicio_cargar_titulares=function()
{
	var url='peticiones/titulares_interesantes.ajax.php?modo=obtener_titulares&d='+this.desplazamiento+'&v='+this.cantidad_mostrar;
	var xml=new Lector_XML();
	xml.crear(url, this.procesar_cargar_titulares, this, false);
}

Titulares.prototype.procesar_cargar_titulares=function(v_xml)
{
	var l=new Lector_doc(v_xml.raiz);
	l.bajar(0);

	var total=parseInt(l.posicion.atributos['total']);	
	var mostrados=parseInt(l.posicion.atributos['mostrados']);
	var desplazamiento=parseInt(l.posicion.atributos['desplazamiento']);

	if(mostrados)
	{
		this.limpiar_titulares();
		var i=0;

		l.bajar(0);
		for(i=0; i<mostrados; i++)
		{
			this.crear_titular(l.posicion.actual);		
			l.avanzar();
		}			
	}
	
	//Procesar el final...
	this.DOM_boton_anterior.className=desplazamiento==0 ? 'oculto' : 'scroll_arriba';
	this.DOM_boton_siguiente.className=((desplazamiento+mostrados)==total) ? 'oculto' : 'scroll_abajo';

	this.aparecer_desaparecer(this.desbloquear, 0.1, 1);
}

Titulares.prototype.limpiar_titulares=function()
{
	eliminar_contenido(this.DOM_contenedor);
}


Titulares.prototype.desbloquear=function()
{
	this.bloqueado=false;
}

Titulares.prototype.crear_titular=function(v_nodo)
{

	var l=new Lector_doc(v_nodo);
	l.bajar(0);
	
	var img_bala=l.obtener();
	var titulo=l.obtener();
	var tipo_elemento=l.obtener();
	var fecha=l.obtener();
	var texto=l.obtener();
	var url_ver_mas=l.obtener();

	var DOM_titular=montar_elemento('div', 'titular', null, null);
		var DOM_fecha=montar_elemento('div', 'fecha', null, DOM_titular);
			var DOM_imagen=montar_imagen('img_boletin/'+img_bala, null, null,DOM_fecha);
			var DOM_span_fecha=montar_elemento('span', 'texto_fecha', fecha, DOM_fecha);
		var DOM_titulo=montar_elemento('div', 'titulo', null, DOM_titular);
			var DOM_enlace=montar_enlace(url_ver_mas, null, titulo, DOM_titulo)
			
	this.DOM_contenedor.appendChild(DOM_titular);		
}

Titulares.prototype.aparecer_desaparecer=function(v_metodo, v_cantidad, v_final)
{
	var aquello=this;

	if(v_final==0) var finalizado=this.opacidad <= v_final;
	else var finalizado=this.opacidad >= v_final;

	if(finalizado)
	{
		v_metodo.call(aquello);
	}
	else
	{
		this.opacidad+=v_cantidad;
		this.variar_opacidad();
		setTimeout(function(){aquello.aparecer_desaparecer.call(aquello, v_metodo, v_cantidad, v_final);}, 50);
	}
}

Titulares.prototype.variar_opacidad=function()
{
	this.DOM_contenedor.style.opacity=this.opacidad;
	this.DOM_contenedor.style.filter='alpha(opacity='+(this.opacidad*100)+')';
}

Titulares.prototype.destruir=function()
{

}

