﻿(function() {


    $.fn.ListeGratuitUne = function() {
        function repeat(str, n) {
            return new Array(n + 1).join(str);
        }

        return this.each(function() {
            // magic!
            var $wrapper = $('> div', this).css('overflow', 'hidden'),
                $slider = $wrapper.find('> ul').width(9999),
                $items = $slider.find('> li'),
                $single = $items.filter(':first')

            singleWidth = $single.outerWidth(),
                visible = Math.ceil($wrapper.innerWidth() / singleWidth),
                currentPage = 1,
				nbPageTotal = Math.ceil($items.length / visible),
                pages = Math.ceil($items.length / visible);

            /* TASKS */

            // 1. pad the pages with empty element if required
            if ($items.length % visible != 0) {
                // pad
                $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
                $items = $slider.find('> li');
            }

            // 2. create the carousel padding on left and right (cloned)
            $items.filter(':first').before($items.slice(-visible).clone().addClass('cloned'));
            $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
            $items = $slider.find('> li');
            var start = true;
            var fin = false;

            // 3. reset scroll
            $wrapper.scrollLeft(singleWidth * visible);

            var reset = false;









            // 4. Changement de page
            function gotoPage(page) {
                start = false;
                var pageTermine = false;

                //Pour retour au debut si on arrive à la fin du slider
                if (fin && (page == 1 || page == pages + 1)) 
                {
                    fin = false;
                    gotoPage(currentPage - ((pages - 1)));
                }


                var dir = page < currentPage ? -1 : 1,
                    n = Math.abs(currentPage - page),
                    left = singleWidth * dir * visible * n;




                $wrapper.filter(':not(:animated)').animate({
                    scrollLeft: '+=' + left
                }, 1000, function() {
                    //if page == last page - then reset position
                    if (page > pages) {
                        $wrapper.scrollLeft(singleWidth * visible);
                        page = 1;
                    } else if (page == 0) {
                        page = pages;
                        $wrapper.scrollLeft(singleWidth * visible * pages);
                    }

                    currentPage = page;
                    detecteDebut();
                    detecteFin();

                });


            }


            
            function detecteDebut() {

                fin = false;
                if (currentPage == 1) {

                    //Affichage de la fleche droite
                    afficherFlecheDroit();
                    masquerFlecheGauche();

                }
                else {
                    if (currentPage != nbPageTotal) {
                        afficherFlecheDroit();
                    }
                    afficherFlecheGauche();
                }


            }

            function detecteFin() {

                fin = false;
                if (currentPage == nbPageTotal) {
                    //alert("masqueDroit");
                    masquerFlecheDroit();
                    afficherFlecheGauche();
                    fin = true;
                }
                else {
                    if (currentPage != 1) {
                        afficherFlecheGauche();
                    }
                    afficherFlecheDroit();
                }
            }

            function masquerFlecheGauche() {

                //$('#flechegauche').hide();
                $('#flechegauche').addClass("backNo");
                $('.back').hide();

            }
            
            function masquerFlecheDroit() {
                //$('#flechedroit').hide();

                //$('.forward').removeClass("arrow").removeClass('forward').addClass("forwardNo");
                $('#flechedroit').addClass("forwardNo");
                $('.forward').hide();
            }

            function afficherFlecheDroit() {
                //$('#flechedroit').show();
                //$('.forwardNo').addClass("arrow").addClass('forward').removeClass("forwardNo");
                $('#flechedroit').removeClass("forwardNo");
                $('.forward').show();

            }
            
            function afficherFlecheGauche() {
                //$('#flechegauche').show();
                //$('.backNo').addClass("arrow").addClass('back').removeClass("backNo");
                $('#flechegauche').removeClass("backNo");
                $('.back').show();

            }



            // 5. Ajout des liens avant et arriere
           $wrapper.after('<div id="flechegauche"><a href="#" class="arrow back"></a></div><div id="flechedroit"><a href="#" class="arrow forward"></a></div>');
           $wrapper.after('<div class="Zonegratuit"><a href="/channel/art-gratuit" title="Voir les articles gratuits de La Lettre A"><img border="0" alt="Article gratuit" src="/images/lalettrea/deco/LaLettreAGratuit.jpg"></a></div>');
            // 6. Ajout des fonctions sur les nouveaux boutons
            $('a.back', this).click(function() {
                gotoPage(currentPage - 1);
                return false;
            });

            $('a.forward', this).click(function() {
                gotoPage(currentPage + 1);
                return false;
            });

            
            $(this).bind('goto', function(event, page) {
                gotoPage(page);
            });

            masquerFlecheGauche();

            // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
            $(this).bind('next', function() {
                gotoPage(currentPage + 1);
            });
        });
    };
})(jQuery);

$(document).ready(function() {
    // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
    var autoscrolling = true;

    $('.ListeGratuitUne').ListeGratuitUne().mouseover(function() {
        autoscrolling = false;
    }).mouseout(function() {
        autoscrolling = true;
    });

    setInterval(function() {
        if (autoscrolling) {
            $('.ListeGratuitUne').trigger('next');
        }
    }, 10000);
});

