// JavaScript Document

$(document).ready(function(){
	//$('#summary').fadeIn('slow');

		$.preload( '#bgSlideShow img', {//the first argument is a selector to the images
			onRequest:request,
			onComplete:complete,
			onFinish:finish,
			placeholder:'../css/loading.gif',//this is the really important option
			notFound:'../img/notfound.jpg',//optional image if an image wasn't found
			threshold: 2 //'2' is the default, how many at a time, to load.
		});
	/*	
	var mostramenu = 0;					   
	$("a:not(#testiMenuTopFondoScelta a)").mouseover( 
		function(){
			$("#menuTopFondoScelta").hide();
			$("#testiMenuTopFondoScelta").hide();
			mostramenu=0;
			
		});
		

	$("#clickCambiaLingua").click( function (){
		if (mostramenu==1) {
			$("#menuTopFondoScelta").hide();
			$("#testiMenuTopFondoScelta").hide();
			mostramenu=0;
		} else {
			$("#menuTopFondoScelta").show();
			$("#testiMenuTopFondoScelta").show();
			mostramenu=1;
		}
	});
	
	
	$("#menuTopFondoScelta").mouseleave(function(){
		if	(mostramenu==1)	 {								 
			mostramenu=0;
		$("#menuTopFondoScelta").hide();
		$("#testiMenuTopFondoScelta").hide();
		}
    });*/
		  
});
		
function update( data ){
			$('#done').html( ''+data.done );
			$('#total').html( ''+data.total );
			$('#loaded').html( ''+data.loaded );
			$('#failed').html( ''+data.failed );
		};
function complete( data ){
	update( data );
	$('#image-next').html( 'none' );//reset the "loading: xxxx"
	$('#image-loaded').html( data.image );
};
function request( data ){
	update( data );
	$('#image-next').html( data.image );//set the "loading: xxxx"
};
function finish(){//hide the summary
	$('#summary').fadeOut('slow');
	$('#bgSlideShow').fadeIn(2000, function () {
		$(this).cycle({
			delay: 2000,
			speed: 2000,
			timeout: 20000,
			sync: 0,
			fit: 0,
			before: onBefore
		});
	});	
};			 		

function onBefore(){	
	$('#bgSlideShow img').centerScreen();	
	}

jQuery.fn.centerScreen = function(loaded) {
	var obj = this;
	//Define image ratio
	var ratio = 3/4;
	
	//Gather browser and current image size
	var imagewidth = $(this).width();
	var imageheight = $(this).height();
	var browserwidth = $(window).width();
	var browserheight = $(window).height();
	var offset;

	//Check aspect ratio
	if ((browserheight/browserwidth) > ratio){
		$(this).height(browserheight);
		$(this).width(browserheight / ratio);
		/*$(this).children().height(browserheight);
		$(this).children().width(browserheight / ratio);*/
	} else {
		$(this).width(browserwidth);
		$(this).height(browserwidth * ratio);
	   /* $(this).children().width(browserwidth);
		$(this).children().height(browserwidth * ratio);*/
	}			
	$(this).css('top', 0);//(browserheight - $(this).height())/2);
	$(this).css('left', (browserwidth - $(this).width())/2);	
	//alert((browserheight - $(this).height())/2);
	if(!loaded) {
			$(window).resize(function() { obj.centerScreen(!loaded); });
	}
}

