// peticion ajax de nueva imagen
function loadImg(ind){
	// Ocultar
	$('#destacado_txt div').fadeOut(300);
	$('#destacado_producto img').fadeOut(300,function(){
		
		$.ajax({type: 'GET', url:host+'/_php/ajax_home.php', 
			data: 'img='+ind+'&lang='+lang, 
			error:function(){}, 
			success: function(html){changeImg(html,ind)}
		});
	});
}

// timer: siguiente imagen
function nextImg(){
	nextImg = (imgNum < totalImgs-1)? imgNum+1 : 0;
	loadImg(nextImg);
}

// cambiar html tras peticion
function changeImg(html,ind){
	var arrayHtml = html.split('___');
	var id = arrayHtml[0];
	var nombre = arrayHtml[1];
	var imagen = host+'/_images/productos/tipos/xg/'+arrayHtml[2];
	var href = host+'/'+lang+'/productos-y-servicios/familia.php?tipo='+id;
	
	var loader = new Image();
	loader.onload=function(){
		$('#destacado_txt div a').attr('href',href);
		$('#destacado_producto a').attr('href',href);
		
		$('#destacado_producto img').attr('src',imagen);
		$('#destacado_producto img').attr('alt',nombre);
		$('#destacado_producto img').attr('class','');
		
		$('#destacado_txt div h2 a').html(nombre)
		
		//mostrar
		$('#destacado_txt div').fadeIn(600);
		$('#destacado_producto img').fadeIn(600);
		
		$('#destacado_txt ul a').attr('class','');
		$('#destacado_txt ul a:eq('+ind+')').attr('class','num_producto_seleccionado');
		
		// reininiar timer
		imgNum = ind;
	}
	loader.src = imagen;
}

function setTimer(){
	$('body').everyTime('4s', nextImg);	
}

// crear eventos de links y de timer
$(document).ready(function(){
	setTimer();
	$('#destacado_txt ul a').bind('click', function(){
		$('body').stopTime();
		imgNum = $(this).attr('href').replace(host+'/'+lang+'/?img=','');
		loadImg(imgNum); 
		setTimer();
		return false;
	});					   
});