var default_slide_current = 1;
var default_slide_delay = 10000;
var default_slide_effect = "fade";
var default_slide_haspausefeature = "unactive";


var slider_count = 0;
var slide_current = new Array();
var slide_delay = new Array();
var slide_effect = new Array();
var slide_timer = new Array();
var slide_paused = new Array();
var slide_haspausefeature = new Array();
var max_slide = new Array();

jQuery(document).ready(function() {
    (function($) {
        
        //start init slideshow
        $('.slider').each(function() {
            var $current_slide = $(this);
            
            //set slider id
            $(this).attr('slider_id', slider_count);
            
            //set slider default param
            slide_current[slider_count] = default_slide_current;
            
            //set slider delay
            slide_delay[slider_count] = default_slide_delay;
            if($(this).attr('transition-delay') != undefined && $(this).attr('transition-delay') != '' && parseInt($(this).attr('transition-delay')) > 0) {
                slide_delay[slider_count] = parseInt($(this).attr('transition-delay'))*1000;
            }
            
            //set slider effect
            slide_effect[slider_count] = default_slide_effect;
            if($(this).attr('transition-type') != undefined && $(this).attr('transition-type') != '') {
                slide_effect[slider_count] = parseInt($(this).attr('transition-type'))*1000;
            }

            max_slide[slider_count] = 0;
            if($('.slide', $(this)).length>1) {
                slide_timer[slider_count] = window.setTimeout("doSlideFade("+slider_count+")", slide_delay[slider_count]);
                $('.slide', $(this)).each(function() {
                   //set slide id
                   $(this).attr('id', 'slider_'+slider_count+'_'+(max_slide[slider_count]+1));
                   if(max_slide[slider_count] == 0) {
                       $(this).addClass('first-slide');
                   }
                   max_slide[slider_count]++; 
                });
                
                
                /*SCROLLBAR - CONTROLLER*/
                $('.scrollbar-controller', $(this)).each(function() {
                    $(this).attr('relslider', slider_count);
                    $(this).draggable({
                        containment: $(this).parent(),
                        stop: function(event, ui) {
                            //register all step for the scroll bar
                            var scrollPosition = new Array();
                            var scrollWidth = $(this).parent().width()-$(this).width();
                            var scrollCran = scrollWidth/(max_slide[$(this).attr('relslider')]-1);
                            var slideIndex;
                            
                            for(slideIndex = 0; slideIndex < max_slide[$(this).attr('relslider')]-1; slideIndex++) {
                                scrollPosition[slideIndex] = scrollCran*slideIndex;
                                if(slideIndex!=0) {
                                    scrollPosition[slideIndex] = scrollPosition[slideIndex]-($(this).width()/2);
                                }
                            }
                            scrollPosition[slideIndex] = scrollWidth;
                            
                            //find closest step for dropped position
                            var scrollDroppedPosition = $(this).css('left').replace('px', '');
                            var closestIndex = 0;
                            var closestPosition = Math.abs(scrollPosition[0]-scrollDroppedPosition);
                            
                            for(slideIndex = 0; slideIndex < scrollPosition.length; slideIndex++) {
                                if(Math.abs(scrollPosition[slideIndex]-scrollDroppedPosition) < closestPosition) {
                                    closestIndex = slideIndex;
                                    closestPosition = Math.abs(scrollPosition[slideIndex]-scrollDroppedPosition);
                                }
                            }
                            
                            //move scroll controller to closest position
                            $(this).animate({left: scrollPosition[closestIndex]}, 300);
                            gotoSlideFade(closestIndex+1, $(this).attr('relslider'));
                        }
                    }); 
                });
                
                /*CLICK ON SCROLLBAR*/
                $('.thumbs-scrollbar', $(this)).each(function() {
                    $(this).attr('relslider', slider_count);
                    $(this).click(function(e) {
                        var relativeX = e.pageX - this.offsetLeft;
                        
                        //register all step for the scroll bar
                        var scrollPosition = new Array();
                        var scrollWidth = $(this).width()-$('.scrollbar-controller', $(this)).width();
                        var scrollCran = scrollWidth/(max_slide[$(this).attr('relslider')]-1);
                        var slideIndex;

                        for(slideIndex = 0; slideIndex < max_slide[$(this).attr('relslider')]-1; slideIndex++) {
                            scrollPosition[slideIndex] = scrollCran*slideIndex;
                            if(slideIndex!=0) {
                                scrollPosition[slideIndex] = scrollPosition[slideIndex]-($('.scrollbar-controller', $(this)).width()/2);
                            }
                        }
                        scrollPosition[slideIndex] = scrollWidth;

                        //find closest step for dropped position
                        var scrollDroppedPosition = relativeX;
                        var closestIndex = 0;
                        var closestPosition = Math.abs(scrollPosition[0]-scrollDroppedPosition);

                        for(slideIndex = 0; slideIndex < scrollPosition.length; slideIndex++) {
                            if(Math.abs(scrollPosition[slideIndex]-scrollDroppedPosition) < closestPosition) {
                                closestIndex = slideIndex;
                                closestPosition = Math.abs(scrollPosition[slideIndex]-scrollDroppedPosition);
                            }
                        }

                        //move scroll controller to closest position
                        $('.scrollbar-controller', $(this)).animate({left: scrollPosition[closestIndex]}, 300);
                        gotoSlideFade(closestIndex+1, $(this).attr('relslider'));
                        
                    }); 
                });
                
                /* Go to previous/next link */
                $('.gotoprevious', $(this)).each(function() {
                    $(this).attr('relslider', slider_count);
                });
                $('.gotonext', $(this)).each(function() {
                    $(this).attr('relslider', slider_count);
                });
                
                $('.gotoprevious', $(this)).click(function() {
                    gotoSlideFade_previous($(this).attr('relslider'));
                });
                $('.gotonext', $(this)).click(function() {
                    gotoSlideFade_next($(this).attr('relslider'));
                });
                
                //set thumb rel and relpos
                $('.thumbs', $(this)).each(function() {
                    var counter_thumb = 1;
                    $('.thumb', $(this)).each(function() {
                       //init .thumb rel
                       $(this).attr('rel', 'slider_'+slider_count+'_'+counter_thumb);
                       $(this).attr('relpos', counter_thumb);
                       $(this).attr('relslider', slider_count);
                       counter_thumb++;
                    });
                });
                
                //set slider effect rel
                $('.slider-effect', $(this)).each(function() {
                        var counter_slider_effect_leef = 1;
                        var slider_effect_name = $(this).attr('effectname');
                        
                        $('.slider-effect-leef', $(this)).each(function() {
                           if(counter_slider_effect_leef == 1) {
                               $(this).addClass('slider-effect-leef-active');
                           }
                           $(this).attr('rel', 'slider_'+slider_count+'_'+counter_slider_effect_leef);
                           $(this).attr('reluniqueid', 'slider_'+slider_effect_name+'_'+slider_count+'_'+counter_slider_effect_leef);
                           $(this).attr('relslider', slider_count);
                           counter_slider_effect_leef++;
                        });
                        
                });

                //set thumb listener
                $('.thumb', $(this)).click(function() {
                    gotoSlideFade($(this).attr('relpos'), $(this).attr('relslider'));
                });
            }
            else if($('.slide', $(this)).length==1) { 
                //add ...-first class if slideshow size == 1
                $('.slide:first', $(this)).addClass('first-slide');
                
                $('.slider-effect', $(this)).each(function() {
                    $('.slider-effect-leef', $(this)).addClass('slider-effect-leef-active');
                });
            }
            
            /*Play pause controller*/
            slide_paused[slider_count] = false;
            $('.slider-pause', $(this)).attr('rel', slider_count);
            $('.slider-play', $(this)).attr('rel', slider_count);
            
            $('.slider-pause', $(this)).click(function() {
                window.clearTimeout(slide_timer[$(this).attr('rel')]);
                slide_paused[$(this).attr('rel')] = true;
                $(this).hide();
                $('.slider-play', $(this).parent()).show();
            });
            $('.slider-play', $(this)).click(function() {
                slide_paused[$(this).attr('rel')] = false;
                $(this).hide();
                gotoSlideFade_next($(this).attr('rel'));
                $('.slider-pause', $(this).parent()).show();
            });
            
            //set point pause effect
            slide_haspausefeature[slider_count] = default_slide_haspausefeature;
            if($(this).attr('playpause') != undefined && $(this).attr('playpause') == 'active') {
                slide_haspausefeature[slider_count] = 'active';
                $('.point', $(this)).attr('rel', slider_count);
            }
            
            slider_count++;
        });
        //end init du slideshow
        
        
        //IMAGE THUMBNAIL ANIMATED
        $('.slider .navigation-animated a.opener').click(function() {
            var bup_height;
            if($(this).parent().hasClass('navigation-opened')) {
                bup_height = $('.navigation-thumb', $(this).parent()).height();
                var elem = $(this).parent();
                
                $('.navigation-thumb', $(this).parent()).animate({height: 0+'px'}, 200, function() {
                    $(elem).removeClass('navigation-opened').addClass('navigation-closed');
                    $(this).height(bup_height);
                });                
            }
            else {
                $(this).parent().addClass('navigation-opened').removeClass('navigation-closed');
                bup_height = $('.navigation-thumb', $(this).parent()).height();
                $('.navigation-thumb', $(this).parent()).height(0).animate({height: bup_height}, 200);
            }
        });
        
        /*HOVER THUMB*/
        $('.slider .thumb').hover(function() {
            $(this).toggleClass('hover');
        });
        
        $('.slider-pause').hover(function() {
            $(this).toggleClass('slider-pause-hover');
        });
        
        /*HOVER POINT => MODIFY Z-INDEX*/
        $('.point').hover(function() {
            $(this).css('z-index', 51);
        }, function() {
            $(this).css('z-index', 50);
        });
        
    })(jQuery);
});

