$(document).ready(function(){
	Shadowbox.init();
	
	$('#fondo .resize').each(function(){resizeBG($(this));});
	//fondo = self.setInterval("slideshow()",5000);
});


function slideshow(){
	var actual;
	var siguiente;
	
	actual = $("#galeria_fondo img.visible");
	siguiente = $("#galeria_fondo img.visible").prev("img");
	
	if(siguiente.attr("src")){
		siguiente = $("#galeria_fondo img.visible").prev("img");
	}
	else{
		siguiente = $("#galeria_fondo img:last");
	}
	
	actual.fadeOut(2000, function(){
		$("#galeria_fondo img").each(function(){
			this.style.zIndex = parseInt(this.style.zIndex)+1;
		}); 
		actual.css("z-index", "1");
		actual.removeClass("visible");
		siguiente.addClass("visible");
		actual.css("display","block");
	});
}

function resizeBG(bg){
	
	// bg = $('img.resize').first();
	
	bg.css({'width':'auto','height':'auto','margin-left':'50%','display':'block'});
	
	bgheight = bg.height();
	bgwidth = bg.width();
	
	height = $('body').height();
	width = $('body').width();
	
	newwidth = bgwidth;
	newheight = bgheight;
	
	
	
	if(bgwidth < width){
		newwidth = width;
		newheight = (newwidth*bgheight/bgwidth);
		
		if(newheight < height){
			newheight = height;
			newwidth = (newheight*bgwidth/bgheight);
		}
	}
	else{
		if(bgheight < height){
			newheight = height;
			newwidth = (newheight*bgwidth/bgheight);
		
			if(newwidth < width){
				newwidth = width;
				newheight = (newwidth*bgheight/bgwidth);
			}
		
		}
	}
	
	margin = (newwidth/2)*(-1);
	
	bg.css({'width':newwidth,'height':newheight,'left':margin,'display':'block'});
	
	/* AŅADIDO NUEVO */
	fondoHeight = $('body').height();
	$('#fondo').css({'height': fondoHeight ,'overflow-y':'hidden'});
}

setInterval("slideshow()",8000);
