(function($) {
    $.fn.slide = function(options) {
        var defaults = {
            slideWidth: 226,
            autoplay: true,
            duration: 8000,
            showNavigator: true,
            showSlideIndex: false
        };
        var options = $.extend(defaults, options);
        return this.each(function() {
            var slideshow = $(this);
            var o = options;
            var currentPosition = 0;
            var slides = $('.slide2');
            var numberOfSlides = slides.length;
            var t;

            // Remove scrollbar in JS
            $('#slidesContainer2').css('overflow', 'hidden');

            // Wrap all .slides with #slideInner2 div
            slides.wrapAll('<div id="slideInner2"></div>')

            // Float left to display horizontally, readjust .slides width
			.css({
			    'float': 'left',
			    'width': o.slideWidth
			});

            // Insert a clone of first slide 
            $('.slide2:first').clone().appendTo('#slideInner2');

            // Set #slideInner2 width equal to total width of all slides
            $('#slideInner2').css('width', o.slideWidth * (numberOfSlides + 1));

            // Insert controls in the DOM
            if (o.showNavigator) {
                slideshow
				    .prepend('<span  id="leftControl2" ><a href="#">Precedent</a></span>')
				    .append('<span  id="rightControl2" ><a href="#">Suivant</a></span>');

                // Create event listeners for .controls clicks
                $('#leftControl2').click(function() { prev(); return false; });
                $('#rightControl2').click(function() { next(); return false; });
            }
            // Insert slides index
            if (o.showSlideIndex == true) {
                slideshow.append('<div id="slideIndex"></div>');
                for (var i = 1; i <= numberOfSlides; i++) {
                    $('#slideIndex').append('');
                }
                $('.numbers').click(function() { goto(($(this).attr('id')).replace('slide-', '') - 1, false); return false; });
            }

            // Start
            init();

            //Init function
            function init() {
                manageControls(currentPosition);
                if (o.autoplay == true) setNextTimeOut(o.duration);
            }

            // Next
            function next() {
                currentPosition++;
                if (currentPosition >= numberOfSlides) currentPosition = 0;
                slideTo(currentPosition, true);
            }

            // Previous
            function prev() {
                currentPosition--;
                if (currentPosition < 0) currentPosition = numberOfSlides - 1;
                slideTo(currentPosition, false);
            }

            // Go to a slide 
            function goto(position) {
                currentPosition = position;
                slideTo(currentPosition, false);
            }

            // Set time out for next slide
            function setNextTimeOut() {
                t = setTimeout(function() { next(); }, o.duration);
            }

            function clearNextTimeOut()
            { 
                clearTimeout(t);
            }
            // Slide
            function slideTo(position, continuously) {
                $('#slideInner2').stop();
                clearNextTimeOut();
                // usual cases
                if (continuously == false || o.autoplay == false || position != 0) {
                    $('#slideInner2').animate({ 'marginLeft': o.slideWidth * (-position) }, '', '',
						function() {
						    manageControls(position);
						    if (o.autoplay == true) setNextTimeOut();
						}
					)
                }
                // autoplay: slide from last to first one continuously
                else {
                    // slide to the 'fake' first slide (actually at the last)
                    $('#slideInner2').animate({ 'marginLeft': o.slideWidth * (-numberOfSlides) }, '', '',
						function() {
						    //immediately change to the 'true' first slide
						    $('#slideInner2').css('marginLeft', 0);
						    manageControls(position)
						    if (o.autoplay == true) setNextTimeOut();
						}
					)
                }
            }

            // manageControls: Hides and Shows controls depending on currentPosition
            function manageControls(position) {
                if (o.showNavigator) {
                    // Hide left arrow if position is first slide
                    if (position == 0) { $('#leftControl2').hide() } else { $('#leftControl2').show() };
                    // Hide right arrow if position is last slide
                    if (position == (numberOfSlides - 1)) { $('#rightControl2').hide() } else { $('#rightControl2').show() };
                }

                // Hilight the current page
                if (o.showSlideIndex == true) {
                    // remove active class from all pages
                    $('.numbers').removeClass("active");
                    // add only to the current page
                    $('#slide2-' + (position + 1)).addClass("active");
                }
            }
        });
    };
})(jQuery);
