/*
**  Utilisation de la librairie jQuery
**  http://docs.jquery.com/Downloading_jQuery#Download_jQuery
*/
/**
* Carousel à l'infini
* @author Aline Binet 
* BleuRoy.com
* @version 1.0

*/



//PLUGIN CAROUSEL A L INFINI

jQuery(function($) {

    $.fn.carouselInfini = function(params) {
    
        //Fusionne les paramètres par défaut avec ceux de l'utilisateur (grace à $.extend()
        params = $.extend({
            auto: false,
            manual: true,
            speed: 1000,
            frequency: 1000,
            itemVisibles: 1
        }, params);
        
        // Traverse tous les noeuds
        this.each( function() {
            var $this = $(this);
            var $masque = $this.find('.masque');
            var $ul = $this.find('ul');
            var $li = $ul.find('li');
            var $liWidth = $li.width();
            var $liHeight = $li.height();
            var $liLength = $li.length;
            var $animActive = false;
            var $margeGoPrev = 0;
            var $margeResetPrev = $margeGoPrev - ($liWidth * params.itemVisibles);
            var $margeGoNext = -($liWidth * params.itemVisibles);
            var $margeResetNext = $margeGoNext + ($liWidth * params.itemVisibles);
            
            $ul.css('width', $liWidth * $li.length);
            $masque.css({ 
                'width' : $liWidth * params.itemVisibles,
                'height' : $liHeight
            });
            
            // Autorise le déplacement manuel
            if(params.manual == true) {
                
                //Ajoute un bouton précédent
                jQuery('<a>', {
                    className: 'prev',
                    href: '#',
                    alt: 'Précédent',
                    click: function() {
                        carouselPrev();
                        return false;
                    }
                }).prependTo($this);
                
                //Ajoute un bouton Suivant
                jQuery('<a>', {
                    className: 'next',
                    href: '#',
                    alt: 'Suivant',
                    click: function() {
                        carouselNext();
                        return false;
                    }
                }).appendTo($this);
            }
            
            if(params.auto == true) {
                
                var interval;
                $animActive == true;
                
                function autoSlide() {
                    interval = setInterval(function() {
                        carouselNext();
                    }, params.frequency);
                }
                autoSlide();
                
                $this.mouseenter(function(){ 
                    clearInterval(interval);
                });
                $this.mouseleave(function(){ 
                    autoSlide(); 
                });
                
            }
            
            function carouselPrev() {
                var $liLast = $ul.find('li:gt('+(($liLength - 1) - params.itemVisibles)+')');                
                
                if($animActive === false) {                
                    $liLast.clone().prependTo($ul);
                    $ul.css('marginLeft', $margeResetPrev);
                    $animActive = true;
                    $ul.animate({marginLeft: $margeGoPrev}, params.speed, function() {
                        $liLast.remove();
                        $animActive = false;
                    });
                }
                return false;                
            }
            
            function carouselNext() {
                var $liFirst = $ul.find('li:lt('+params.itemVisibles+')');
                
                if($animActive === false) {
                    $liFirst.clone().appendTo($ul);                
                    $animActive = true;               
                    $ul.animate({marginLeft: $margeGoNext}, params.speed, function() {
                        $ul.css('marginLeft', $margeResetNext);
                        $liFirst.remove();            
                        $animActive = false;
                    });
                }
                return false;
            }
        
        });
    
        // Permettre le chaînage par jQuery
        return this;
    
    };  
});
 /* fin jQuery */
