/* ------------------------------------------------------------------------
	s3Slider
	
	Developped By: Boban Kariik -> http://www.serie3.info/
        CSS Help: Mészáros Róbert -> http://www.perspectived.com/
	Version: 1.2rc1
	
	Copyright: Feel free to redistribute the script/modify it, as
			   long as you leave my infos at the top.
------------------------------------------------------------------------- */
(function($) {
    $.fn.s3Slider = function(options) { 
        var element     = this;
        element.each(function() {
            var temp        = new Array();
            var list        = $(this).find('ul li');
            var text        = $(this).find('ul li div');
            var status      = 'closed';
			var stopped		= false;
            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');
            
			var tHolder = $('#sliderThumbs ul');
			var thumbs  = $('#sliderThumbs ul li a');
			thumbs.each(function(i) {
				$(thumbs[i]).bind('click', function() {
					fadeInTo(i);
					return false;
				});
			});
            
			if(options.navigator) {
				$("#sliderPrev").bind('click', function() {
					fadeInTo(calcPrev(no));
					return false;
				});
				$("#sliderNext").bind('click', function() {
					fadeInTo(calcNext(no));
					return false;
				});
				$("#sliderPause").bind('click', function() {
					if($(this).attr('rel') == 'stop') {
						$(this).attr('rel', 'play');
						$(this).empty().append("<img src='/img/play.jpg' />");
						stop(no);
					} else {
						$(this).attr('rel', 'stop');
						$(this).empty().append("<img src='/img/stop.jpg' />");
						play(no);
					}
					return false;
				});
			}
            
            fadeIn = function(no) {
                this.no = no;
                $(list[no]).fadeIn(defaults.speed/5, function() {
                    if(!stopped) {
						slidingText(no, 'slideOut');
					}
                });
                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 //
                thumbs.removeClass('current');
                $(thumbs[no]).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);
            }
			
			fadeAndStop = function(no) {
				this.no = no;
                $(list[no]).fadeIn(defaults.speed/5);
			}
			
			stop = function(no) {
				clearTimeout(temp[this.no]);
                $(list[this.no]).stop();
                $(text[this.no]).stop();
                status = 'closed';
				fadeAndStop(no);
				clearTimeout(temp[this.no]);
				stopped = true;
			}
			
			play = function(no) {				
				stopped = false;
				fadeIn(calcNext(no));
			}
            
            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);
