var usascroll = false;
var PT___ = "";

function internoIN() {
    if ($("#contenuto").height() < 100) {
        $("#contenuto").animate({
            marginTop: "-200px"
        }, 500, function () {
            $("#contenuto").animate({
                height: currentH
            }, 500);
        });
        $('.piumeno').text("-");
    } else {
        $("#contenuto").animate({
            height: "1px"
        }, 800, function () {
            $("#contenuto").animate({
                marginTop: "0px"
            }, 500);
        });
        $('.piumeno').text("+");
    }
}

function test(chi) {
    alert(chi)
}

function anicont() {
    $("#contenuto").prepend('<div class="sopra"><div  class="piumeno">+</div></div>');
    $("#contenuto").show();
    if ($("#contenuto").height() < 200 && $("#testo").height() <= $("#contenuto").height()) {
        $("#contenuto").height(240);
    }
    currentH = $("#contenuto").height();
    if ($("#scheda").height() != null) {
        $("#scheda").jScrollPane({
            reinitialiseOnImageLoad: true
        });
    }
    $("#contenuto").css({
        "overflow": "hidden",
        "min-height": "1px",
        "height": "1px"
    });
    $("#contenuto").css("margin-top", "0px");
    $('.piumeno').bind({
        click: internoIN
    });
/*if (usascroll) {
        if ($("#testo").height() > 200) {
            $("#testo").css({
                "height": "300px",
                "overflow": "hidden",
				"padding-left":"10px"
            });
            $("#testo").addClass('scroll-pane');
            $("#testo").jScrollPane({
                reinitialiseOnImageLoad: true
            });
        }
    }*/
}

function allinea() {
    if ($("#Nv").height() < $("#navigation").height() - 9) {
        var mg = $("#navigation").height() - $("#Nv").height() - 9;
        $("#Nv").css("margin-top", mg + "px");
    }
    var mgLog = $("#navigation").height() - $(".logo").height();
    $(".logo").css("margin-top", mgLog + "px");
    var mgTh = $("#navigation").height() - $(".thumb").height() - 11;
    $(".thumb").css("margin-top", mgTh + "px");
}

function cloner() {
    $(".ani").each(function (index) {
        var of = $(this).offset();
        var h = $(this).height() + 1;
        var w = $(this).width();
        $("body").prepend("<div style='	background-color:#FFF;z-index:" + (3000) + ";position:absolute;margin-left:" + (of.left) + "px;" + " margin-top:" + ((of.top)) + "px" + "' id=\"ghost" + index + "\"><img src='" + PT___ + "imgs/px.png'  width='" + w + "' height='" + h + "'  /></div");
        $("#ghost" + index).width(w);
        $("#ghost" + index).height(h);
    });
    $("#navigation").css("z-index", "3500");
    $("#ghost1").css("z-index", "3600");
    $("#ghost2").css("z-index", "3600");
    Ani_Logo();
}

function Ani_Logo() {
    // $(".ani").css("display","none");
    $(".ani").css("visibility", "visible");
    $(".ani li").css("visibility", "visible");
    $("#ghost1").fadeTo("slow", 0, Ani_Ball);
}

function Ani_Footer() {
	
	  $("#family a").each(function(index) {

	
	 if(index>0){
		
		
		//lg+=$(this).
		
		$(this).css("margin-left",  Math.ceil( ($("#family").width() - ( ($("#family a ").size())*100) )  / ($("#family a ").size()-1))-10+"px"  )
		
		 }
	
	
	
	
  });

	
	
    $("#family a img").fadeTo("slow", .36);
    $("#family a img").bind({
        mouseleave: function () {
            $(this).fadeTo("slow", 0.33);
        },
        mouseenter: function () {
            //alert($(this))
            $(this).fadeTo("slow", 1);
        }
    });
}

function Ani_Ball() {
    var of = $(".logo").offset();
    var t = of.top;
    var lt = of.left;
    if ($.flash.available || $.flash.available!=null) {
        $("body").prepend("<div id='introflash' style='position:absolute;z-index:10000;width:99%;height:98%';overflow:hidden></div>");
        $('#introflash').flash({
            swf: PT___ + 'imgs/palla.swf',
            height: $('#introflash').height(),
            width: $('#introflash').width(),
            params: {
                wmode: "transparent"
            },
            flashvars: {
                "px": 195 + lt
            }
        });
    } else {
        var bezier_params = {
            start: {
                x: 0,
                // $(window).width()/3, 
                y: 0,
                //$(window).height() - 200, 
                angle: 20
            },
            end: {
                x: $(window).width() - 250,
                //(lt+188),
                y: $(window).height() - 250,
                angle: -80,
                length: 0.25
            }
        }
        $("#myElement").animate({
            path: new $.path.bezier(bezier_params),
            width: "80px",
            height: "80px",
            easing: "easein"
        }, 1400, togli);
        Ani_Ball2()
    }
}

