/**
 *  Este archivo se encarga de obtener las noticias que deben estar como banner principal.
 *  Solo se obtendrán 5 noticias.
 *  
 * @author Rafael Chavez Solis
 * 	Iensistemas
 *  rchavez@iensistemas.com
 *  rchavez@icadep.org
 */
 var DATA;
 var totalN = -1;
 var curN = -1;
 
 $(function(){
	 $.ajaxSetup({cache:false});
	 $("#titTwo #cuadroLeyenda").hide(function(){
		 loadAvailableNews();
	 });
 	 	
 });

function loadAvailableNews(){
	var urlpage = '../php/getNoticias.php';
	$.ajax({
		type: 'GET',
   		url: urlpage,
	    //dataType: ($.browser.msie) ? "text/xml" : "xml",
		dataType:'xml',
	    success: function(data){
		     /*if (typeof data == "string") {
			     DATA = new ActiveXObject("Microsoft.XMLDOM");
		         DATA.async = false;
			     DATA.loadXML(data);
		     } else {
			    DATA = data;
     		}*/
		 DATA = data;
    	 // Returned data available in object "xml"
		 totalN = $(DATA).find('elementos').text();
		 if (totalN > 0) {
			 loadBanners();
			 window.setInterval('loadBanners()', 12000);
		 }
		 else {//Aqui falta definir imagen por default
			$("#titTwo").html('<br/><br/><br/><h1>Sin noticias disponibles</h1>');
		 }
	   }
	 });
}

function loadBanners(){
	
    $("#titTwo #cuadroLeyenda").animate({
		height:"0px",
		width:"0px"
	  },900,function(){
	  		$("#titTwo #cuadroLeyenda .lcont,#titTwo #cuadroLeyenda .lmas").empty();
	  		$("#titTwo").animate({
				opacity: 0.0
			}, 1500,function(){
				$(this).css('background-image', 'url('+loadImage(curN)+')');
			});
	  					
	  		$("#titTwo").animate({
				opacity: 1
			  },1000,function(){
			  		$("#titTwo #cuadroLeyenda").animate({
						height:"200px",
						width:"300px"
					  },1000);
					  $("#titTwo #cuadroLeyenda .lcont").html(loadLeyenda(curN));
		  	  		  $("#titTwo #cuadroLeyenda .lmas").html('<a href="../noticias/viewNoticia.php?id='+loadIDNoticia(curN)+'">Leer m&aacute;s</a>');
		  	  		  
		  	  		
			  });
	  	
	  });
    
    if(curN < totalN-1)
			curN ++;
		else
		    curN=0;
}

function loadIDNoticia(idnoticia){
	var linktonew=0;
	$(DATA).find('noticia[id="'+idnoticia+'"]').each(function(){
		linktonew = $(this).find('link').text();
	});
	
	return linktonew;
}

function loadLeyenda(idnoticia){
	var leyenda="";
	$(DATA).find('noticia[id="'+idnoticia+'"]').each(function(){
		leyenda = $(this).find('leyenda').text();
	});
	
	return leyenda;
}

function loadImage(idnoticia){
	var image="";
	$(DATA).find('noticia[id="'+idnoticia+'"]').each(function(){
		image = $(this).find('foto').text();
	});
	
	return image;
}

