//CONFIGURACIÓ

var tempsCanviFons = 10000;//10s
var tempsTranscisio = 1200;

var tempo;
var fonsActual;
var pic_real_width1, pic_real_height1;
var pic_real_width2, pic_real_height2;


$(document).ready(function(){
	
	$('body').prepend('<div id="divImatgeFons" style="position:fixed; top:0px; left:0px; overflow:hidden"></div>');
	carregarImatge();
	$(window).resize(resizeBackground);
});



var imatgeActual = 2;

function carregarImatge(){
	if(imatgeActual==2){
		imatgeActual=1;	
	}else{
		imatgeActual=2;	
	}
	
	var imatge = $("<img id='imatgeFons"+imatgeActual+"'>");

	imatge.hide();
	imatge.bind("load",function(){ 
		
		if(imatgeActual==1){
			pic_real_width1 = $(this).width();   // Note: $(this).width() will not
			pic_real_height1 = $(this).height(); // work for in memory images.
		
			$("#imatgeFons1").fadeIn(tempsTranscisio, "easeOutExpo", function(){
				$("#imatgeFons2").remove();
			}); 
		
		
			$("#imatgeFons1").height(1);	
			$("#imatgeFons1").width(1);
		}else{
			pic_real_width2 = $(this).width();   // Note: $(this).width() will not
			pic_real_height2 = $(this).height(); // work for in memory images.
		
			$("#imatgeFons2").fadeIn(tempsTranscisio, "easeOutExpo", function(){
				$("#imatgeFons1").remove();
			}); 
		
		
			$("#imatgeFons2").height(1);	
			$("#imatgeFons2").width(1);
		}
		
		
		resizeBackground();
		canviBola(fonsActual);
		if(fons.length>1){
			tempo = setTimeout('canviFons()', tempsCanviFons);
		}
	});


	$('#divImatgeFons').append(imatge);
	imatge.attr('src',fons[fonsActual]);

}



function resizeBackground(){

	var posTOP = jQuery(document).scrollTop();

	var widthScreen = jQuery(window).width();
	if(widthScreen<984){ widthScreen=984; } 
	var heightScreen = jQuery(window).height();
	var proporcioScreen = widthScreen/heightScreen;
	
	jQuery("#divImatgeFons").width(widthScreen);
	jQuery("#divImatgeFons").height(heightScreen);
	
	
	
	var width1 = pic_real_width1;
	var height1 = pic_real_height1;
	var proporcioImatge1 = width1/height1;	
	

	if(proporcioImatge1 > proporcioScreen){
		$("#imatgeFons1").height(heightScreen);	
		$("#imatgeFons1").width(heightScreen * proporcioImatge1);
	}else{
		$("#imatgeFons1").width(widthScreen);
		$("#imatgeFons1").height(widthScreen / proporcioImatge1);
	}

	var _top1 = (heightScreen-jQuery("#imatgeFons1").height())/2;
	var _left1 = (widthScreen-jQuery("#imatgeFons1").width())/2;
	$("#imatgeFons1").css({position: 'fixed', top: _top1+"px", left: _left1+"px"});	
	
	
	var width2 = pic_real_width2;
	var height2 = pic_real_height2;
	var proporcioImatge2 = width2/height2;	
	

	if(proporcioImatge2 > proporcioScreen){
		$("#imatgeFons2").height(heightScreen);	
		$("#imatgeFons2").width(heightScreen * proporcioImatge2);
	}else{
		$("#imatgeFons2").width(widthScreen);
		$("#imatgeFons2").height(widthScreen / proporcioImatge2);
	}

	var _top2 = (heightScreen-jQuery("#imatgeFons2").height())/2;
	var _left2 = (widthScreen-jQuery("#imatgeFons2").width())/2;
	$("#imatgeFons2").css({position: 'fixed', top: _top2+"px", left: _left2+"px"});	

}



function Bola(num){	
	clearTimeout(tempo);
	fonsActual=num-1;
	canviFons();
}

function canviBola(num){
	$("#boleta"+num).attr({'src': 'img/bolagran.png'});
	$(".boleta:not(#boleta"+num+")").attr({'src': 'img/bolapetita.png'});
}



function canviFons(){	
	fonsActual++;
	if(fonsActual == fons.length){
		fonsActual=0;	
	}
	setCookie("Futur2fons",fonsActual,null, "/");
	carregarImatge();

}
