//幻灯片
(function($){
	$.fn.fcSlider = function(options){
		var settings = {
			speed : 3000
		}
		if(options){
			$.extend(settings, options);	
		}
		
		var slider = $(this);
		var list = slider.find("ul");
		var nav = slider.find("div");
		var intervalID = null;
		var count = list.find("li").length;
		
		list.find("li").each(function(index){
			$(this).attr("rel", index);
			$(this).hover(
				function(){
					stopRun();	
				},function(){
					autoRun();
				});							  
		});
		
		nav.find("a").each(function(index){
			$(this).attr("rel", index);
			$(this).hover(function(){
				stopRun();
				var _this = $(this);
				var index = _this.attr("rel");
				list.find("li:eq(" + index + ")").addClass("__fc_start");
				slider.data('index', index);
				run(index);
			},function(){
				stopRun();
				autoRun();
			});
		});
		
		autoRun();
		
		function autoRun(){
			stopRun();
			intervalID = setInterval(autoRun__, settings.speed);
		}
		
		function autoRun__(){
			var index = parseInt(slider.data('index'));
			if(isNaN(index)){
				index = 0;
			}else{
				index += 1;	
			}
			if(index > count-1) index = 0;
			slider.data('index', index);
			run(index);
		}
		
		function stopRun(){
			clearInterval(intervalID);
			list.find("li.__fc_start").stop().removeClass("__fc_start");
			list.find("li.__fc_fadeOut").stop().removeClass("__fc_fadeOut").removeClass("cur").hide().css("opacity", '');
			list.find("li.__fc_fadeIn").stop().removeClass("__fc_fadeIn").addClass("cur").show().css("opacity", 1);
		}
		
		function run(index){
			var speed = 700;
			var cur = list.find("li.cur");
			var next = null;
			if(typeof(index)=="undefined"){
				next = list.find("li:eq(" + (parseInt(cur.attr("rel") )+1)+ ")");
				if(next.html()==null) next = list.find("li:first");
			}else{
				next = list.find("li:eq(" + index + ")");	
			}
			if(cur.attr('rel')==next.attr('rel')) return;
			nav.find("a").removeClass("cur");
			nav.find("a:eq(" + next.attr("rel") + ")").addClass("cur");
			cur.addClass("__fc_fadeOut").fadeOut(speed, function(){$(this).removeClass("__fc_fadeOut").removeClass("cur");});
			next.removeClass("__fc_start").addClass("__fc_fadeIn").fadeIn(speed, function(){$(this).removeClass("__fc_fadeIn").addClass("cur");});
		}
		
	}
})(jQuery);
