﻿//lightbox jquery
$(document).ready(function() {
    $('#immagineSfondoGrande').load(function(){$('#immagineCaricamento').fadeOut(800);});

    $("#boxMiniatureGallery .carrelloMiniatureGallery").css("left", 0)
    var elemRot = $("#boxMiniatureGallery .imgGalleryPagina");
    var countElemRot = elemRot.size();
    var modalitaGallery = false;
    var spostaCarrello = false;
    var dentroGallery = false;
    
    var hWindow = $(window).height();
    var wWindow = $(window).width();
    jQuery.event.add(window, "resize", resizeFrame);
    
    function resizeFrame() {
        hWindow = $(window).height();
        wWindow = $(window).width();
        
        //if(hWindow < 600) aumeta % del body, form, div in modo da non sovrapporre nulla
        
        //$('#test2').html('h: '+ hWindow +', w: '+wWindow);
    }
    
    var xSpostamento = 0;
    
    
    $(document).mousemove(function(e){
       xSpostamento = e.pageX - wWindow/2;
       //$('#test3').html(xSpostamento);
       //$('#test').html(e.pageX +', '+ e.pageY);
    });
    
    
    $("#boxMiniatureGallery .carrelloMiniatureGallery").css("width", (131 * countElemRot));
    
    $("#boxMiniatureGallery .linkGalleryPagina, .linkGalleryCamere").click(function (){
        if(!modalitaGallery){
            entraModalitaGallery();        
            modalitaGallery = true;
        }
        $('#immagineCaricamento').show();
        stoppaSpostamento();
        cambiaSfondo($(this).attr("rel"))
    });
    
    $("#pulsanteEsciGallery").click(function (){
        esciModalitaGallery();
        modalitaGallery = false;
    });
    
    function entraModalitaGallery(){
        $("#pulsanteEsciGallery").fadeIn();
        $("#contenutoDinamico, #boxMenuPrincipale, #boxMenuSecondario, #logo-villa-di-campo").fadeOut(1000);
    }
    
    function esciModalitaGallery(){
        $("#pulsanteEsciGallery").fadeOut();
        $("#contenutoDinamico, #boxMenuPrincipale, #boxMenuSecondario, #logo-villa-di-campo").fadeIn(1000);
    }
    
    function cambiaSfondo(s){
        $("#immagineSfondoGrande").attr("src", s);
    }
    
    
    /* rotazione miniature */
    
    $("#boxMiniatureGallery").hover(function(){
        spostaCarrello = true;
        dentroGallery = true;
        cicloSpostamento();
        //alert('in');
    },function(){
        dentroGallery = false;
        spostaCarrello = false;
        //alert('out');
    });

    var count = 0;
    function cicloSpostamento(){
        count++;
        var oldLeftCarrello = $("#boxMiniatureGallery .carrelloMiniatureGallery").css("left");
        var nuovaLeftCarrello = (parseInt(oldLeftCarrello.replace("px","")) - xSpostamento/80);
        //    alert(oldLeftCarrello +" - "+ );
        if(xSpostamento < 150 && xSpostamento > -150){
            setTimeout(function () {cicloSpostamento();},200)
            return;
        }
        
        if(nuovaLeftCarrello>0) {
            setTimeout(function () {cicloSpostamento();},200)
            $("#boxMiniatureGallery .carrelloMiniatureGallery").css("left", 0);
            return;
        }
        
        if(nuovaLeftCarrello < ((-131 * countElemRot) + wWindow)) {
            setTimeout(function () {cicloSpostamento();},200)
            $("#boxMiniatureGallery .carrelloMiniatureGallery").css("left", ((-131 * countElemRot) + wWindow));
            return;
        }
    
//        $('#test4').html(oldLeftCarrello);
        if(spostaCarrello){
            //$('#test4').html(nuovaLeftCarrello + " - " + count + " -" + spostaCarrello);
            $("#boxMiniatureGallery .carrelloMiniatureGallery").css("left", nuovaLeftCarrello);
            setTimeout(function () {cicloSpostamento();}, 20);
        }
    }
    
    function stoppaSpostamento(){
        spostaCarrello = false;
        setTimeout(function(){
            if(dentroGallery) spostaCarrello = true;
            cicloSpostamento();}, 2000);
    }
});

/* menu JL */
/*$(document).ready(function() {

    var timerMenu = null;
    var elemntoSelezionato = null;
    
    $(".menuPrincipale li").hover(function(){
        clearTimeout(timerMenu);
        elemntoSelezionato = $(".menuPrincipale_liv2 li.selezionato");
        $(this).addClass("hover");
        if(!$(this).hasClass("selezionato")) $(".selezionato .menuPrincipale_liv2").stop(true, true).fadeOut();
        $(this).children(".menuPrincipale_liv2").stop(true, true).fadeIn();
    },function(){
        $(this).removeClass("hover");
        if(!$(this).hasClass("selezionato") || !$(this).parent(".menuPrincipale li").hasClass("selezionato") || !$(this).parent(".menuPrincipale li").hasClass("hover")){
            $(this).children(".menuPrincipale_liv2").stop(true, true).fadeOut();
            
            timerMenu = setTimeout(function(){
                $(".selezionato .menuPrincipale_liv2").stop(true, true).fadeIn();
            }, 1500);
        }
    });
});*/

$(document).ready(function() {

    var timerMenu = null;
    var elemntoSelezionato = null;
    
    $(".menuPrincipale > li.selezionato").children(".menuPrincipale_liv2").fadeOut(1500);
    
    $(".menuPrincipale > li").hover(function(){
        if(!$(this).hasClass("hover")){
            $(this).children(".menuPrincipale_liv2").fadeIn();
            $(this).addClass("hover");
        }
    },function(){
        $(this).removeClass("hover");
        if(!$(this).hasClass("hover")){
            $(".menuPrincipale_liv2").hide();
        }
    });
});
