/*******************************************************************************************
/* necesita: jquery.js e slideshow.css
/* feito por jorge con jquery e css
*******************************************************************************************/
$(document).ready(function(){
	
	$("#slidePhotos ul").css({"display":"none"})
	
	$(".js_enlace").each(function( i, thisEnlace ){
		thisEnlace.thisRutaFichero = $(thisEnlace).attr("href");
		thisEnlace.thisRel = $(thisEnlace).attr("rel");
		$(thisEnlace).attr({"href":"javascript:void(0);"});
		$(thisEnlace).bind("click", function(){
			$("#cargador_foto2").attr("src",this.thisRutaFichero);
			if(this.thisRel){
				var url_split = this.thisRel.split('#');
				$("#ir_a").attr("href",url_split[0]);
				$("#ir_a").attr("target",url_split[1]);
			}
			loop = i;
		});
	});
	
	var loop = 0;
	
	function rolling_photos(){
		loop++;
		
		if($(".js_enlace").size() < loop){
			loop = 0; //al comienzo
		}
	
		$("#cargador_foto2").fadeOut('slow',function(){
			$(".js_enlace").each(function( i, thisEnlace ){
				if(i == loop){
					$("#cargador_foto2").attr("src",this.thisRutaFichero);
					if(this.thisRel){
						var url_split = this.thisRel.split('#');
						$("#ir_a").attr("href",url_split[0]);
						$("#ir_a").attr("target",url_split[1]);
					}
				}
				$("#cargador_foto2").fadeIn('fast');
			});
		});

	}
	
	//rolling_photos();
	var looping = setInterval(rolling_photos,7000);	
	
	
	$("a").bind("focus",function(){this.blur();});
	
});

$(window).bind("load", function(){
	var slideShow = $( "#slidePhotos" );
	var backgroundColorOnClick = "#756201";
	var slideShowPrev = slideShow.find("#slidePhotosPrev");
	var slideShowNext = slideShow.find("#slidePhotosNext");
	slideShowPrev.bind("focus",function(){this.blur();});
	slideShowNext.bind("focus",function(){this.blur();})
	var anchoSlideShow = slideShow.outerWidth() -  ( slideShowPrev.outerWidth() + slideShowNext.outerWidth() );
	var UlSlideShow = slideShow.find("ul");
	UlSlideShow.css({"display":"inline"});
	var marginLeftIni = parseInt( UlSlideShow.css("margin-left").toLowerCase().split("px").join(""));
	var Nfotos = UlSlideShow.find("li").each(function(i){return this;}).length ; 
	var anchoLI = UlSlideShow.find("li:first").outerWidth();
	var anchoFoto = UlSlideShow.find("li:first img").outerWidth();
	var anchoUlSlideShow = ( Nfotos * anchoLI );
	if( anchoSlideShow < anchoUlSlideShow ){
		UlSlideShow.width( anchoUlSlideShow );
		var Nfoto = 0;
		slideShowNext.bind("click", function(){
			if( ( Nfoto * anchoLI ) < (anchoUlSlideShow - ( anchoSlideShow + marginLeftIni )) ){
				Nfoto ++;
				UlSlideShow.animate({ 
					marginLeft: parseInt( ( -1 * ( anchoLI * Nfoto ) ) + ( marginLeftIni ) ) + "px"
				}, 300 );
			}
		});
		slideShowNext.bind("mousedown", function(){
			this.backgroundColor = $(this).css("background-color");
			$(this).css({"background-color":backgroundColorOnClick});
		});
		slideShowNext.bind("mouseup mouseout", function(){
			$(this).css({"background-color": this.backgroundColor });
		});
		slideShowPrev.bind("click", function(){
			if( Nfoto > 0 ){
				UlSlideShow.animate({ 
					marginLeft: parseInt( (-1 * (anchoLI * Nfoto)) + ( anchoLI + marginLeftIni ) ) + "px"
				}, 300 );
				Nfoto --;
			}
		});
		slideShowPrev.bind("mousedown", function(){
			this.backgroundColor = $(this).css("background-color");
			$(this).css({"background-color":backgroundColorOnClick});
		});
		slideShowPrev.bind("mouseup mouseout", function(){
			$(this).css({"background-color": this.backgroundColor });
		});
	}else{
		var marginLeft = ( anchoSlideShow - anchoUlSlideShow ) / 2 + ( slideShowPrev.outerWidth());
		UlSlideShow.css({ "margin-left" : marginLeft+"px" });
	}
	
});
