$(function(){
    setIndexes();
    animate(0,$('#flash img').length);
});

function setIndexes(){
    count = $('#flash img').length;
    $('#flash img').each(function(){
        $(this).css("z-index",count--);
    });
}

function animate(index,count){
    if(index + 1 == count){
        $('#flash img:eq(' + index + ')').css("z-index",count + 1);
        $('#flash img:eq(0)').fadeIn(0,function(){
            $('#flash img:eq(' + index + ')').fadeOut(1500,function(){
                $('#flash img:eq(' + index + ')').css("z-index",1);
                setTimeout(
                    'animate(0,' + count + ')'
                    ,2000
                );
            });
        });
    } else {
        $('#flash img:eq(' + (index + 1) + ')').fadeIn(0,function(){
            $('#flash img:eq(' + (index++) + ')').fadeOut(1500,function(){
                setTimeout(
                    'animate(' + index + ',' + count + ')'
                    ,2000
                );
            });
        });
    }
}