(function($) {

	$.fn.sbSlider = function(options){
	  
		// default properties
		var defaults = {
			orientation:	'',
			speed: 			1100,		
			timeout:		5000
		}; 
		
		var options = $.extend(defaults, options);  
		
		return this.each(function() {  
			obj = $(this); 				
			var s = $("li", obj).length;
			var w = obj.width(); 
			var h = obj.height(); 
			var ts = s-1;
			var t = 0;
			var restart = false;
			var btnShown = false;
			var vertical = (options.orientation == 'vertical');
			$("ul", obj).css('width',s*w);			
			if(!vertical) $("li", obj).css('float','left');
			
			$("#slider").mouseover(function() {
				$(document).stopTime("slider");
				$(document).stopTime("sliderBtn");
								
				btnShown = true;
				ShowBtn();			
			});
			
			$("#slider").mouseout(function() {
			    $(document).everyTime(options.timeout, "slider", function() {
					animate("next");						
				});
				
			    $(document).everyTime(200, "sliderBtn", function() {
		            btnShown = false;
			        $("#btn_next").fadeOut(500);
			        $("#btn_prev").fadeOut(500);
			    });	
			});

			$(document).everyTime(options.timeout, "slider", function() {
				animate("next");
			});
			
			$("#btn_next").hide();
			$("#btn_next").click(function() {
			    animate("next");
			});		
				
			$("#btn_prev").hide();
			$("#btn_prev").click(function() {
			    restart = false;
			    animate("prev");
			});	
			
			function ShowBtn()
			{
			    if (btnShown)
			    {
			        if (t == 0) {
			            $("#btn_prev").fadeOut(500);
			            $("#btn_next").fadeIn(500);
			        }
			        else  if (t == ts) {
			            $("#btn_prev").fadeIn(500);
			            $("#btn_next").fadeOut(500);
			        }
			        else if (t > 0 && t < ts)
			        {
			            $("#btn_next").fadeIn(500);
			            $("#btn_prev").fadeIn(500);
			        }		
			    }	
			}			
			
			function animate(dir){		
				if(dir == "next"){
					t = (t>=ts) ? ts : t+1;	
					
					if (restart)
					{
						restart = false;
						
						$("#bg" + (t+1)).fadeOut(options.speed);
						t=0;
						$("#bg" + (t+1)).fadeIn(options.speed);
						
						$("ul",obj).css({ marginTop: h });	

						$("ul",obj).animate(
							{ marginTop: 0 }, 
							options.speed
						);						 
					
						return;
					}
					
					$("#bg" + (t+1)).fadeIn(options.speed);
					$("#bg" + t).fadeOut(options.speed);
					
				} else {                    
					$("#bg" + t).fadeIn(options.speed);
					$("#bg" + (t+1)).fadeOut(options.speed);				

					t = (t<=0) ? 0 : t-1;
				};	
				
				if (!vertical) {
					p = (t*w*-1);
					
					$("ul",obj).animate(
						{ marginLeft: p }, 
						options.speed
					);				
				} else {
					p = (t*h*-1);

					$("ul",obj).animate(
						{ marginTop: p }, 
						options.speed
					);					
				}
				
				if (t>=ts) 
					restart = true;
					
				ShowBtn();
			};
		});
	};

})(jQuery);
