	// layout variables
	var bodyWidth;
	var bodyHeight;
	var bodyRatio;
	var wContentW;

	// background image variables
	var newImage;
	var bgImgWidth;
	var bgImgHeight;
	var bgImgRatio;
	var newWidth;
	var newHeight;
	var newLeft;
	var newTop;
	
	// random photo
	var randomNumber;
	var imagesList = new Array("fotos_fondo1.jpg","fotos_fondo2.jpg","fotos_fondo3.jpg","fotos_fondo4.jpg","fotos_fondo5.jpg","fotos_fondo6.jpg","fotos_fondo7.jpg","fotos_fondo8.jpg","fotos_fondo9.jpg","fotos_fondo10.jpg","fotos_fondo11.jpg","fotos_fondo12.jpg","fotos_fondo13.jpg","fotos_fondo14.jpg");
	var thereIsImg0 = false;
	var thereIsImg1 = false;
	var nextImg		= "backgroundImg0";
	var currentImg;
	var lastLoadedImage;
	var timerIsRunning = false;

	$(document).ready(function(){
		enableTimer();
	});

	$(window).load(function(){
		manageLayout();
		loadImage();
	});
	
	$(window).resize(function(){
		$("body").css({"overflow":"hidden"});
		manageLayout();
	});

	function manageLayout(){
		bodyWidth	= $("body").width();
		bodyHeight	= $("body").height();
		if(bodyHeight < 745) bodyHeight = 745;
		bodyRatio	= bodyWidth/bodyHeight;
		calculateImgFeatures();
		
		$("#wrapperBackground").css({
			"width" : "100%", 
			"height" : bodyHeight.toString() + "px"
		});
		
		if($("#" + currentImg).length > 0){
			$("#" + currentImg).css({
				"width" : newWidth.toString() + "px", 
				"height" : newHeight.toString() + "px", 
				"left" : newLeft + "px", 
				"top" : newTop + "px"
			});
		}
		
		if($("#" + nextImg).length > 0){
			$("#" + nextImg).css({
				"width" : newWidth.toString() + "px", 
				"height" : newHeight.toString() + "px", 
				"left" : newLeft + "px", 
				"top" : newTop + "px"
			});
		}
		
		$("body").css({"overflow":"visible"});
		
	}

	function loadImage(imageURL){

		if(!imageURL){
			imageURL = "bg_presitio/" + imagesList[Math.round(Math.random() * 13)];
			lastLoadedImage = imageURL;
		}
		if($("#backgroundImg0").length > 0) thereIsImg0 = true; else thereIsImg0 = false;
		if($("#backgroundImg1").length > 0) thereIsImg1 = true; else thereIsImg1 = false;

		if(!thereIsImg0 && !thereIsImg1){
			newImage = $("<img />").attr("src",imageURL).
			load(function(){
				bgImgWidth	= $(newImage).attr("width");
				bgImgHeight	= $(newImage).attr("height");
				bgImgRatio	= bgImgWidth/bgImgHeight;
				calculateImgFeatures();
				$(newImage).css({"width" : newWidth.toString() + "px", "height" : newHeight.toString() + "px", "left" : newLeft + "px", "top" : newTop + "px"});
				$(newImage).hide();
				$(newImage).attr("id",nextImg);
				$("#wrapperBackground").html(newImage);
				$("#" + nextImg).fadeIn(1000);
			});
		}

		else{
			newImage = $("<img />").attr("src",imageURL).
			load(function(){
				bgImgWidth	= $(newImage).attr("width");
				bgImgHeight	= $(newImage).attr("height");
				bgImgRatio	= bgImgWidth/bgImgHeight;
				calculateImgFeatures();
				$(newImage).css({"width" : newWidth.toString() + "px", "height" : newHeight.toString() + "px", "left" : newLeft + "px", "top" : newTop + "px"});
				$(newImage).attr("id",nextImg);
				$("#wrapperBackground").prepend(newImage);
				$("#" + currentImg).fadeOut(1000,function(){$(this).remove()});
			});
		}

		if(nextImg == "backgroundImg0"){
			nextImg		= "backgroundImg1";
			currentImg	= "backgroundImg0";
		}
		else{
			nextImg		= "backgroundImg0";
			currentImg	= "backgroundImg1";
		}
		
		// alert("bgImgRatio = " + bgImgRatio);
	
	}

	function calculateImgFeatures(){
		if(bgImgRatio > bodyRatio){
			newWidth	= bodyHeight * bgImgRatio;
			newHeight	= bodyHeight;
			newLeft		= (bodyWidth - (bodyHeight * bgImgRatio))/2;
			newTop		= 0;
		}
		else{
			newWidth	= bodyWidth;
			newHeight	= bodyWidth/bgImgRatio;
			newLeft		= 0;
			newTop		= (bodyHeight - (bodyWidth/bgImgRatio))/2;
		}
		
		// alert("newWidth = " + newWidth + ", bodyWidth = " + bodyWidth );
		
	}
	
	function enableTimer(){
		if(!timerIsRunning){
			$(document).everyTime(7000,"intervalo",function(){loadImage()});
			timerIsRunning = !timerIsRunning;
		}
	}
