﻿var pxMove = 942;
var numeroElementi = 0;
var contatore = 0;
var sleep = false;
var sleepInterna = false;
var isFromClick = false;
var timerID;
$(function() {
    if (!sleepInterna) {
        $("#slidercontenitoreOrizzontale").mouseover(function() { sleep = true });
        $("#slidercontenitoreOrizzontale").mouseout(function() {
            sleep = false;
            //timerID = setTimeout(function() { spostaSlider('sx', pxMove) }, 5000); 
        });
    }
    $('#slider_btn_sx').click(function() {
        isFromClick = true;
        spostaSlider('dx', pxMove)
    });
    $('#slider_btn_dx').click(function() {
        isFromClick = true;
        spostaSlider('sx', pxMove)
    });
    numeroElementi = $('.slider_item').length;
    $('#slidercontenitoreOrizzontale').css('width', ((numeroElementi * pxMove) + 100) + 'px');
    if (numeroElementi > 1) { timerID = setTimeout(function() { spostaSlider('sx', pxMove) }, 5000); }
})
function spostaSlider(verso, px) {
    //document.getElementById('debug').value = sleep;
    clearTimeout(timerID);
    if (((!sleep) && (numeroElementi > 1)) || (isFromClick)) {
        enablePulsantiSlider('disattiva')
        var numero=0;
        if (verso == 'sx') {
            contatore++;
            numero = - (contatore * px)
            /*numero = parseInt($('#slidercontenitoreOrizzontale').css('left')) - px*/
        } else {
            contatore--;
            numero = - (contatore * px);
        }
        if (contatore == numeroElementi) {
            numero = 0;
            contatore = 0;
        }
        if (contatore < 0) {
            contatore = numeroElementi - 1;
            numero = -((numeroElementi - 1) * px);
        }
        $('#slidercontenitoreOrizzontale').animate({ marginLeft: numero + "px" }, { duration: 1200, easing: 'easeInOutExpo', complete: function() {
            isFromClick = false; enablePulsantiSlider('attiva');
            timerID = setTimeout(function() { spostaSlider('sx', pxMove) }, 5000); 
        } });
        //$('#slidercontenitoreOrizzontale').animate({ left: numero + "px" });
    }else
        timerID = setTimeout(function() { spostaSlider('sx', pxMove) }, 5000); 
}
function enablePulsantiSlider(mode) {
    $('#slider_over_sx').css('display', mode == 'attiva' ? 'none' : 'block');
    $('#slider_over_dx').css('display', mode == 'attiva' ? 'none' : 'block');
}

