  $.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 chiusa =1;
function apri_chiudi(r) {
	
	if(chiusa==0){
		$("#pannello_offerte").animate({"top": "+=330px"}, 1000);
		$("#apri_c").attr({ src: r+"img/apri.gif" });

		
		chiusa=1;
	}else{
		$("#pannello_offerte").animate({"top": "-=330px"}, 1000);
		$("#apri_c").attr({ src: r+"img/blank.png" });

		chiusa=0;
		}
	
}



function parti() {
	conta();
	$("#loading").fadeOut("fast");
	$("#all").fadeIn("fast");
	
}


 function parti_home() {
	conta();
	$("#loading").fadeOut("fast");
	$("#testo_home").fadeTo("slow", 0.66);
	$("#all").fadeIn("fast");
	 setTimeout("offerte_home()", 150);
	 setTimeout("news_home()", 300);
	  setTimeout("foto_home()", 600);
	   setTimeout("apri_chiudi('')", 800);
	   
	    
  
  
}

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

function conta(){
	totale_offerte = $("#offerte li").length;
 	totale_news = $("#news li").length;
	totale_foto = $("#testo_home li").length;
	totale_offerte_meeting = $("#pannello_offerte_meeting li").length;
	offerte_meeting1();
}







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





var o=-1;
function offerte_home(){
	  o=o+1;
   		if(o==totale_offerte){o=0;}
		$("#offerte_"+o).fadeIn(1000);
		$("#offerte_"+o).wait(2000);
		$("#offerte_"+o).fadeOut(1000, offerte_home);
   		
  } 



var m=-1;
function offerte_meeting1(){
	  m=m+1;
   		if(m==totale_offerte_meeting){m=0;}
		
		$("#offerte_meeting_"+m).fadeIn(1000);
		$("#offerte_meeting_"+m).wait(2000);
		$("#offerte_meeting_"+m).fadeOut(1000, offerte_meeting1);
   		
  } 

 


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 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);

	
	
	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");}

$(document).ready(function() {
						   if($("#testo_home li").length>0){parti_home();}else{parti()};
   });
