function updateSfondo(numero) {
	var secondi = 20000; //Espresso in MILLESIMI DI SECONDI
	var imgSfondo = '';
    
	switch (numero) {
		case 1: imgSfondo='background1.jpg'; numero++; break;
		case 2: imgSfondo='background2.jpg'; numero++; break;
		case 3: imgSfondo='background3.jpg'; numero = 1; break;
	}
	
	$('#sfondo').fadeOut('slow', function() {
		$("#sfondo").attr({ 
  src: "images/"+imgSfondo
	});


	$("#sfondo").fadeIn("slow");
  });
	

	/*$('body').css({background : 'url(sfondoDinamico.php?file=images/'+ imgSfondo + '&altezza='+ screen.height+'&larghezza='+ screen.width + ')'});*/
    
	 setTimeout("updateSfondo("+numero+");", secondi);
	}

$(function(){
	//PRELOAD DELLE IMMAGINI IN CACHE
	var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }

	$('#content>dt:not(.active) a').live('click',function(){
		var toHide=$('#content>dd:visible'),toShow=$(this.parentNode).next(),sH=toShow.height()
		$(this.parentNode).addClass('active').siblings().removeClass('active')
		toShow.animate({height:'show'},{step:function(now){
			toHide.height(~~(sH-now))
		},complete:function(){
			toHide.hide().height(sH)
		}})
		return false
	})
	
		$('#content>dt.active a').live('click', function(){ 
		var toHide=$('#content>dd:visible'),toShow=$(this.parentNode).next(),sH=toShow.height()
		$(this.parentNode).removeClass('active');
		toShow.animate({height:'hide'},{step:function(now){
			toHide.height(~~(sH-now))
		},complete:function(){
			toHide.hide().height(sH)
		}})
		
		return false
		
		});
		
});

$(document).ready(function() {
	
	 $("#bannerHome").fancybox({
        'overlayOpacity'	:	0.8,
		'overlayColor'		:	'black',
		'overlayShow'		:	true,
		'type' : 'image'
     });
	
	jQuery.preLoadImages("images/background1.jpg","images/background2.jpg","images/background3.jpg");
	updateSfondo(1);
	resizerImg('sfondo',getSizeScreen('w'),getSizeScreen('h'));
	
	$("a.foto").fancybox({});
	
});

$(window).resize(function() 
{
  
  resizerImg('sfondo',getSizeScreen('w'),getSizeScreen('h'));
  
});


function resizerImg(idImage,w,h) 
{
    $('#'+idImage).each(function() {
        var maxWidth = w; // Max width for the image
        var maxHeight = h;    // Max height for the image
        var ratio = 0;  // Used for aspect ratio
        var width = $(this).width();    // Current image width
        var height = $(this).height();  // Current image height

            ratio = maxWidth / width;   // get ratio for scaling image
            $(this).css("width", maxWidth); // Set new width

    });
}
function getSizeScreen(t)
{
  if(t=='w')
  {
    return $(window).width();
  }
  if(t=='h')
  {
    return $(window).height();
  }
}
