// contador para el slider de la home
var cual = 0;
var tm = setInterval("apertura.carrusel(cual)",18000);

var apertura = {
	carruselClick:function(){			
		// apertura
		if( $(".thumbnail").length ){
			$(".thumbnail li h2 a").click(function(){
				var obj = $("#imageThumb a");
				obj.hide();
				
				// cambiamos la imagen grande
				var src = $(this).parent().parent().children(".ampliacion").attr("src");
				var href = $(this).parent().parent().children("h2").children(".ampliacion_url").attr("href");
				$("#imageThumb .foto").hide();
				$("#imageThumb .foto").attr("src",src);
				$("#imageThumb .foto").parent().attr("href", href);
				$("#imageThumb > a").fadeIn("slow");
				$("#imageThumb .foto").fadeIn("slow");				
				
				// rellenamos el texto
				var detalle = $(this).parent().parent().children("div").html();
				$("#imageThumb .show").hide();
				$("#imageThumb .show").html(detalle).show();
				
				//hacemos el strong del enlace
				$(".thumbnail li").children("a").css("font-weight","normal");
				$(".thumbnail li a").children("strong").css("font-weight","normal");
				$(this).parent().parent().children("a").css("font-weight","bold");
				$(this).parent().parent().children().children().children("strong").css("font-weight","bold");
	
				// hacemos el hover de la galeria
				$(".thumbnail li").removeClass();
				$(this).parent().parent().addClass("sel");				

				// Reiniciamos el interval que cambia de noticia
				var liclicado = $(this).parents('li');
				var posicionClicado = $(".thumbnail li").index(liclicado);
				cual = posicionClicado+1;
				clearInterval(tm);
				tm = setInterval("apertura.carrusel(cual)",10000);
				
				return false;
			});
			
			$(".thumbnail li>img").click(function(){
													 
				var obj = $("#imageThumb img");
				obj.hide();
				
				// cambiamos la imagen grande
				var src = $(this).parent().children(".ampliacion").attr("src");
				var href = $(this).parent().children("h2").children(".ampliacion_url").attr("href");
				$("#imageThumb .foto").hide();
				$("#imageThumb .foto").attr("src",src);
				$("#imageThumb .foto").parent().attr("href", href);
				$("#imageThumb > a").fadeIn("slow");
				$("#imageThumb .foto").fadeIn("slow");				
				
				// rellenamos el texto
				var detalle = $(this).parent().children("div").html();
				$("#imageThumb .show").hide();
				$("#imageThumb .show").html(detalle).show();
				
				//hacemos el strong del enlace
				$(".thumbnail li").children("a").css("font-weight","normal");
				$(".thumbnail li a").children("strong").css("font-weight","normal");
				$(this).parent().children("a").css("font-weight","bold");
				$(this).parent().children().children().children("strong").css("font-weight","bold");
	
				// hacemos el hover de la galeria
				$(".thumbnail li").removeClass();
				$(this).parent().addClass("sel");
				
				// Reiniciamos el interval que cambia de noticia
				var liclicado = $(this).parents('li');
				var posicionClicado = $(".thumbnail li").index(liclicado);
				cual = posicionClicado+1;
				clearInterval(tm);
				tm = setInterval("apertura.carrusel(cual)",10000);
				
				return false;
			});
		}
	},
	carrusel:function(){			
		if( $(".thumbnail").length){
			// si hemos llegado a los 5 elementos del slider reseteamos para que empiece de nuevo
			if(cual == $(".thumbnail li").length){ cual = 0; }
			
			var obj = $(".thumbnail li");
			
			var img = $("#imageThumb img");
			img.hide();
		
			// cambiamos la imagen grande
			var src = $(obj[cual]).children(".ampliacion").attr("src");
			var href = $(obj[cual]).children("h2").children(".ampliacion_url").attr("href");
			$("#imageThumb .foto").hide();
			$("#imageThumb .foto").attr("src",src);
			$("#imageThumb .foto").parent().attr("href", href);
			$("#imageThumb .foto").fadeIn("slow");
			//$("#imageThumb .foto").load(function(){$("#imageThumb .foto:hidden").fadeIn("slow")});				
			
			// rellenamos el texto
			var detalle = $(obj[cual]).children("div").html();
			//alert(detalle)
			$("#imageThumb .show").hide();
			$("#imageThumb .show").html(detalle).show();
			
			//hacemos el strong del enlace
			$(".thumbnail li").children("a").css("font-weight","normal");
			$(".thumbnail li a").children("strong").css("font-weight","normal");
			$(obj[cual]).children("a").css("font-weight","bold");
			$(obj[cual]).children().children().children("strong").css("font-weight","bold");

			// hacemos el hover de la galeria
			$(".thumbnail li").removeClass();
			$(obj[cual]).addClass("sel");	
			
			cual++;
		}
	}
}




$(function () {	
	if( $(".thumbnail").length){
		if( $(".thumbnail li").length>1){
			apertura.carruselClick();
		}
	// lanzo el carrusel para que ponga la primera noticia en la apertura
	apertura.carrusel(cual);
	
	}
	
})
