﻿var pxMoveBottom = 450;
var numeroElementiSX = 0;
var numeroElementiDX = 0;
var contatoreSX = 0;
var contatoreDX = 0;
var timerIDSX;
var timerIDDX;
var elVisibili = 3;
$(function() {
    var tmpElSX = parseInt($('.img_sponsor_div1').length);
    numeroElementiSX = parseInt(tmpElSX / elVisibili);
    if (tmpElSX % elVisibili != 0) numeroElementiSX += 1;
    contatoreSX = numeroElementiSX;
    $('#tableSponsorSX').css('margin-left', '-' + (pxMoveBottom * (numeroElementiSX - 1)) + 'px');
    var tmpElDX = parseInt($('.img_sponsor_div2').length);
    numeroElementiDX = parseInt(tmpElDX / elVisibili);
    if (tmpElDX % elVisibili != 0) numeroElementiDX += 1;
    if (numeroElementiSX > 1) { setTimeout(function() { timerIDSX = spostaSponsor('tableSponsorSX', pxMoveBottom, numeroElementiSX) }, 5000); }
    if (numeroElementiDX > 1) { setTimeout(function() { timerIDDX = spostaSponsor('tableSponsorDX', pxMoveBottom, numeroElementiDX) }, 5000); }
})
function spostaSponsor(id, px, nElem) {
    var n = 0;
    var ct;
    if (id == 'tableSponsorSX') {
        clearTimeout(timerIDSX);
        contatoreSX--;
        ct = contatoreSX;
        n = - ((contatoreSX-1) * px);
        if (contatoreSX == 0) {
            n = - (pxMoveBottom * (numeroElementiSX - 1));
            contatoreSX = numeroElementiSX;
        }
    } else {
        clearTimeout(timerIDDX);
        contatoreDX++;
        ct = contatoreDX;
        n = -(contatoreDX * px);
        if (contatoreDX == nElem) {
            n = 0;
            contatoreDX = 0;
        }
    } 
    $('#' + id).animate({ marginLeft: n + "px" }, { duration: 1200, easing: 'easeInOutExpo', complete: function() {
        if (id == 'tableSponsorSX') {
            timerIDSX = setTimeout(function() { spostaSponsor(id, px, nElem) }, 5000);
        } else {
            timerIDDX = setTimeout(function() { spostaSponsor(id, px, nElem) }, 5000);
        }
    }
    });
}