/**
 * Go to the next programmed lide with fade effect
 */
function doSlideFade(slider_id, old_position) {
    (function($) {
        
        //recalculate position
        window.clearTimeout(slide_timer[slider_id]);
        var slide_next = slide_current[slider_id]+1;
        if(slide_next>max_slide[slider_id]) {
            slide_next=1;
        }
        
        if(old_position) {
            slide_current[slider_id] = old_position;
        }

        //prepare fade
        $('#slider_'+slider_id+'_'+slide_next).css({zIndex: 1}).show();
        
        //stop current animation
        $('.slider[slider_id='+slider_id+'] .slide').stop(true, true);
        
        //update scrollbar
        updateScrollbar(slider_id, slide_next);
        
        //update navigation thumbnail position
        updateNavigationThumbs(slider_id, slide_next);
        
        //update slide counter
        updateSlideCounter(slider_id, slide_next);
        
        //fadeout
        var $slide_current_local = slide_current[slider_id];
        slide_current[slider_id]=slide_next;
        $('#slider_'+slider_id+'_'+$slide_current_local).css({zIndex: 2}).fadeOut(700, function() {
            
            if(slide_paused[slider_id] == false) {
                slide_timer[slider_id] = window.setTimeout("doSlideFade("+slider_id+")", slide_delay[slider_id]); 
            }
            
            //change slider effect leef status
            $('.slider-effect-leef[rel=slider_'+slider_id+'_'+slide_next+']', $('.slider[slider_id='+slider_id+']')).addClass('slider-effect-leef-active');
            
            $(this).hide();
        });
        
        //change thumb status
        $('.thumbs li.active', $('.slider[slider_id='+slider_id+']')).removeClass('active');
        $('.thumbs li[rel=slider_'+slider_id+'_'+slide_next+']').addClass('active');

        //change slider effect leef status
        $('.slider-effect-leef-active', $('.slider[slider_id='+slider_id+']')).removeClass('slider-effect-leef-active');
        
    })(jQuery);
}

