jQuery(function($) {

    /**
    // Plugin carousel
    **/
    $.fn.carousel = function(params) { 
    
        // Fusionner les paramètres par défaut et ceux de l'utilisateur
        params = $.extend({
            manual: true,
            auto: false,
            speed: 500,
            frequency: 0
        }, params);
        
        // Traverser tous les noeuds.
        this.each(function() {
            var $t          = $(this);
            var $li         = $t.find('li');
            var $li_length  = $li.length;
            var $index      = 0;
            
            $t.find('li:not(:first)').hide();
            
            /* Debug
            info('Variables initiales :');
            log('Objets selectionnés : ' + $t);
            log('Eléments de listes : ' + $li);
            log('Nombre d\'éléments liste : ' + $li_length);
            log('Index : ' + $index);
            */
            
            // Autorise le déplacement manuel
            if(params.manual === true) {
                // Ajoute et gère le bouton précédent
                jQuery('<a>', {
                    'class': 'prev',
                    href: '#',
                    title: 'Précédent',
                    html: '<span>Précédent</span>',
                    click: function() {
                        carouselPrev();
                        return false;
                    }
                }).prependTo($t);
                
                // Ajoute et gère le bouton suivant
                jQuery('<a>', {
                    'class': 'next',
                    href: '#',
                    title: 'Suivant',
                    html: '<span>Suivant</span>',
                    click: function() {
                        carouselNext();
                        return false;
                    }
                }).appendTo($t);
            }
            
            // Autorise le déplacement automatique
            if(params.auto === true && $li_length > 1) {
            
                var interval;
                
                function autoSlide() {
                    interval = setInterval(function() {
                        carouselNext();
                    }, params.frequency);
                }
                autoSlide();
                
                $t.mouseenter(function(){ 
                    clearInterval(interval);
                });
                $t.mouseleave(function(){ 
                    autoSlide(); 
                });
            }
            
            // Item Précédent
            function carouselPrev() {
                if($index === 0) {
                    $index = $li_length-1;
                    $t.find('li:visible').fadeOut(params.speed).end()
                      .find('li:last-child').fadeIn(params.speed);
                } else {
                    $index--;
                    $t.find('li:visible').fadeOut(params.speed).end()
                      .find('li:eq('+ $index +')').fadeIn(params.speed);
                }
            }
            
            // Item Suivant
            function carouselNext() {
                if($index === $li_length-1) {
                    $index = 0;
                    $t.find('li:visible').fadeOut(params.speed).end()
                      .find('li:first-child').fadeIn(params.speed);
                } else {
                    $index++;
                    $t.find('li:visible').fadeOut(params.speed).end()
                      .find('li:eq('+ $index +')').fadeIn(params.speed);
                }
            }

        });
        
        // Permettre le chaînage par jQuery
        return this;
        
    };

}); /* jQuery end */
