jQuery(function($) {

    /**
    // Plugin carousel
    **/
    $.fn.carousel = function(params) { 
    
        // Fusionner les parametres par defaut et ceux de l'utilisateur
        params = $.extend({
            manual: true,
            auto: false,
            navigation: false,
            speed: 500,
            frequency: 0
        }, params);
        
        // Traverser tous les noeuds.
        this.each(function() {
            var $t          = $(this);
            var $ul         = $t.find('ul');
            var $li         = $t.find('li');
            var $li_length  = $li.length;
            var $index      = 0;
            
            // Autorise le deplacement manuel
            if(params.manual === true) {
                // Ajoute et gere le bouton precedent
                jQuery('<a>', {
                    className: 'prev',
                    href: '#',
                    text: 'Previous',
                    click: function() {
                        carouselPrev();
                        return false;
                    }
                }).prependTo($t);
                
                // Ajoute et gere le bouton suivant
                jQuery('<a>', {
                    className: 'next',
                    href: '#',
                    text: 'Next',
                    click: function() {
                        carouselNext();
                        return false;
                    }
                }).appendTo($t);
            }
            
            // Autorise le deplacement automatique
            if(params.auto === true) {
            
                var interval;
                
                function autoSlide() {
                    interval = setInterval(function() {
                        carouselNext();
                    }, params.frequency);
                }
                autoSlide();
                
                $t.mouseenter(function(){ 
                    clearInterval(interval);
                });
                $t.mouseleave(function(){ 
                    autoSlide(); 
                });
            }
            
            // Item Precedent
            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:first-child img').attr('src', $t.find('li:first-child img').attr('src'));
                    $t.find('li:visible').fadeOut(params.speed).end()
                      .find('li:first-child').fadeIn(params.speed, function() {
                        if(params.navigation === true) {
                            var $navigation = $t.find('.navCarousel');
                            var $classLi = $t.find('li:first-child').attr('class');                       
                            $navigation.find('.on').removeClass('on').addClass('off').find('img').attr('width', 60).attr('height', 40);
                            $navigation.find('#' + $classLi).removeClass('off').addClass('on').find('img').attr('width', 90).attr('height', 60);
                        }
                      });
                    
                } else {
                    $index++;
                    $t.find('li:eq('+ $index +') img').attr('src', $t.find('li:eq('+ $index +') img').attr('src'));
                    $t.find('li:visible').fadeOut(params.speed).end()
                      .find('li:eq('+ $index +')').fadeIn(params.speed, function() {
                        if(params.navigation === true) {
                            var $navigation = $t.find('.navCarousel');
                            var $classLi = $t.find('li:eq('+ $index +')').attr('class');                        
                            $navigation.find('.on').removeClass('on').addClass('off').find('img').attr('width', 60).attr('height', 40);
                            $navigation.find('#' + $classLi).removeClass('off').addClass('on').find('img').attr('width', 90).attr('height', 60);
                        }
                      });
                }
            }

        });
        
        // Permettre le chainage par jQuery
        return this;
        
    };

}); /* jQuery end */