function Ani_Ball2() {
    var of = $(".logo").offset();
    t = of.top;
    lt = of.left;
    var bezier_params = {
        start: {
            x: $(window).width() - 250,
            //(lt+188),
            y: $(window).height() - 250,
            angle: 80
        },
        end: {
            x: (lt + 188),
            y: t,
            angle: -50,
            length: 0.25
        }
    }
    $("#myElement2").delay(1370);
    $("#myElement2").animate({
        path: new $.path.bezier(bezier_params),
        width: "12px",
        height: "12px",
        easing: "easeout"
    }, 1000, Ani_Site);
}

function togli() {
    $(this).remove();
}

function Ani_Site() {
    $(".logo img").attr('src', PT___ + "imgs/logo_full.png");
    $("#myElement2").remove();
    $("#ghost3").fadeTo("slow", 0, togli);
    $("#ghost4").delay(600).fadeTo("slow", 0, togli);
    $("#ghost5").delay(800).fadeTo("slow", 0, togli);
    $("#ghost6").delay(1000).fadeTo("slow", 0, togli);
    $("#ghost0").delay(1200).fadeTo("slow", 0, togli);
    $("#ghost2").delay(1400).fadeTo("slow", 0, togli);
    $("#ghost1").remove();
    if (($('#introflash').length)) {
        $("#introflash").remove();
    }
    //if (($('#routes img').length)) {
    Ani_Image()
    Ani_Footer();
}

function Ani_Image() {
    // $('#pic').append($("#contenuto"))
    $('#slideshow').cycle({
        fx: 'fade',
        timeout: 4000,
        before: onBefore,
		 after:   checkdesc
    });
}
var currentImageNum = 0;
var currentDescNum = 0;
function onBefore(curr, next, opts) {
 $("div.descrizione").fadeTo(200,0)
   // on the first pass, addSlide is undefined (plugin hasn't yet created the fn); 
    // when we're finshed adding slides we'll null it out again 
    if (!opts.addSlide)  return;
    // on Before arguments: 
    //  curr == DOM element for the slide that is currently being displayed 
    //  next == DOM element for the slide that is about to be displayed 
    //  opts == slideshow options 
    //var currentImageNum= parseInt(next.src.match(/pic_(\d)/)[1]); 	
    var corrige = 0;
	
	
    currentImageNum++; 
	
   
    if (currentImageNum == arImg.length - 1) {
        // final slide in our slide slideshow is about to be displayed 
        // so there are no more to fetch 
        opts.addSlide = null;
        currentImageNum = 0;
		// currentDescNum=0;
		 
		//checkdesc(currentImageNum+2);
        return;
    }

var in_='';
var out_='';
    // add our next slide 
    //alert(arImg[currentImageNum])
	
	if($(".ltbx").size()>0){
		var in_='<a  class="ltbx"  href="'+ gallery[Math.ceil(Math.random()*gallery.length-1)] +'">';
		var out_='</a>';
		
		}
	
    opts.addSlide(in_+'<img width="975"  height="243"  src="' + (arImg[currentImageNum + 1]) + '" id="pic' + (currentImageNum + 1).toString() + '"    />'+ out_);
   
    
	//$("#pic"+currentImageNum+1).css({'margin-top': corrige+'px'});
	
	
	
	
    //alert( corrige)
}
// JavaScript Document


function checkdesc(){
	
	
	
	
	 if((posizioni[currentDescNum ])!=''){
	   
	   
	          $("div.descrizione").fadeTo(700,1)
		     $("div.descrizione").text(posizioni[currentDescNum ]);
		   
		   
	  
	   
	   
	   }else{
		  
		  
		         $("div.descrizione").fadeTo(300,0)
		  // $("div.descrizione").text("");
			   
	
		   
		   
		   }
   
   
   currentDescNum ++;
   
  // alert(currentDescNum)
   if (currentDescNum == arImg.length ) {
		
		currentDescNum  =0
		}
	
   
	
	} 
