/*
* optaSlider - plugin JQuery
* 31/10/2011
*
* Paramètres acceptés par le plugin et valeurs possibles
*  - mode : slide, fade
*  - displayPagination : true, false
*  - displayPrevNext : true, false
*  - autoPrevNextPosition : true, false 
*  - isAnimated : true, false
*  - delay : nombre en millisecondes, 4000 par défaut
*  - duration : nombre en millisecondes, 500 par défaut
*  - showAllSlide : false, true
*/

(function ($) {

    $.fn.optaSlider = function (settings) {
        options = {
            mode: 'slide',
            displayPagination: true,
            displayPrevNext: true,
            autoPrevNextPosition: true,
            isAnimated: true,
            delay: 4000,
            duration: 500,
            showAllSlide : false,
			os_interval : ''
        };

        var options = $.extend(options, settings);
        
        return this.each(function () {
            
            //
            // init
            //
            // initialisation des variables
            var container = $(this);
            var containerHeight = parseInt($(this).height());
            var containerWidth = parseInt($(this).width());
            var nbSlide = parseInt($(this).find('ul.os_slider>li').length);
            var maxSlideIndex = nbSlide - 1;
            var slideHeight = parseInt($(this).find('ul.os_slider>li').outerHeight(true));
            var slideWidth = parseInt($(this).find('ul.os_slider>li').outerWidth(true));
            var ulSliderWidth;
            switch (options.mode) {
                case 'slide' :
                    ulSliderWidth = parseInt(slideWidth * nbSlide);
                    break;
                
                case 'fade' :
                    ulSliderWidth = slideWidth;
                    break;
            }
            var containerOverflow = 'hidden';
            if (options.showAllSlide) { containerOverflow = 'visible';}
            
            
            // affectation des css
            // css du container
            $(this).css({
                'position' : 'relative',
                'overflow' : containerOverflow
                });
            
            // css ul.os_slider
            $(this).children('ul.os_slider').css({
                'position' : 'relative',
                'height' : slideHeight,
                'width' : ulSliderWidth,
                'margin' : 0,
                'padding' : 0
                });
            $(this).find('ul.os_slider>li').css({
                'position' : 'absolute',
                'list-style' : 'none'
            });
            $(this).find('ul.os_slider>li:first').addClass('selected');
            
            // css ul.os_pagination
            if (options.displayPagination) {
                $(this).find('ul.os_pagination>li:first').addClass('selected');
            } else {
                $(this).children('ul.os_pagination').hide();
            }
            
            // css a.os_previous, a.os_next
            if (options.displayPrevNext) {
                if (options.autoPrevNextPosition) {
                    $(this).children('a.os_previous').css({
                        'position' : 'absolute',
                        'left' : '0px',
                        'top' : getCenterTopPosition(containerHeight, $(this).children('a.os_previous').height())
                        });
                    
                    $(this).children('a.os_next').css({
                        'position' : 'absolute',
                        'right' : '0px',
                        'top' : getCenterTopPosition(containerHeight, $(this).children('a.os_next').height())
                        });
                }
            } else {
                $(this).children('a.os_previous, a.os_next').hide();
            }
            
            // initialisation du slider
            switch (options.mode) {
                case 'slide' :
                    $(this).find('ul.os_slider>li').each(function(index) {
                       $(this).css('left', slideWidth * index);
                    });
                    $(this).find('ul.os_slider>li:last').css('left', slideWidth * -1);
                    break;
                
                case 'fade' :
                    container.find('ul.os_slider>li').not('.selected').hide();
                    break;
            }
            
            // défilement automatique
            if (options.isAnimated) {
                /*
                os_interval = setInterval(function () {
                    animateSlider('next', getCurrentSlideIndex(), getPreviousSlideIndex(), getNextSlideIndex(), null, options.duration);
                }, options.delay);
                */
				
                options.os_interval = setTimeout(function () {
                    animateSlider('next', getCurrentSlideIndex(), getPreviousSlideIndex(), getNextSlideIndex(), null, options.duration);
                }, options.delay);
            }
            
            //
            // functions
            //
            function getCenterTopPosition(wrapperHeight, itemHeight) {
                return wrapperHeight / 2 - itemHeight / 2;
            }
            
            function getCurrentSlideIndex() {
                var currentIndex = 0;
                container.find('ul.os_slider>li').each(function(){
                    if ($(this).hasClass('selected')) {
                        currentIndex = parseInt($(this).index());
                    }
                });
                return currentIndex;
            }
            
            function getPreviousSlideIndex() {
                var currentIndex = getCurrentSlideIndex();
                var previousIndex = maxSlideIndex;
                if (currentIndex > 0) {
                    previousIndex = currentIndex - 1;
                }
                return previousIndex;
            }
            
            function getNextSlideIndex() {
                var currentIndex = getCurrentSlideIndex();
                var nextIndex = 0;
                if (currentIndex < maxSlideIndex) {
                    nextIndex = currentIndex + 1;
                }
                return nextIndex;
            }
            
            function animateSlider(control, currentIndex, previousIndex, nextIndex, gotoIndex, myDuration) {
                if (!container.children('ul.os_slider').is(':animated') || gotoIndex != null) {
                    
                    // gestion du défilement automatique
                    if (options.isAnimated) {clearTimeout(options.os_interval);}
                    
                    switch (options.mode) {
                        case 'slide' :
                            switch (control) {
                                case 'next' :
                                    // gestion du slider
                                    container.find('ul.os_slider>li:eq(' + currentIndex + ')').removeClass('selected');
                                    container.find('ul.os_slider>li:eq(' + nextIndex + ')').addClass('selected');
                                    // gestion de la pagination
                                    container.find('ul.os_pagination>li:eq(' + currentIndex + ')').removeClass('selected');
                                    container.find('ul.os_pagination>li:eq(' + nextIndex + ')').addClass('selected');
                                    // animation
                                    container.children('ul.os_slider').animate({
                                        'margin-left' : '-=' + slideWidth
                                    }, {
                                        duration : myDuration,
                                        queue : true,
                                        complete : function() {
                                            // gestion du slider suite
                                            container.find('ul.os_slider>li:eq(' + previousIndex + ')').css({
                                                'left' :  '+=' + slideWidth * nbSlide
                                            });
                                        }
                                    });
                                    break;
                                
                                case 'previous' :
                                    // gestion du slider
                                    container.find('ul.os_slider>li:eq(' + currentIndex + ')').removeClass('selected');
                                    container.find('ul.os_slider>li:eq(' + previousIndex + ')').addClass('selected');
                                    container.find('ul.os_slider>li:eq(' + getPreviousSlideIndex() + ')').css({
                                        'left' :  '-=' + slideWidth * nbSlide
                                    });
                                    // gestion de la pagination
                                    container.find('ul.os_pagination>li:eq(' + currentIndex + ')').removeClass('selected');
                                    container.find('ul.os_pagination>li:eq(' + previousIndex + ')').addClass('selected');
                                    //animation
                                    container.children('ul.os_slider').animate({
                                        'margin-left' : '+=' + slideWidth
                                    }, {
                                        duration : myDuration,
                                        queue : true
                                    });
                                    break;
                                
                                case 'goto' :
                                    if (gotoIndex > currentIndex) {
                                        // next
                                        var loop = gotoIndex - currentIndex;
                                        for (var i=0; i<loop; i++) {
                                            animateSlider('next', getCurrentSlideIndex(), getPreviousSlideIndex(), getNextSlideIndex(), gotoIndex, Math.ceil(options.duration/loop));
                                        }
                                    } else {
                                        //previous
                                        var loop = currentIndex - gotoIndex;
                                        for (var i=0; i<loop; i++) {
                                            animateSlider('previous', getCurrentSlideIndex(), getPreviousSlideIndex(), null, gotoIndex, Math.ceil(options.duration/loop));
                                        }
                                    }
                                    break;
                            }
                            break;
                        
                        case 'fade' :
                            switch (control) {
                                case 'next' :
                                    // gestion du slider
                                    container.find('ul.os_slider>li:eq(' + currentIndex + ')').removeClass('selected').fadeOut(myDuration);
                                    container.find('ul.os_slider>li:eq(' + nextIndex + ')').addClass('selected').fadeIn(myDuration);
                                    // gestion de la pagination
                                    container.find('ul.os_pagination>li:eq(' + currentIndex + ')').removeClass('selected');
                                    container.find('ul.os_pagination>li:eq(' + nextIndex + ')').addClass('selected');
                                    break;
                                
                                case 'previous' :
                                    // gestion du slider
                                    container.find('ul.os_slider>li:eq(' + currentIndex + ')').removeClass('selected').fadeOut(myDuration);
                                    container.find('ul.os_slider>li:eq(' + previousIndex + ')').addClass('selected').fadeIn(myDuration);
                                    // gestion de la pagination
                                    container.find('ul.os_pagination>li:eq(' + currentIndex + ')').removeClass('selected');
                                    container.find('ul.os_pagination>li:eq(' + previousIndex + ')').addClass('selected');
                                    break;
                                
                                case 'goto' :
                                    // gestion du slider
                                    container.find('ul.os_slider>li:eq(' + currentIndex + ')').removeClass('selected').fadeOut(myDuration);
                                    container.find('ul.os_slider>li:eq(' + gotoIndex + ')').addClass('selected').fadeIn(myDuration);
                                    // gestion de la pagination
                                    container.find('ul.os_pagination>li:eq(' + currentIndex + ')').removeClass('selected');
                                    container.find('ul.os_pagination>li:eq(' + gotoIndex + ')').addClass('selected');
                                    break;
                            }
                            break;
                    }
                    
                    // gestion du défilement automatique
                    if (options.isAnimated)
                    {
                        options.os_interval = setTimeout(function () {
                            animateSlider('next', getCurrentSlideIndex(), getPreviousSlideIndex(), getNextSlideIndex(), null, options.duration);
                        }, options.delay);
                    }
                }
            }
            
            //
            // events
            //
            $(this).children('a.os_previous').click(function() {
                animateSlider('previous', getCurrentSlideIndex(), getPreviousSlideIndex(), null, null, options.duration);
                return false;
            });
            
            $(this).children('a.os_next').click(function() {
                animateSlider('next', getCurrentSlideIndex(), getPreviousSlideIndex(), getNextSlideIndex(), null, options.duration);
                return false;
            });
            
            $(this).find('ul.os_pagination>li>a').click(function() {
                animateSlider('goto', getCurrentSlideIndex(), getPreviousSlideIndex(), getNextSlideIndex(), parseInt($(this).parent().index()), options.duration);
                return false;
            });
            
            /*
            $(this).hover(function(){
                if (options.isAnimated) {clearInterval(os_interval);}
            }, function(){
                if (options.isAnimated)
                {
                    os_interval = setInterval(function () {
                        animateSlider('next', getCurrentSlideIndex(), getPreviousSlideIndex(), getNextSlideIndex(), null, options.duration);
                    }, options.delay);
                }
            });
            */
        
        });
    };
    
})(jQuery);
