$(document).ready(function() {
    $('.scroll-pane').jScrollPane({autoReinitialise: true});
    /* diaporama accueil */
    var $diapo = $('#accueil_images');
    var $elected = $diapo.find('li:eq(2)');
    var $itemLeft2 = $diapo.find('li:eq(0)')
    var $itemLeft = $diapo.children('li:eq(1)') ;
    var $itemRight = $diapo.children('li:eq(3)') ;
    var $itemRight2 = $diapo.children('li:eq(4)');
    var $nextItem = $diapo.children('li:eq(5)');
    var $otherItems = $diapo.children('li:gt(4)');
    var $lastItem = $diapo.children('li:last-child');
    var isIE6 = $.browser.msie && /^6/.test($.browser.version);

    $elected.addClass('selected');
    $itemLeft.removeClass().addClass('left');
    $itemLeft2.removeClass().addClass('left2');
    $itemRight.removeClass().addClass('right');
    $itemRight2.removeClass().addClass('right2');
    $otherItems.removeClass().hide();
    /**************************************************************/

    $('.snippet_last_news.template1 ul.diapo').each(function() {
        var
            $list = $(this),
            $container = $list.closest('.template1'),
            length = $list.children('li').each(function(i) {
                $(this).data('element_index', i);
            }).length,
            $current = $container.find('span.current'),
            $total = $current.siblings('span.total')
        ;
        
        $total.html(length);
        
        $list.carouFredSel({
            width: 298,
            height: 298,
            padding: [0, 0, 0, 0],
            circular: true,
            infinite: false,
            auto: false,
            prev: {
                    button: $container.find('a.btn_left')
            },
            next: {
                button: $container.find('a.btn_right')
            },
            scroll: {
                duration: 500,
                onBefore: function(oldItems, newItems) {
                    $current.html($(newItems[0]).data('element_index') + 1);
                }
            }
        });
    });
    
    $('.snippet_last_news.template2 ul.diapo').carouFredSel({
        width: 218,
        height: 408,
        padding: [0, 0, 0, 0],
        scroll: {
            duration: 500
        },
        circular: true,
        infinite: false,
        auto: false,
        prev: {
                button: 'a.btn_left'
        },
            next: {
                button: 'a.btn_right'
            }
    });

});

$('.news_choices > li > a').live("click", function()  {
    var link = $(this);
    
    // Si clic sur l'élément actif, on ne fait rien
    if (!(link.parent('li').hasClass('active')))
    { 
        var infos = link.parent('li').attr('class').split("_");
        var id = infos[2];
        
        $('.news_choices').find(".active").removeClass('active');
        link.parent('li').addClass('active');
        
        $('.news_list').find(".active").removeClass('active');
        $('.news_list').find(".news_item_"+id).addClass('active');
    }
});

var myTimer = setInterval(function() {
    AnimateNext();
},4000);


