(function($) {
    $.fn.s3Slider = function(options) { 
        var element     = this;
        element.each(function() {
            var temp        = new Array();
            var list        = $(this).find('.slides-holder li');
            var text        = $(this).find('.slides-holder li div');
            var status      = 'closed';
            var hovered     = false;
            this.no          = 0;
            var defaults    = {
                speed: 4000,
                scrollTB: false,
                textEasing: false,
                textEasingIn: false,
                textEasingOut: false,
                randomize: false,
                paginator: false,
                navigator: true,
                captionOpacity: .6
            }
            options = $.extend(defaults, options);
            
            text.css({'opacity': options.captionOpacity, 'display': 'none'});
            list.css('display','none');
            
            if(options.paginator) {
                var tHolder = $(this).find('.thumbs .thumbs-holder ul');
                var thumbs  = $(this).find('.thumbs .thumbs-holder ul li a');
                thumbs.each(function(i) {
                    $(thumbs[i]).bind('click', function() {
                        fadeInTo(i);
                        return false;
                    });
                });
                var tWidth = $(thumbs[0]).parent().width() + parseInt($(thumbs[0]).parent().css('margin-left'), 10) + parseInt($(thumbs[0]).parent().css('margin-right'), 10) + parseInt($(thumbs[0]).parent().css('padding-left'), 10) + parseInt($(thumbs[0]).parent().css('padding-right'), 10);
                var tHeight = $(thumbs[0]).parent().height() + parseInt($(thumbs[0]).parent().css('margin-top'), 10) + parseInt($(thumbs[0]).parent().css('margin-bottom'), 10) + parseInt($(thumbs[0]).parent().css('padding-top'), 10) + parseInt($(thumbs[0]).parent().css('padding-bottom'), 10);
                var hWidth = $(tHolder).parent().width();
                var hHeight = $(tHolder).parent().height();
                if(tWidth*2 < hWidth) {var scrollType = 'horisontal';}
                else {var scrollType = 'vertical';}
                if(scrollType == 'horisontal') {
                    var decimal  = (hWidth%tWidth == 0) ? 0 : 1;
                    var maxShown = Math.round(hWidth/tWidth) - decimal;
                    var maxLeft  = (thumbs.length * tWidth) - (tWidth * maxShown);
                } else {
                    var decimal  = (hHeight%tHeight == 0) ? 0 : 1;
                    var maxShown = Math.round(hHeight/tHeight) - decimal;
                    var maxTop = (thumbs.length * tHeight) - (tHeight * maxShown);
                }
                tHolder.parent().parent().hover(function() {
                        hovered = true;
                        $(this).find('.thumb-nav').fadeIn('fast');
                    }, function() {
                        hovered = false;
                        $(this).find('.thumb-nav').fadeOut('fast');
                });
                
                var globalTB = this.no;
                $('.thumb-nav').bind('click', function() {
                    if($(this).attr('rel') == 'prev') {
                        scrollTB((globalTB > 0) ? --globalTB : 0, true);
						return false;
                    } else {
                        scrollTB((globalTB < (thumbs.length-maxShown)) ? ++globalTB : (thumbs.length-maxShown), true);
						return false;
                    }
                });
            }
            
            if(options.navigator) {
                $(".slide-nav").css('display', 'none');
                list.parent().parent().hover(function() {
                    $(".slide-nav").fadeIn('fast');
                }, function() {
                    $(".slide-nav").fadeOut('fast');
                });
                $(".prev-slide").bind('click', function() {
                    fadeInTo(calcPrev(no));
                    return false;
                });
                $(".next-slide").bind('click', function() {
                    fadeInTo(calcNext(no));
                    return false;
                });
            }
            
            fadeIn = function(no) {
                this.no = no;
                $(list[no]).fadeIn(defaults.speed/5, function() {
                    slidingText(no, 'slideOut');
                });
                if(options.paginator) {switchClass(no);}
            }
            
            slideOut = function(no) {
                slidingText(no, 'fadeOut');
            }
            
            fadeOut = function(no) {
                this.no = no;
                $(list[no]).fadeOut(options.speed/4);
                fadeIn(calcNext(no));
            }
            
            switchClass = function(no) {
                // switch style for thumbs //
                if(options.scrollTB) {scrollTB(no, false);}
                thumbs.parent().removeClass('current');
                $(thumbs[no]).parent().addClass('current');
            }
            
            fadeInTo = function(no) {
                clearTimeout(temp[this.no]);
                $(list[this.no]).stop();
                $(text[this.no]).stop();
                status = 'closed';
                fadeOutTo(this.no);
                fadeIn(no);
                switchClass(no);
                clearTimeout(temp[this.no]);
            }
            
            fadeOutTo = function(no) {
                $(text[no]).fadeOut('fast');
                $(list[no]).fadeOut('normal');
                list.css('opacity',1);
            }
            
            scrollTB = function(no, clicked) {
                if(!hovered || clicked) {
                    if(scrollType == 'horisontal') {
                        var sLeft    = no * tWidth;
                        var sLeft    = (sLeft >= 0) ? sLeft : 0;
                        var pLeft    = (sLeft < maxLeft) ? -sLeft : -maxLeft;
                        $(tHolder).animate({'left': pLeft + 'px'});
                    } else {
                        var sTop   = no * tHeight;
                        var sLeft    = (sTop >= 0) ? sTop : 0;
                        var pTop   = (sTop < maxTop) ? -sTop : -maxTop;
                        $(tHolder).animate({'top': pTop + 'px'});
                    }
                }
            }
            
            var calcNext = function(no) {
                return (options.radnomize) ? Math.rand(0,list.length-1) : (list.length-1 == no) ? 0 : ++no;
            }
            var calcPrev = function(no) {
                return (0 == no) ? list.length-1 : --no;
            }
            
            var slidingText = function(no, callback) {
                this.no = no;
                if($(text[no]).css('top') == '0px' && status == 'closed') {
                    $(text[no]).slideDown({
                        duration: options.speed/5,
                        easing: (options.textEasing) ? (status == 'open') ? options.textEasingOut : options.textEasingIn : 'swing',
                        complete: function() {
                            if(callback == 'slideOut') temp[no] = setTimeout('slideOut(this.no)', options.speed);
                            else fadeOut(no);
                            status = (status == 'open') ? 'closed' : 'open';
                        }
                    });
                } else {
                    $(text[no]).slideUp({
                        duration: options.speed/5,
                        easing: (options.textEasing) ? (status == 'open') ? options.textEasingOut : options.textEasingIn : 'swing',
                        complete: function() {
                            if(callback == 'slideOut') temp[no] = setTimeout('slideOut(this.no)', options.speed);
                            else fadeOut(no);
                            status = (status == 'open') ? 'closed' : 'open';
                        }
                    });
                }

            }            
            fadeIn(this.no);
        });
        return this;
    }
})(jQuery);
