(function($){
if(!$.registerNameSpace("ydapp.autoSlideClass")){return false;};
/**
* @desc 内容轮播滚动类
* @调用方法
(new $.ydapp.autoSlideClass.Class({
	"id_class":".conCase",
	"listArr":conListArr,
	"indexImg":imgIndex,
	"leftBar":".leftBar",
	"rightBar":".rightBar",
	"position":"left-right",
	"autoPlay":true,
	"step":51,
	"time":0,
	"moveDomNumber":3,
	"pauseTime":6000
})).Start();

var options={
	id_class:"列表容器#id或者.class",
	listArr:[<li></li>],
	indexImg:0,
	leftBar:obj,
	rigthBar:obj,
	numBar:obj,
	 position:"移动方向",
	autoPlay:true,
	step:"一次移动距离",
	time:"执行移动步长间隔时间",
	pauseTime:"暂停移动间隔时间",
	moveDomNumber:"一次移动多少个list dom",
	callback:function(){}
}
*/
$.registerNameSpace("ydapp.autoSlideClass");
/**************************************************************************************/
/*视频首页、小游戏-全部专题 滚动方法*/
$.ydapp.autoSlideClass.videoIndexSlides=function(sildeCaseId, leftBarId, rightBarId, numBarId){
	var showlen=1;
	var conListArr=[];
	var numBarArr=[];
	$(sildeCaseId).children().children().each(function(i){
		conListArr.push($(this).get(0).outerHTML);			
		if(i==0){
			numBarArr.push('<a class="active" href="javascript:;" index="'+i+'">'+parseInt(i+1)+'</a>');
		}else{
			$(this).remove();
			numBarArr.push('<a href="javascript:;" index="'+i+'">'+parseInt(i+1)+'</a>');
		}
	});
	if(conListArr.length<=1){
		var leftBarClass=$(leftBarId).attr("class");
		var rigthBarClass=$(rightBarId).attr("class");
		$(leftBarId).removeClass().addClass(leftBarClass+"_none").focus(function(){this.blur();});
		$(rightBarId).removeClass().addClass(rigthBarClass+"_none").focus(function(){this.blur();});
		return;
	};
	if(numBarId){
		$(numBarId).html(numBarArr.join(""));
	};
	//$(sildeCaseId).empty().html(conListArr[0]);
	(new this.Class({
		"position":"left-right",
		"easing":"easeInQuad",
		"id_class":sildeCaseId,
		"listArr":conListArr,
		"leftBar":leftBarId,
		"rightBar":rightBarId,
		"numBar":numBarId,
		"numBarEventName":"mousemove",
		"autoPlay":false,
		"time":500,
		"moveDomNumber":1,
		"pauseTime":6000
	})).Start();
};
/**************************************************************************************/
/*详情页截图轮播*/
$.ydapp.autoSlideClass.content_imgSlide=function(sildeCaseId, leftBarId, rightBarId,numBarId){
	var conListArr=[];
	var numBarArr=[];
	$(sildeCaseId+" li").each(function(i){
		var imgObj=$(this).find("img");
		(new Image()).src=imgObj.attr("rel");
		$(this).css({background:"url("+imgObj.attr("rel")+") no-repeat center center"});
		imgObj.remove();
		conListArr.push($(this).get(0).outerHTML);
		if(i==0){
			numBarArr.push('<a class="active" href="javascript:;" index="'+i+'">'+i+'</a>');
		}else{
			numBarArr.push('<a href="javascript:;" index="'+i+'">'+i+'</a>');
		}
	});
	if(numBarId){
		$(numBarId).html(numBarArr.join(""));
	};
	if(conListArr.length<=1){
		$(numBarId).hide();
		var leftBarClass=$(leftBarId).attr("class");
		var rigthBarClass=$(rightBarId).attr("class");
		$(leftBarId).removeClass().addClass(leftBarClass+"_none").focus(function(){this.blur();});
		$(rightBarId).removeClass().addClass(rigthBarClass+"_none").focus(function(){this.blur();});
		return;
	};
	$(sildeCaseId+" > ul").empty().html(conListArr[0]);
	(new this.Class({
		"position":"left-right",
		"easing":"easeInOutCirc",
		"id_class":sildeCaseId,
		"listArr":conListArr,
		"leftBar":leftBarId,
		"rightBar":rightBarId,
		"numBar":numBarId,
		"numBarEventName":"mousemove",
		"autoPlay":true,
		"time":300,
		"moveDomNumber":1,
		"pauseTime":6000,
		"defaultShowLen":0//首次默认显示长度
	})).Start();
};
/**************************************************************************************/
/*图片轮播方法*/
$.ydapp.autoSlideClass.imgSlide=function(sildeCaseId, leftBarId, rightBarId,numBarId,slideTextId,callback,time){
	var conListArr=[];
	var numBarArr=[];
	var conTextArr=[];
	$(sildeCaseId+" li").each(function(i){
		conListArr.push($(this).get(0).outerHTML);
		if(slideTextId){
			conTextArr.push($(slideTextId).find("li").get(i).outerHTML);
		};
		if(i==0){
			numBarArr.push('<a class="active" href="javascript:;" index="'+i+'">'+i+'</a>');
		}else{
			numBarArr.push('<a href="javascript:;" index="'+i+'">'+i+'</a>');
		};
		(new Image()).src=$(this).find("img").attr("src");
	});
	if(numBarId){
		$(numBarId).html(numBarArr.join(""));
	};
	if(slideTextId){
		$(slideTextId).empty().html(conTextArr[0]);
	};
	if(conListArr.length<=1){
		$(leftBarId).remove();
		$(rightBarId).remove();
		return;
	};
	$(sildeCaseId+" > ul").empty().html(conListArr[0]).parent().parent().hover(function(){
		$(leftBarId+","+rightBarId).show();
	},function(){
		$(leftBarId+","+rightBarId).hide();
	});
	(new this.Class({
		"position":"left-right",
		"easing":"easeInOutCirc",
		"id_class":sildeCaseId,
		"listArr":conListArr,
		"leftBar":leftBarId,
		"rightBar":rightBarId,
		"numBar":numBarId,
		"conText":slideTextId,
		"conTextArr":conTextArr,
		"numBarEventName":"mousemove",
		"autoPlay":true,
		"time":time||400,
		"moveDomNumber":1,
		"pauseTime":3000,
		"callback":callback
	})).Start();
};
/*回调滚动图片说明文本方法*/
$.ydapp.autoSlideClass.imgSlide.scrollText=function(){
	if(typeof arguments[0] =="undefined"){return false;}
	var that=arguments[0];
	var conText= that.conText;
	if(that.domAppendWhere=="prepend"){
		conText.prepend(that.conTextArr[that.backcallIndexImg[0]]);
		conText.scrollTop(conText.height());
		move(-1);
	}else{
		conText.append(that.conTextArr[that.backcallIndexImg[0]]);
		move(1);
	}
	function move(n){
		var _h=(n<0)?0:conText.height();
		conText.animate({scrollTop: _h},that.animateTime,that.easing,function(){
			if(n<0){
				conText.find("li").last().remove();
			}else{
				conText.find("li").first().remove();
				conText.scrollTop(0);
			}
		});
	}
};
/**************************************************************************************/
/*视频首页图片轮播方法*/
$.ydapp.autoSlideClass.imgSlide_video_index=function(sildeCaseId, leftBarId, rightBarId,slideTextId,callback,time){
	var conListArr=[];
	var conTextArr=[];
	$(sildeCaseId+" li").each(function(i){
		conListArr.push($(this).get(0).outerHTML);
		if(slideTextId){
			conTextArr.push($(slideTextId).find("li").get(i).outerHTML);
			(new Image()).src=$(slideTextId).find("li").eq(i).find("img").attr("src");
		};
		(new Image()).src=$(this).find("img").attr("src");
	});
	if(slideTextId){
		$(slideTextId).empty().html(conTextArr[0]);
	};
	if(conListArr.length<=1){
		$(leftBarId).remove();
		$(rightBarId).remove();
		return;
	};
	$(sildeCaseId+" > ul").empty().html(conListArr[0]+conListArr[1]+conListArr[2]).parent().parent().hover(function(){
		$(leftBarId).show();
	},function(){
		$(leftBarId+","+rightBarId).hide();
	});
	(new this.Class({
		"position":"up-down",
		"id_class":sildeCaseId,
		"listArr":conListArr,
		"leftBar":leftBarId,
		"rightBar":rightBarId,
		"conText":slideTextId,
		"conTextArr":conTextArr,
		"numBarEventName":"mousemove",
		"domAppendWhere":"prepend",
		"autoPlay":true,
		"time":time||500,
		"moveDomNumber":1,
		"pauseTime":3000,
		"callback":callback
	})).Start();
};
/*视频首页轮播回调Opacity轮换图片方法*/
$.ydapp.autoSlideClass.imgSlide.opacityImg=function(){
	if(typeof arguments[0] =="undefined"){return false;}
	var that=arguments[0];
	var opacityCase= that.conText;
	var conTextArr=that.conTextArr;
	var listLen=conTextArr.length;
	var _index=that.indexImg+3-listLen;
	if(_index<0){
		_index=listLen+_index;
	};
	opacityCase.append(conTextArr[_index]).find("li").eq(1).hide().fadeIn(that.animateTime,function(){
		opacityCase.children().first().remove();
	});
};
/**************************************************************************************/
/*内容滚动方法*/
$.ydapp.autoSlideClass.type0=function(sildeCaseId, leftBarId, rightBarId, showlen,autoPlay,time,pauseTime){
	showlen=showlen||1;//默认显示数量
	var conListArr=[];
	$(sildeCaseId+" li").each(function(){
		conListArr.push($(this).get(0).outerHTML);			 
	});
	if(conListArr.length<=showlen){
		var leftBarClass=$(leftBarId).attr("class");
		var rigthBarClass=$(rightBarId).attr("class");
		$(leftBarId).removeClass().addClass(leftBarClass+"_none").focus(function(){this.blur();});
		$(rightBarId).removeClass().addClass(rigthBarClass+"_none").focus(function(){this.blur();});
		return;
	};
	var imgIndex=0;
	var tmpHtml=conListArr.slice(imgIndex);
	if(tmpHtml.length<showlen){
		tmpHtml=tmpHtml.concat(conListArr.slice(0,showlen-tmpHtml.length));
	}else{
		tmpHtml=tmpHtml.slice(0,showlen);
	};
	$(sildeCaseId+" > ul").empty().html(tmpHtml.join(""));
	(new this.Class({
		"position":"left-right",
		"easing":"easeInOutCirc",
		"id_class":sildeCaseId,
		"listArr":conListArr,
		"leftBar":leftBarId,
		"rightBar":rightBarId,
		"autoPlay":autoPlay,
		"time":time||500,
		"moveDomNumber":showlen,
		"pauseTime":pauseTime||6000
	})).Start();
};
/**************************************************************************************/
/*滚动类*/
$.ydapp.autoSlideClass.Class=function(options){
	options=options||{};
	this.Timer = [null,null];
	this.id_class=options.id_class||null;
	this.leftBar=($(options.leftBar).length>0)?$(options.leftBar):null;
	this.rightBar=($(options.rightBar).length>0)?$(options.rightBar):null;
	this.numBar=($(options.numBar).length>0)?$(options.numBar):null;
	this.numBarEventName=options.numBarEventName||"mouseover";
	this.conText=($(options.conText).length>0)?$(options.conText):null;
	this.conTextArr=options.conTextArr||[];
	this.position=options.position||"left-right";
	this.easing=options.easing||"easeOutQuad";//缓动方法 使用easing插件
	this.animateTime=options.time||0;//动画持续时间
	this.autoPlay=options.autoPlay||false;
	this.domAppendWhere=options.domAppendWhere||"append";
	this.pauseTime=options.pauseTime||6000;
	this.indexImg=options.indexImg||0;
	this.moveDomNumber=options.moveDomNumber||1;
	this.firstShowLen=options.defaultShowLen||0;//显示多少张图
	this.firstShowEvent=false;//显示多少张图状态之下，滚动时indexImg的确定
	this.randIndexImg=this.firstShowLen?Math.abs(this.firstShowLen-this.moveDomNumber):0;
	this.fn=options.callback||function(){};
	this.barClickPlay=this.autoPlay||false;//未设置自动播放，控制bar事件启动自动播放
	this.conCase=$(this.id_class);
	this.conCaseWidth=this.conCase.innerWidth()||620;
	this.conCaseHeight=this.conCase.innerHeight()||200;
	this.ulObj=this.conCase.children();
	this.conLiWidth=this.ulObj.children().innerWidth()||120;
	this.conLiHeight=this.ulObj.children().innerHeight()||180;
	this.listArr=options.listArr||[];
	this.isMoving=false;
	this.Start=function(){
		if(this.position=="left-right"){
			this.ulObj.width(this.conCaseWidth*this.moveDomNumber*2.5);//初始化UL宽度
		}else{
			this.ulObj.width("105%");
			this.ulObj.height(this.conCaseHeight*this.moveDomNumber*2.5);//初始化UL高度
		};
		var that=this;
		
		if(this.leftBar){
			this.leftBar.focus(function(){$(this).blur()}).click(function(){
				if(that.isMoving){return false;};
				that.reWriteList(that.addDomList("prepend"));
				return false;
			}).hover(function(){that.autoPlay=false;that.Stop();},function(){that.reStart();});
		};
		
		if(this.rightBar){
			this.rightBar.focus(function(){$(this).blur()}).click(function(){
				if(that.isMoving){return false;};
				that.reWriteList(that.addDomList("append"));
				return false;
			}).hover(function(){that.autoPlay=false;that.Stop();},function(){that.reStart();});
		};
		
		if(this.numBar){
			var clickTimer=100;
			if(this.numBarEventName=="click"){
				this.numBar.find("a").bind("mousemove",function(){
					clickTimer=0;
					that.autoPlay=false;
					that.Stop();								   
				});
			};
			this.numBar.find("a").unbind().focus(function(){$(this).blur()}).bind(this.numBarEventName,function(){
				that.autoPlay=false;
				that.Stop();
				if(that.isMoving){return;}
				var index=parseInt($(this).attr("index"));
				clearTimeout(that.Timer[1]);
				that.Timer[1]=setTimeout(function(){
					if(that.indexImg==index){
						return;
					}else if(that.indexImg>index){
						that.indexImg=index+1;
						that.reWriteList(that.addDomList("prepend"));
					}else{
						that.indexImg=index-1;
						that.reWriteList(that.addDomList("append"));
					}
					that.numBar.find("a").removeClass("active").eq(index).addClass("active");
				 },clickTimer);
				return false;
			}).bind("mouseout",function(){
				clearTimeout(that.Timer[1]);
				that.reStart();
			});
		}
		
		if(this.conText){
			this.conText.hover(function(){that.autoPlay=false;that.Stop();},function(){that.reStart();});
		};
		this.conCase.hover(function(){that.autoPlay=false;that.Stop();},function(){that.reStart();});
		if(this.autoPlay){
			this.reStart();
		};
	};
	this.addDomList=function(where){
		this.domAppendWhere=where;
		var listLen=this.listArr.length;
		switch(where){
			case "prepend":
				if(!this.firstShowLen){
					this.indexImg=(this.indexImg-this.randIndexImg<=0)?Math.abs(listLen-this.indexImg):this.indexImg-this.randIndexImg;
					this.firstShowLen=true;
				};
				var indexArr=[this.indexImg];
				for(var i=0;i<this.moveDomNumber;i++){
					indexArr.push((indexArr[i]-1<0)?listLen-1:indexArr[i]-1);
				};
				indexArr.shift();
				if(this.indexImg==indexArr[indexArr.length-1]){return [];}
				this.indexImg=indexArr[indexArr.length-1];
				break;
			default:
				if(this.firstShowLen){
					this.indexImg=(this.indexImg+this.randIndexImg>=listLen)?Math.abs(listLen-this.indexImg-this.randIndexImg):this.indexImg+this.randIndexImg;
					this.firstShowLen=false;
				};
				var index=this.indexImg+this.moveDomNumber-1;
				index=(index>=listLen)?Math.abs(listLen-index):index;
				var indexArr=[index];
				for(var i=0;i<this.moveDomNumber;i++){
					indexArr.push((indexArr[i]+1>=listLen)?0:indexArr[i]+1);
				};
				indexArr.shift();
				if(this.indexImg==indexArr[0]){return [];};
				this.indexImg=indexArr[0];
				break;
		}
		return indexArr;
	},
	this.reWriteList=function(indexArr){
		this.backcallIndexImg=indexArr;//此属性回调函数中使用
		if(indexArr.length<1){return;};
		for(var i=0,l=indexArr.length;i<l;i++){
			if(this.domAppendWhere=="prepend"){
				this.ulObj.prepend(this.listArr[indexArr[i]]);
				if(this.position=="left-right"){
					this.conCase.scrollLeft(this.conLiWidth*(i+1));
				}else{
					this.conCase.scrollTop(this.conLiHeight*this.moveDomNumber);
				};
				if(i>=l-1){
					this.Move(-1);
				}
			}else{
				this.ulObj.append(this.listArr[indexArr[i]]);
				if(i>=l-1){
					this.Move(1);
				}
			}
		};
		if(this.numBar){
			this.numBar.find("a").removeClass("active").eq(indexArr[0]).addClass("active");
		};
		this.fn(this);
	};
	this.Move=function(n){
		this.Stop();
		this.isMoving=true;
		var that=this;
		switch(this.position){
			case "left-right":
				var _w=(n<0)?0:this.conLiWidth*this.moveDomNumber;
				this.conCase.animate({scrollLeft: _w},this.animateTime,this.easing,function(){
					that.isMoving=false;
					if(n<0){
						for(var i=0;i<that.moveDomNumber;i++){
							that.ulObj.children().last().remove();
						}
					}else{
						for(var i=0;i<that.moveDomNumber;i++){
							that.ulObj.children().first().remove();
						}
						that.conCase.scrollLeft(0);
					};
					if(that.autoPlay){that.reStart();};
					return;								   
				});
				break;
			default:
				var _h=(n<0)?0:this.conLiHeight*this.moveDomNumber;
				this.conCase.animate({scrollTop: _h},this.animateTime,this.easing,function(){
					that.isMoving=false;
					if(n<0){
						for(var i=0;i<that.moveDomNumber;i++){
							that.ulObj.children().last().remove();
						}
					}else{
						for(var i=0;i<that.moveDomNumber;i++){
							that.ulObj.children().first().remove();
						}
						that.conCase.scrollTop(0);
					};
					if(that.autoPlay){that.reStart();};
					return;															   
				});
				break;
		}
	};
	this.Stop=function(){
		clearTimeout(this.Timer[0]);
	};
	this.reStart=function(){
		this.Stop();
		this.autoPlay=true;
		var that=this;
		if(this.autoPlay && this.barClickPlay){
			this.Timer[0]=setTimeout(function(){that.reWriteList(that.addDomList(that.domAppendWhere));},this.pauseTime);
		}
	}
}

})(jQuery);
