  $.fn.wait = function(time, type) {
        time = time || 1000;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $(self).dequeue();
            }, time);
        });
    };



 
var totale_offerte = 0;
var totale_news = 0;
var totale_sconti = 0;
var totale_foto = 0;

function conta(){
 totale_offerte = $("#offerte_home li").length;
 totale_news = $("#news li").length;
 totale_sconti = $("#sconti li").length;
 

 $("#sconti li").each(function(i, val) {
      this.id = "sconti_"+i;
	  
    });

 
 
 totale_foto = $("#testo_home li").length;
 
 parti();
}

 

 function parti() {
	$("#loading").fadeOut("fast");
	$("#testo_home").fadeTo("slow", 0.66);
	$("#all").fadeIn("fast", 
		function(){$("#home_up").fadeIn(attiva)}
		
		);
	
}

function attiva(){

 setTimeout("offerte_home()", 0);
 setTimeout("news_home()", 300);
  setTimeout("foto_home()", 600);
setTimeout("sconti_home()", 900);

}












var im = "/public/home_1.jpg";
var o=-1;
function offerte_home(){
	
	  o=o+1;
	 
   		if(o>=totale_offerte){o=0;}
	
		im = ""+$("#off_"+o).attr("title")+".jpg";
		$("#offerte_home").css("background","url("+im+")");
		 
		$("#offerte_home").show(500);
		$("#off_"+o).fadeIn(1000);
   		$("#off_"+o).animate({"left": "+=0px", "top": "+=0px"}, 5000, function(){$("#offerte_home").fadeOut(1000);$("#off_"+o).fadeOut(1000, function(){$("#off_"+o).animate({"left": "-=0px", "top": "-=0px"}, "fast", offerte_home);})});
     
  } 
	 
	 

var im2 = "/public/home_1.jpg";
var n=-1;
function news_home(){
	
	  n=n+1;
	 
   		if(n>=totale_news){n=0;}
	
		im2 = "/public/"+$("#news_"+n).attr("title")+".jpg";
		$("#news_home").css("background","url("+im2+")");
		 
		$("#news_home").show(500);
		$("#news_"+n).fadeIn(1000);
   		$("#news_"+n).animate({"left": "+=0px", "top": "+=0px"}, 10000, function(){$("#news_home").fadeOut(1000);$("#news_"+n).fadeOut(1000, function(){$("#news_"+n).animate({"left": "-=0px", "top": "-=0px"}, "fast", news_home);})});
     
  } 	 
	 


var im3 = "/public/home_1.jpg";
var f = -1;
function foto_home(){
	
	  f=f+1;
	
   		if(f>=totale_foto){f=0;}
	 
	
		im3 = "/foto/"+"foto_"+f+".jpg";
		$("#foto_home").css("background","url("+im3+") bottom no-repeat");
		 
		$("#foto_home").show(500);
		$("#testo_home").fadeIn(1000);
   		
		$("#foto_"+f).fadeIn(1000);
   		$("#foto_"+f).animate({"left": "+=0px", "top": "+=0px"}, 10000, function(){$("#foto_home").fadeOut(1000);$("#testo_home").fadeOut(1000, function(){$("#foto_"+f).fadeOut(1000);$("#foto_"+f).animate({"left": "-=0px", "top": "-=0px"}, "fast", foto_home);})});
     
  } 	 




var s=-1;
function sconti_home(){
	  s=s+1;
   		if(s==totale_sconti){s=0;}
		$("#sconti_"+s).fadeIn(1000);
		$("#sconti_"+s).wait(2000);
		$("#sconti_"+s).fadeOut(1000, sconti_home);
   		
  } 
	 



  
 var w=480;
var h =400;
var Pic = new Array();

function inizializza_slide(){

	 $("#link_foto li a").each(function(i){
	   Pic[i] = $(this).attr("href");
	   
	  
	});


 
p = Pic.length;
preLoad = new Array()

for (i = 0; i < p; i++){
   preLoad[i] = new Image()
   preLoad[i].src = Pic[i]
}


	$("#foto").append("<div id=\"immagini\"><img  id=\"slide\" src=\""+Pic[0]+"\" /></div>");
	$("#immagini").animate({  width: w+"px",height:h+"px"}, 0);
	$("#immagini").append("<img  id=\"SlideShow\" src=\""+Pic[0]+"\" />");
	$("#SlideShow").animate({  width: w+"px",height:h+"px"}, 0);
	$("#slide").animate({  width: w+"px",height:h+"px"}, 0);

	 parti();
	
	cambia2();
	
	}
  
 
  
  
  
  
var l = -1;
var i =-1;
		
var vai =0;
function chiudi()
{
$("#slide").attr({ src: preLoad[l].src});
if(vai==0){window.setTimeout('cambia()',1000);}
}
 
function cambia(){
if(vai==0){$('#SlideShow').fadeOut("slow", cambia2);}
}

function cambia2(){
  l = l + 1
   if (l > (p-1)) l=0
   
$("#SlideShow").attr({ src: preLoad[l].src});
if(vai==0){$('#SlideShow').fadeIn("slow", function(){$('#SlideShow').animate({ 
       
        opacity: 1.0
      }, 3500, chiudi);
});}
}







function offerta(){ location.href = $("#off_"+o+" a").attr("href");}