﻿
$(function() {
    $("#Tabs a").click(function() {
        $("#Tabs a").removeClass("selezionato");
        $(this).addClass("selezionato");

        $(".testoInterna div").hide();
        $(".testoInterna div:eq(" + $("#Tabs li a").index(this) + ")").show();
    });
    
    $('#pulsanteAttivaGallery').click(function(){
        $('#pulsanteAttivaGallery').fadeOut();
        $('#boxMiniatureGallery').animate({bottom: 0}, 1000);
    });
    
    $('#rotatorOfferte').cycle('fade');
});

$.fn.archieRotator = function(n, s, t) {
    if (!$.isReady) {
        log('DOM not ready, queuing slideshow');
        return;        
    }

    var elTimer = null;    
    var puntatore = 0;
    var elRotator = this;
    var widthRotator = elRotator.width();
    var arrElFiglio = elRotator.children();
    var numFigli = arrElFiglio.size();
    elRotator.children().css("position", "absolute");
    var heightEl = elRotator.children(0).height();
    var widthEl = 250;
    var boolParteAnimazione = (((widthRotator / numFigli) < widthEl)? true : false);
    
    if(arrElFiglio.size() < 1){
        log('The number of element is 0');
        return;
    }
    
    var numElShow = n;
    if (numElShow + "" == "" || numElShow == 1){
        numElShow = 3;
    }
    
    var speed = s;
    if (speed + "" == ""){
        speed = 3000;
    }
    
    var timeAnimation = t;
    if (timeAnimation + "" == ""){
        timeAnimation = 800;
    }
    
    if(boolParteAnimazione){
        elRotator.stop(true,true).hover(function(){
            stopRotator();
        },
        function(){
            startRotator();
        });
    }
    
    inizializzaPosizioni();
    
    if(boolParteAnimazione){
        startRotator();
    }
    
    //funzioni
    function inizializzaPosizioni(){
        elRotator.css("position","relative");
        var elTemp = null;
        $.each(arrElFiglio, function(index, el){
            elTemp = $(el);            
            elTemp.height(heightEl);
            elTemp.css({'top': 0, 'left': ((widthEl) * index)});
        });
    }
    
    function startRotator(){
        elTimer = setTimeout(function() { nextElDxRotator(); }, 3000);
    }
    
    function stopRotator(){
        clearTimeout(elTimer);
    }
    
    function nextElDxRotator(){
        if(puntatore <= (-1 * numFigli)){
            puntatore = -1;
        }else{
            puntatore--;
        }
        
        elTimer = setTimeout(function() { nextElDxRotator(); }, 3000);
        
        var elTemp = null;
        var indexSposato = 0;
        $.each(arrElFiglio, function(index, el){
            indexSposato = puntatore + index;
            elTemp = $(el);
            
            if(indexSposato < -1){
                elTemp.animate({'left': (widthEl * (numFigli + indexSposato))}, 1000);
            }else{
                if(indexSposato == -1){
                        elTemp.animate({'left': (widthEl * indexSposato)}, 1000 , function(){
                        $(this).css({'left': (widthEl * (numFigli-1))});
                    });
                }else{
                    elTemp.animate({'left': (widthEl * indexSposato)}, 1000);
                }
            }
        });
        
    }
}
