(function($){  

    $.fn.s3Slider = function(vars) {     
        
        var element     = this;
        var timeOut     = (vars.timeOut != undefined) ? vars.timeOut : 7000;
        var current     = null;
        var timeOutFn   = null;
        var faderStat   = true;
        var mOver       = false;
		var blockTM     = false;
        var items       = $("#" + element[0].id + "Content ." + element[0].id + "Image");
        var itemsSpan   = $("#" + element[0].id + "Content ." + element[0].id + "Image span");
        items.each(function(i) {
            $(items[i]).mouseover(function() {
               mOver = true;
			   clearTimeout(timeOutFn);
            });
            $(items[i]).mouseout(function() {
                mOver = false;
				timeOutFn = setTimeout(tmp1fn, 7000);
            });
        });
        
		$('#sl_r').bind('click',function() { tmp1fn(); });
		$('#sl_l').bind('click',function() { tmp2fn(); });
		
		var tmp1fn = function(){
			clearTimeout(timeOutFn);
			makeSlider(1);
		}
		var tmp2fn = function(){
			clearTimeout(timeOutFn);
			makeSlider(-1);
		}
		
		var donext = function() {
			makeSlider(1);
			timeOutFn = setTimeout(tmp1fn, 5000);
		}
		var doprev = function() {
			makeSlider(-1);
			timeOutFn = setTimeout(tmp1fn, 7000);
		}
		
        var makeSlider = function(num) {
            if(!num || num > 0) {
				current = (current != null) ? current : items[(items.length-1)];
				var currNo = jQuery.inArray(current, items) + 1
    	        currNo = (currNo == items.length) ? 0 : (currNo - 1);
			} else {
				current = (current != null) ? current : items[(items.length-2)];
				var currNo = jQuery.inArray(current, items) + 1
    	        currNo = (currNo-1 == 0) ? 0 : currNo - 1;
			}
            var newMargin   = $(element).width() * currNo;
            if(faderStat == true) {
                if(!mOver) {
                    $(items[currNo]).fadeIn((timeOut/6), function() {
                        if($(itemsSpan[currNo]).css('bottom') == 0) {
                            $(itemsSpan[currNo]).slideUp((timeOut/6), function() {
                                faderStat = false;
                                current = items[currNo];
                            });
                        } else {
                            $(itemsSpan[currNo]).slideDown((timeOut/6), function() {
                                faderStat = false;
                                current = items[currNo];
                            });
                        }
                    });
                }
            } else {
                if(!mOver) {
                    if($(itemsSpan[currNo]).css('bottom') == 0) {
                        $(itemsSpan[currNo]).slideDown((timeOut/6), function() {
                            $(items[currNo]).fadeOut((timeOut/6), function() {
                                faderStat = true;
								if(!num || num > 0) {
	                                current = items[(currNo+1)];
									donext();
								} else {
									current = items[(currNo-1)];
									doprev();
								}
                            });
                        });
                    } else {
                        $(itemsSpan[currNo]).slideUp((timeOut/6), function() {
                       		$(items[currNo]).fadeOut((timeOut/6), function() {
                                faderStat = true;
								if(!num || num > 0) {
	                                current = items[(currNo+1)];
									donext();
								} else {
									current = items[(currNo-1)];
									doprev();
								}
                            });
                        });
                    }
                }
            }
        }
        
        makeSlider();
		
		timeOutFn = setTimeout(tmp1fn, 7000);
		
		return this;

    };  

})(jQuery);
