var imgWidth;
var posLeft = 0;
var totalWidth;
var play = true;
var delay = 5000;

var currentImg = 0;
var totalImg = 0;

$(document).ready(function(){
    anim2();
    $('.banniere .event .dynamicText').bind('click', function(e){
        e.preventDefault();
        window.location = $('.banniere .event a:first').attr('href');
    })
});

function anim1(){
    imgWidth = 789
    totalWidth = $('#imagesContainer img').length * imgWidth;
    $('#imagesContainer').width(totalWidth);
    setTimeout(loopVisionneuse, delay);
}

function anim2(){
    totalImg = $('#imagesContainer img').length
    zIndexMax = $('#imagesContainer img').length+10;

    $('#imagesContainer img').css({
        'position':'absolute',
        'top':'0',
        'left':'0'
    });
    orderZIndex();

    setTimeout(loopVisionneuse2, delay);
}

function loopVisionneuse(){
    posLeft -= imgWidth;
    if(posLeft < (imgWidth - totalWidth)){
        posLeft = 0;
    }
    $('#imagesContainer').animate(
        {left: posLeft},
        1200,
        'easeInOutExpo',
        function(){
            setTimeout(loopVisionneuse, delay);
        }
    );
}

function loopVisionneuse2(){
    $('#imagesContainer img').eq(0).fadeOut('slow', function(){
        $('#imagesContainer').append($(this));
        orderZIndex();
        $(this).show();
        setTimeout(loopVisionneuse2, delay);
    })
}

function orderZIndex(){
    var i = zIndexMax;
    $('#imagesContainer img').each(function(){
        $(this).css('zIndex', i);
        i--;
    })
}
