// JavaScript Document
(function($) {
	$.fn.ITSailSlider = function(){
		var obj = $("ul", $(this))
		var lis = $("li", obj);
		var s = lis.length;
		var w = lis.width(); 
		var ts = s - 1;
		var t = 0;
		var vs = 1000;
		var ps = 6000;
		
		var timeout;
		var clickable = true;
		
		obj.css('width', 2*w);
		
		$("li:gt(0)", obj).remove();
		$(this).hover(
			function() {
				clearTimeout(timeout);
			},
			function() {
				start();
			}
		);
		
		var opa = 0.2;
		$(".ctrlL .control", $(this)).click(function(){control(false);}).css('opacity', opa).hover(function(){$(this).css('opacity', 1)}, function(){$(this).css('opacity', opa)});
		$(".ctrlR .control", $(this)).click(function(){control(true);}).css('opacity', opa).hover(function(){$(this).css('opacity', 1)}, function(){$(this).css('opacity', opa)});
		
		function control(next) {
			//alert(next);
			var tt = 0;
			if(next) {
				tt = t + 1;
				tt = tt > ts ? 0 : tt;
			} else {
				tt = t - 1;
				tt = tt < 0 ? s-1 : tt;
			}
			//alert(tt);
			animate(tt,true);
		}
		
		function animate(tt, clicked){
			if(clickable == false) return;
			else clickable = false;
				
			var ot = t;
			t = tt > ts ? 0 : tt;
			
			if(clicked) clearTimeout(timeout);
			else {
				timeout = setTimeout(function(){
					animate(t+1, false);
				}, ps+vs);
			}
			
			obj.append(lis.eq(t));
			obj.animate(
				{ marginLeft: w*-1},
				{ queue:false, duration:vs, complete:adjust }
			);
		}
		
		function adjust() {
			obj.css({marginLeft:0}).find("li:first").remove();
			clickable = true;
		}
		
		function start() {
			timeout = setTimeout(function(){
				animate(t+1, false);
			}, ps);
		}
		
		start();
	};
})(jQuery);


