﻿var pxMove = 828;
var numeroElementi = 0;
var contatore = 0;
var timerID;
var sleep = false;
var sleepInterna = false;
var isFromClick = false;
$(function() {
    if (!sleepInterna) {
        $("#sliderGen").mouseover(function() { sleep = true });
        $("#sliderGen").mouseout(function() { sleep = false });
    }
    $('#clickLeft').click(function() {
        isFromClick = true;
        spostaSlider('dx', pxMove)
    });
    $('#clickRight').click(function() {
        isFromClick = true;
        spostaSlider('sx', pxMove)
    });
    numeroElementi = $('.slider').length;
    $('#sliderGen').css('width', ((numeroElementi * pxMove) + 100) + 'px');
    timerID = setInterval(function() { spostaSlider('sx', pxMove) }, 5000);
})
function spostaSlider(verso, px) {
    //document.getElementById('debug').value = sleep;
    if (((!sleep)&&(numeroElementi > 1))||(isFromClick)) {
        clearInterval(timerID);
        enablePulsantiSlider('disattiva')
        var numero;
        if (verso == 'sx') {
            contatore++;
            numero = parseInt($('#sliderGen').css('left')) - px
        } else {
            numero = parseInt($('#sliderGen').css('left')) + px
            contatore--;
        }
        //alert(contatore);
        if (contatore == numeroElementi) {
            numero = 0;
            contatore = 0;
        }
        if (contatore < 0) {
            contatore = numeroElementi - 1;
            numero = -((numeroElementi - 1) * px);
        }
        $('#sliderGen').animate({ left: numero + "px" }, { duration: 1200, easing: 'easeInOutExpo', complete: function() { isFromClick = false; enablePulsantiSlider('attiva'); timerID = setInterval(function() { spostaSlider('sx', pxMove) }, 5000); } });
    }
}
function enablePulsantiSlider(mode) {
    $('.disabler').css('display', mode == 'attiva' ? 'none' : 'block');
}

