// JavaScript Document
(function($) {
	$.fn.ITSailFade = function(option){
		var defaults = {autoable:1};
		option = $.extend(defaults, option);
		
		var oMain = $(this);
		var oULi = $(".slider ul li", oMain);
		var nums = oULi.length - 1;
		var this_num = 0;
		var last_num = 0;
		
		var timerID = null;
		var timeout = 6000;
		var timestep = 1000;
		
		var autoable = 1;
		var t_autoable = 1;
		if(typeof option.autoable != undefined) t_autoable = option.autoable;
		//alert(option.autoable);
		//alert(t_autoable);
		
		oULi.css({opacity: 0.0});
		oULi.eq(0).addClass('show').css({opacity: 1.0});
		
		var html = '';
		for(var i=0;i<=nums;i++){
			html += '<li></li>';
		}
		$('.dot', oMain).html('<ol>' + html+ '</ol>');
		
		var controls = $('ol li', oMain);
		controls.click(function(){onclick($(this).index());});
		controls.eq(this_num).addClass("current");
		
		$('.arrowL', oMain).click(function(){onclick(this_num - 1);});
		$('.arrowR', oMain).click(function(){onclick(this_num + 1);});
		
		function slide() {
			autoable = 0;
			
			if(this_num > nums) this_num = 0;
			else if(this_num < 0) this_num = nums;
			
			if(this_num == last_num) return;
			
			//$('.footer').text(nums + ' = = ' + this_num + ' = = ' + last_num);
			
			oULi.eq(this_num).addClass('show').animate({opacity: 1.0}, timestep);
			oULi.eq(last_num).removeClass('show').animate({opacity: 0.0}, timestep, timer());
			
			controls.eq(last_num).removeClass("current");
			controls.eq(this_num).addClass("current");
			
			last_num = this_num;
		}

		function auto() {
			this_num++;
			slide();
		}
		
		function timer() {
			autoable = 1;
			if(t_autoable == 1) timerID = setTimeout(auto, timeout);
		}
		
		function onclick(id) {
			if(autoable == 1) {
				clearTimeout(timerID);
				this_num = id;
				slide();
			}
		}
		
		oULi.hover(function(){clearTimeout(timerID);}, function(){timer();});

		timer();
	};
})(jQuery);


