// JavaScript Document
//DESPLIEGUE DE NOTICIAS PORTADA INICIO


var lastItem;
var totalItems;
var noItems = false;
var almacen;
var cont=0;

function setGaleriaNoticias()
{
	this.setValoresItems();	
	this.setEventosFlechas();
}

function setValoresItems()
{	
	this.lastItem = document.getElementById('lastItem').value;
	this.totalItems = document.getElementById('totalItems').value;
}

function setEventosFlechas()
{
	

	document.getElementById('flechaRight').onclick = 
	function(){

	if(cont==0){
	
    	reqNoticiaPrev();
		reqNoticiaNext()
		cont++;
	}else{
		
		reqNoticiaNext()
		//
	}

	};
	document.getElementById('flechaRight').onmouseover = function(){
		this.src='art/flecha_derecha_gris.jpg';
	}; 
	document.getElementById('flechaRight').onmouseout = function(){
		this.src='art/flecha_derecha.jpg';
	}; 
	
	document.getElementById('flechaLeft').onclick = function(){
		reqNoticiaPrev()
	};
	document.getElementById('flechaLeft').onmouseover = function(){
		this.src='art/flecha_izquierda_gris.jpg';
	}; 
	document.getElementById('flechaLeft').onmouseout = function(){
		this.src='art/flecha_izquierda.jpg';
	}; 
}

function getLastItemNext()
{		
	if( parseInt(this.lastItem) < 0) this.lastItem = 0;
	
	
	return this.lastItem;	

}

function getLastItemPrev()
{		
	if( parseInt(this.lastItem) < 0) this.lastItem = 0;
	
	return this.lastItem;
}

function setLastItemNext()
{
	var newValor = parseInt(this.lastItem)+3;
	this.lastItem = newValor.toString();	
}

function setLastItemPrev()
{
	var newValor = parseInt(this.lastItem)-3;	
	this.lastItem = newValor.toString();
}

function setNoticiaColumna(noticiaItem,id_contenido,fechaIngreso,titulo,sub_titulo,imagen_1,noticiaTituloLink)
{
	var aryFechaIngreso = fechaIngreso.split(' ');
	var aryFecha = aryFechaIngreso[0].split('-');
	
	fechaYear = aryFecha[0];
	fechaMes = aryFecha[1];
	fechaDia = aryFecha[2];
	
	document.getElementById('noticiaFechaDay_'+noticiaItem).innerHTML = fechaDia;
	document.getElementById('noticiaFechaMonth_'+noticiaItem).innerHTML = fechaMes;
	document.getElementById('noticiaFechaYear_'+noticiaItem).innerHTML = fechaYear;
	
	document.getElementById('noticiaTitulo_'+noticiaItem).innerHTML = titulo;
	document.getElementById('noticiaSubTitulo_'+noticiaItem).innerHTML = sub_titulo;
	
	document.getElementById('lineaRoja_'+noticiaItem).src = 'art/linea_roja.jpg';
		
	if(imagen_1 == '') 
		imagen_1 = 'art/nada.jpg';
	else
		imagen_1 = 'art/contenido/'+imagen_1;
	
	document.getElementById('noticiaImg_'+noticiaItem).src = imagen_1;
	document.getElementById('noticiaItemTabla_'+noticiaItem).onclick = function(){
		showNoticiaDetalle(id_contenido,noticiaTituloLink)
	};
	
}

function cleanGaleriaCuadro()
{
	
	
	var top=3;
	for(i=1;i<=top;i++)
	{
		document.getElementById('noticiaFechaDay_'+i).innerHTML = '';	
		document.getElementById('noticiaFechaMonth_'+i).innerHTML = '';	
		document.getElementById('noticiaFechaYear_'+i).innerHTML = '';	
		
		document.getElementById('noticiaTitulo_'+i).innerHTML = '';
		document.getElementById('noticiaSubTitulo_'+i).innerHTML = '';
		
		document.getElementById('lineaRoja_'+i).src = 'art/nada.jpg';
		document.getElementById('noticiaImg_'+i).src = 'art/nada.jpg';
	}
}


function almacenar(respuesta){
	
	this.almacen=respuesta;
	
	}
		
	
function getAlmacen()
{
	return this.almacen;	
}

function reqNoticiaNext()
{		
	this.setLastItemNext();
	var lastItem = this.getLastItemNext();

	var url 	= 'galeriaNoticiasAjax.php?lastItem='+lastItem;
	var pars	= 'ejecutar=obtieneGrupoNoticiaPortada';
	var myAjax = new Ajax.Request(url,{method: 'get',parameters: pars,onComplete: resNoticiaNext});
	
	function resNoticiaNext(originalRequest)
	{	
		
		eval("var respuesta=" + originalRequest.responseText);
		
     if(respuesta != false )
      {
				
			cleanGaleriaCuadro();			
			
			 var largo=3;
			 var top = (largo - respuesta.length);			   
						 
			  			  
					if(respuesta.length<largo){
													
									
							   var almacen = getAlmacen();
									
							   for(i=0;i<top;i++)					   
							   {					
							setNoticiaColumna(i+1,almacen[respuesta.length].id_contenido,almacen[respuesta.length].fecha_ingreso,almacen[respuesta.length].titulo,  almacen[respuesta.length].sub_titulo,almacen[respuesta.length].imagen_1_cover,almacen[respuesta.length].noticiaTituloLink);
							respuesta.length++;
							   }
							
								for(i=0;i<largo;i++)			  
								{
								 setNoticiaColumna(top+1,respuesta[i].id_contenido,respuesta[i].fecha_ingreso,respuesta[i].titulo,respuesta[i].sub_titulo,respuesta[i].imagen_1_cover,respuesta[i].noticiaTituloLink);
								top++;
								}
										
						
					}else{
								almacenar(respuesta);
								
								for(i=0;i<respuesta.length;i++)
								 {	
								 setNoticiaColumna(i+1,respuesta[i].id_contenido,respuesta[i].fecha_ingreso,respuesta[i].titulo,respuesta[i].sub_titulo,respuesta[i].imagen_1_cover,                     respuesta[i].noticiaTituloLink);
							
								 }		
						 }
							
							
      }else
    	 {
            this.setLastItemPrev();
 		 }
		
      }

}


function reqNoticiaPrev()
{	
	this.setLastItemPrev();
	var lastItem = this.getLastItemPrev();
	
	var url 	= 'galeriaNoticiasAjax.php?lastItem='+lastItem;
	var pars	= 'ejecutar=obtieneGrupoNoticiaPortada';	
	var myAjax = new Ajax.Request(url,{method: 'get',parameters: pars,onComplete: resNoticiaPrev});
	
	function resNoticiaPrev(originalRequest)
	{		
		eval("var respuesta=" + originalRequest.responseText);
		
		
			cleanGaleriaCuadro();
		
	almacenar(respuesta);	
			for(i=0;i<respuesta.length;i++)
		{	
			setNoticiaColumna(i+1,respuesta[i].id_contenido,respuesta[i].fecha_ingreso,respuesta[i].titulo,respuesta[i].sub_titulo,respuesta[i].imagen_1_cover,respuesta[i].noticiaTituloLink);
			
	
		}	
		
		
		
	}
}

ARY_WINDOW_ONLOAD_FUNCTIONS.push("this.setGaleriaNoticias()"); 

//DESPLIEGUE DE NOTICIAS PORTADA FIN