/**
 * Directly go to a slide with a fade effect
 */
function gotoSlideFade(slide_pos, slider_id) {
    (function($) {
        
        if(slide_pos != slide_current[slider_id]) {
            
            var old_position = slide_current[slider_id];
            slide_current[slider_id]=slide_pos-1;
            if(slide_current[slider_id] == 0) {
                slide_current[slider_id] = max_slide[slider_id];
            }
            
            doSlideFade(slider_id, old_position);
        }
        
    })(jQuery); 
}


/**
 * Directly go to next slide with a fade effect
 */
function gotoSlideFade_next(slider_id) {
    (function($) {
        var old_position = slide_current[slider_id];
        doSlideFade(slider_id, old_position);
    })(jQuery); 
}

/**
 * Directly go to previous slide with a fade effect
 */
function gotoSlideFade_previous(slider_id) {
    (function($) {
        var old_position = slide_current[slider_id];
        slide_current[slider_id]=slide_current[slider_id]-2;
        if(slide_current[slider_id] == -1) {
            slide_current[slider_id] = max_slide[slider_id]-1;
        }
        doSlideFade(slider_id, old_position);
    })(jQuery); 
}

/**
 * Update the scrollbar controller position
 */
function updateScrollbar(slider_id, position) {
    (function($) {
        //register all step for the scroll bar
        var elem = $('.slider[slider_id='+slider_id+'] .scrollbar-controller');
        
        if(!$(elem).hasClass('ui-draggable-dragging')) {
            var scrollPosition = new Array();
            var scrollWidth = $(elem).parent().width()-$(elem).width();
            var scrollCran = scrollWidth/(max_slide[slider_id]-1);
            var slideIndex;

            for(slideIndex = 0; slideIndex < max_slide[slider_id]-1; slideIndex++) {
                scrollPosition[slideIndex] = scrollCran*slideIndex;
                if(slideIndex!=0) {
                    scrollPosition[slideIndex] = scrollPosition[slideIndex]-($(elem).width()/2);
                }
            }
            scrollPosition[slideIndex] = scrollWidth;

            //move scroll controller to given position if not currently dragging
            $(elem).animate({left: scrollPosition[position-1]}, 300);
        }
    })(jQuery); 
}

/**
 * Update the navigation thumbnails position
 */
function updateNavigationThumbs(slider_id, position) {
    (function($) {
        //foreach navigation thumb (static/non static)
        $('.slider[slider_id='+slider_id+'] .navigation-thumb .thumbs').each(function() {
            var elem = $(this);
            var container_width = $(elem).width();
            var one_li_width = $('li', $(elem)).width();
            
            if(position*one_li_width > container_width) {
                $('ul', $(elem)).animate({
                    'left': -((position*one_li_width)-container_width)+"px"
                }, 200);
            }
            else {
                $('ul', $(elem)).animate({
                    'left': '0px'
                }, 200);                
            }
        });
        
    })(jQuery); 
}

/**
 * Update slide counter
 */
function updateSlideCounter(slider_id, position) {
    (function($) {
        $('.slider[slider_id='+slider_id+'] .slider-pagination-counter .current-slide').html(position);
    })(jQuery); 
}