$('.btn_next').live('click', function(){
    clearTimeout(myTimer);
    AnimateNext();
    return false;
 });

 $('.btn_prev').live('click', function(){

        clearTimeout(myTimer);

        var $diapo = $('#accueil_images');
        var $elected = $diapo.find('li:eq(2)');
        var $itemLeft2 = $diapo.find('li:eq(0)')
        var $itemLeft = $diapo.children('li:eq(1)') ;
        var $itemRight = $diapo.children('li:eq(3)') ;
        var $itemRight2 = $diapo.children('li:eq(4)');
        var $nextItem = $diapo.children('li:eq(5)');
        var $otherItems = $diapo.children('li:gt(4)');
        var $lastItem = $diapo.children('li:last-child');
        var isIE6 = $.browser.msie && /^6/.test($.browser.version);

        $('#accueil_images li').stop(true,true);

        $lastItem.children('a').css({ height: '230px', marginTop: '-60px', top: '0px' });
        
        $itemLeft2.addClass('left').animate({ left: '115px', opacity:'1', top: '20px' },500);
        $itemLeft2.children('a').animate({ height: '250px', marginTop: '-40px' });
       

        $lastItem.addClass('left2').css({ display: 'block', left: '-520px', top: '40px' }).animate({ left: '0px', opacity: '0.5', filter: '' }, 500, function(){
            var $newItem = $('<li><a href="' + $(this).find('a').attr('href') + '"><img src="'+ $(this).find('img').attr('src') +'" /></a></li>');
            $(this).remove();
            if ( isIE6 ){
            var dynId = 'i_' + (+new Date);
            $newItem.find('img').attr('id',dynId);
            DD_belatedPNG.fix('#' + dynId);
            }
            $newItem.insertBefore($itemLeft2).addClass('left2');
        });

        $itemLeft.addClass('over_the_top');
        $itemLeft.delay(100).animate({ left: '230px', top: '0px' }, 500, function(){
            $(this).removeClass().addClass('selected');
        });

        $itemLeft.children('a').delay(100).animate({ height: '270px', marginTop: '-20px' }, 500);

        $elected.addClass('under_the_top right');

        $elected.delay(200).animate({ left: '345px', top: '20px' }, 500, function(){
            $(this).removeClass('under_the_top, selected');
        });
        $elected.children('a').delay(200).animate({ height: '250px', marginTop: '-40px' }, 500);
        $itemRight.delay(300).animate({ left: '465px', opacity: '0.5', top: '40px' },500);
            $itemRight.children('a').animate({ height: '230px', marginTop: '-60px' });
            $itemRight.removeClass().addClass('right2');
        
        $itemRight2.delay(400).animate({ left: '960px', opacity: '0' }, 1000, function(){
            $(this).removeClass().hide();
        });

        return false;

});

function AnimateNext() {
    var $diapo = $('#accueil_images');
    var $elected = $diapo.find('li:eq(2)');
    var $itemLeft2 = $diapo.find('li:eq(0)')
    var $itemLeft = $diapo.children('li:eq(1)') ;
    var $itemRight = $diapo.children('li:eq(3)') ;
    var $itemRight2 = $diapo.children('li:eq(4)');
    var $nextItem = $diapo.children('li:eq(5)');
    var $otherItems = $diapo.children('li:gt(4)');
    var $lastItem = $diapo.children('li:last-child');
    var isIE6 = $.browser.msie && /^6/.test($.browser.version);

    $('#accueil_images').stop();
    $itemLeft2.animate ({ left: '-520px', opacity: '0' }, 500, function(){
        var $newItem = $('<li><a href="' + $(this).find('a').attr('href') + '"><img src="'+ $(this).find('img').attr('src') +'" /></a></li>');
        $(this).remove();
        if ( isIE6 ){
        var dynId = 'i_' + (+new Date);
        $newItem.find('img').attr('id',dynId);
        DD_belatedPNG.fix('#' + dynId);
        }
        $newItem.insertAfter($lastItem).hide();
    });
    $itemLeft.animate({ left: '0px', opacity: '0.5', top: '40px' },500, function(){       
        $(this).parent('li').removeClass().addClass('left2');
    });
    $itemLeft.children('a').animate({ height: '230px', marginTop: '-60px' });
    $elected.addClass('under_the_top left');

    $elected.delay(100).animate({ left: '115px', top: '20px' },500, function(){
        $(this).removeClass().addClass('left');
    });
    $elected.children('a').delay(100).animate({ height: '250px', marginTop: '-40px' }, 500);

    $itemRight.addClass('over_the_top');
    $itemRight.delay(200).animate({ left: '230px', top: '0px' }, 500, function(){
        $(this).removeClass().addClass('selected');
    });
    $itemRight.children('a').delay(200).animate({ height: '270px', marginTop: '-20px' }, 500);

    $itemRight2.addClass('under_the_top').animate({ left: '345px', opacity: '1', top: '20px' },500, function(){
        $(this).removeClass().addClass('right');       
    });
           $itemRight2.children('a').animate({ height: '250px', marginTop: '-40px' });

    $nextItem.children('a').css({ height: '230px', marginTop: '-60px', top: '0px' });
    $nextItem.addClass('right2').css({ display: 'block', left: '960px', top:'40px' }).delay(300).animate({ left: '465px', opacity: '0.5' }, 500);
}


