﻿function FadeSlide(){
	this.cfg = {
		sup : ".slide",				//瀹瑰櫒
		pics : ".flash li",			//涓讳綋
		idxs : ".idxs li",			//绱㈠紩
		sleft : ".pre",				//涓婁竴涓�
		sright : ".next",     		//涓嬩竴涓�
		arrowani : 45,				//绠ご鍔ㄧ敾璺濈
		infos : ".title h4",		//鏂囨湰
		delay : 5000				//寤惰繜鏃堕棿
	}
}
FadeSlide.prototype = {
	init : function(cfg){
		//鑾峰彇澶栭儴鍙傛暟(鏇挎崲褰撳墠鍙傛暟)
		$.extend(this.cfg,cfg);
		var that = this;

		(function($){
			var index = 0,	//绱㈠紩
				domSlide = $(that.cfg.sup),	//杞挱澶у鍣�
				domPics = domSlide.find(that.cfg.pics),	//鍥剧墖鍒楄〃
				domIndex = domSlide.find(that.cfg.idxs),	//绱㈠紩鍒楄〃
				domPrev = domSlide.find(that.cfg.sleft),	//涓婁竴涓�
				domNext = domSlide.find(that.cfg.sright),	//涓嬩竴涓�
				domInfo = domSlide.find(that.cfg.infos),
				picLens = domPics.length,	
				currentPic = domPics.eq(index),
				currentIndex = domIndex.eq(index),
				tt = 0,
				pIndex = -1;
			
			fade(index);
			//绱㈠紩绉诲姩鍒囨崲
			domIndex.hover(function(){
				index = $(this).index();
				if(index === pIndex) return;
				fade(index);
			});
			//绠ご鐐瑰嚮鍒囨崲
			domPrev.click(function(){
				fade(indexReduce());
			});
			domNext.click(function(){
				fade(indexAdd());
			});
			//绱㈠紩澧炲己鏁堟灉
			// indexAnimate(0);
			//鑷姩鍒囨崲
			auto();

			if(picLens==1){
				domIndex.hide();
			}
			//绠ご绉诲姩鏁堟灉
			arrowHover();

			//绱㈠紩澧炲己鏁堟灉
			function indexAnimate(idx,func){
				temp = domIndex.eq(idx);
				if(temp.length){
					temp.animate({"marginTop":"0px"},300,function(){
						indexAnimate(idx+1);
					});
				}else{
					return;
				}
			}

			//绠ご绉诲姩鏁堟灉
			function arrowHover(){
				domSlide.hover(function(){
					domPrev.stop(false,true).animate({"left":0},300);
					domNext.stop(false,true).animate({"right":0},300);
					
				},function(){
					domPrev.stop(false,true).animate({"left":"-"+that.cfg.arrowani+"px"},300);
					domNext.stop(false,true).animate({"right":"-"+that.cfg.arrowani+"px"},300);
					
				});
			}

			//鍥剧墖娣″叆娣″嚭鍒囨崲
			function fade(index){
				clearTimeout(tt);
				pIndex = index;
				currentPic.stop(false,true).fadeOut(800);
				currentPic =  domPics.eq(index).stop(false,true).fadeIn(800);
	            currentIndex.removeClass("on");
	            currentIndex = domIndex.eq(index).addClass("on");
	            domInfo.hide();
	            domInfo.eq(index).show();
	            auto();
			}

			//鑷姩杩愯
			function auto(){
				tt = setTimeout(function(){
					fade(indexAdd());
				},that.cfg.delay);
			}

			//Index澧炲姞
			function indexAdd(){
				return (index === picLens-1) ? index = 0 : ++index;
			}

			//Index鍑忓皯
			function indexReduce(){
				return (index === 0) ? index = picLens-1 : --index;
			}

		})($);
		
	}
}