//********************************
// * JavaScript Document
// * plugin.js
// * 渲染&交互&功能 小库(上线版)
// *
// * @Panda.Lee 
// *    Front-End Web PM
// *    Panda@moko.cn
// * @date
// * @link 
//********************************



//自动轮播系统
var anyPlay = {

	start : function(id,type,time,cls) {

				//初始化type参数
				if (anyPlay.test(type)) {
					type = type.split(",").slice(0,4); //截取传递的值的前二个index赋值给type
				} else {
					var type = Array();
				}

				//初始化time参数 当传递的值不能转化成数字或者小于minTime的时候 time等于默认值dTime 当time为数字并且不小于minTime的时候 time等于传递的值
				var minTime = '1000', //定义最小间隔时间
					dTime = '5000'; //定义默认间隔时间
				Number(time) >= minTime && !isNaN(Number(time)) ? time = time : time = dTime ;

				//初始化cls参数 当传递的值是字符串并且不为空的时候 声明cls为数组 并把传递的值赋给cls 否则初始化为默认值dCls
				var dCls = ["tabBox","alive","bd bC","b_1 bC"] //定义默认className
				if (anyPlay.test(cls)) {
					cls = cls.split(",").slice(0,4); //截取传递的值的前三个index赋值给cls
					if (!anyPlay.test(cls[1])) { //如果[1]不是非空的字符串，初始化默认值dCls的[1]，下同
						cls[1] = dCls[1];
					}
					if (!anyPlay.test(cls[2])) {
						cls[2] = dCls[2];
					}
					if (!anyPlay.test(cls[3])) {
						cls[3] = dCls[3];
					}
				} else {
					cls = dCls;
				}
				//alert(id + '\n' + type + '\n' + time + '\n' + cls);
				
				//如果帧内容的链接为空连接，用图片节点替换链接节点
				$('#' + id + ':visible .' + cls[0]).each(
					function() {
						if ($(this).find('[href *= javascript]').length > 0) {
							$(this).find('img').attr('class','bd');
							$(this).find('[href *= javascript]').replaceWith($(this).find('img'));
						}
					}
				)

				var max = $('#' + id + ':visible .' + cls[0]).length; //获取播放帧数量
				if (max <= 1) { //如果不大于1则停止并退出
					return;
				}

				anyPlay.addControl(id,type,time,cls,max);
			},

	addControl : function(id,type,time,cls,max) {
					 //声明自动播放控制变量
					 var t;

					 //初始化下方的控制器部分
					 var $box = $('#' + id + ':visible'); //获取所需要的选择集
					 for (i = 0; i < max; i++) { //为帧容器添加ID值
						 $box.find('.' + cls[0]).eq(i).attr('id',id + '_' + (i + 1));
					 }
					 $box.append('<p class="control"></p>'); //初始化control部分HTML
					 //根据type[0]值添加不同控件
					 switch (type[0]) {
						 case '0' :
							 for (i = 0; i < max; i++) {
								 $box.children('.control').append('<a id="' + id + '_C_' + (i + 1) + '" class="tabBg" href="javascript:void(0);" title="" hidefocus="true"></a>');
							 }
							 $box.children('.control').css('textAlign','center');

							 //获取自动播放控制参数type[1]，如果值未定义或不为0或1，赋值默认为1
							 anyPlay.test(type[1]) && type[1].length == 1 && (type[1] == 0 || type[1] == 1) ? type[1] = type[1] : type[1] = 1;

							 //绑定单击事件
							 $box.find('.tabBg').click(
								 function() {
									 clearInterval(t); //清空自动播放变量
									 var obj = $(this);
									 anyPlay.change(id,type,cls,max,'1',obj);
									 if (type[1] == 1) { //如果type[1]的值为1开始自动播放
										 t = setInterval(function() {anyPlay.change(id,type,cls,max);},time);
									 }
								 }
							 );
							 $box.find('#' + id + '_C_' + anyPlay.justNow(id,cls)).attr('class','tabBg_1'); //根据当前帧ID序号值设置当前状态
							 //如果type[1]的值为1开始自动播放
							 if (type[1] == 1) {
								 //根据type[3]的值控制是否等待图片加载完毕在开始自动播放
								 //默认不等待 0:只有第一次的时候等待
								 switch (type[3]) {
									 case '0' :
										 $('#' + id + ':visible #' + id + '_' + anyPlay.justNow(id,cls) + ' img').load(
											 function() {
												 t = setInterval(function() {anyPlay.change(id,type,cls,max);},time);
											 }
										 );
										 break;
									 default :
										 t = setInterval(function() {anyPlay.change(id,type,cls,max);},time);
										 break;
								 }
							 }
							 break;

						 case '1' :
							 for (i = 0; i < max; i++) {
								 $box.children('.control').append('<a id="' + id + '_C_' + (i + 1) + '" class="tabBg" href="javascript:void(0);" title="" hidefocus="true"></a>');
							 }
							 $box.children('.control').append('<a class="lastTab" href="javascript:void(0);" title="" hidefocus="true"></a><a class="nextTab" href="javascript:void(0);" title="" hidefocus="true"></a>');
							 $box.children('.control').css('textAlign','center');

							 //获取自动播放控制参数type[1]，如果值未定义或不为0或1，赋值默认为0
							 anyPlay.test(type[1]) && type[1].length == 1 && (type[1] == 0 || type[1] == 1) ? type[1] = type[1] : type[1] = 0;

							 //绑定单击事件
							 $box.find('.tabBg').click(
								 function() {
									 clearInterval(t); //清空自动播放变量
									 var obj = $(this);
									 anyPlay.change(id,type,cls,max,'1',obj);
									 if (type[1] == 1) { //如果type[1]的值为1开始自动播放
										 t = setInterval(function() {anyPlay.change(id,type,cls,max);},time);
									 }
								 }
							 );
							 $box.find('.lastTab').click(
								 function() {
									 clearInterval(t); //清空自动播放变量
									 anyPlay.change(id,type,cls,max,'0');
									 if (type[1] == 1) { //如果type[1]的值为1开始自动播放
										 t = setInterval(function() {anyPlay.change(id,type,cls,max);},time);
									 }
								 }
							 );
							 $box.find('.nextTab').click(
								 function() {
									 clearInterval(t); //清空自动播放变量
									 anyPlay.change(id,type,cls,max);
									 if (type[1] == 1) { //如果type[1]的值为1开始自动播放
										 t = setInterval(function() {anyPlay.change(id,type,cls,max);},time);
									 }
								 }
							 );
							 $box.find('#' + id + '_C_' + anyPlay.justNow(id,cls)).attr('class','tabBg_1'); //根据当前帧ID序号值设置当前状态
							 //如果type[1]的值为1开始自动播放
							 if (type[1] == 1) {
								 //根据type[3]的值控制是否等待图片加载完毕在开始自动播放
								 //默认不等待 0:只有第一次的时候等待
								 switch (type[3]) {
									 case '0' :
										 $('#' + id + ':visible #' + id + '_' + anyPlay.justNow(id,cls) + ' img').load(
											 function() {
												 t = setInterval(function() {anyPlay.change(id,type,cls,max);},time);
											 }
										 );
										 break;
									 default :
										 t = setInterval(function() {anyPlay.change(id,type,cls,max);},time);
										 break;
								 }
							 }
							 break;

						 case '2' :
							 for (i = 0; i < max; i++) {
								 $box.children('.control').append('<span id="' + id + '_C_' + (i + 1) + '" class="tabBg"></span>');
							 }
							 $box.children('.control').append('<a class="lastTab" href="javascript:void(0);" title="" hidefocus="true"></a><a class="nextTab" href="javascript:void(0);" title="" hidefocus="true"></a>');
							 $box.children('.control').css('textAlign','center');

							 //获取自动播放控制参数type[1]，如果值未定义或不为0或1，赋值默认为0
							 anyPlay.test(type[1]) && type[1].length == 1 && (type[1] == 0 || type[1] == 1) ? type[1] = type[1] : type[1] = 0;

							 //绑定单击事件
							 $box.find('.lastTab').click(
								 function() {
									 clearInterval(t); //清空自动播放变量
									 anyPlay.change(id,type,cls,max,'0');
									 if (type[1] == 1) { //如果type[1]的值为1开始自动播放
										 t = setInterval(function() {anyPlay.change(id,type,cls,max);},time);
									 }
								 }
							 );
							 $box.find('.nextTab').click(
								 function() {
									 clearInterval(t); //清空自动播放变量
									 anyPlay.change(id,type,cls,max);
									 if (type[1] == 1) { //如果type[1]的值为1开始自动播放
										 t = setInterval(function() {anyPlay.change(id,type,cls,max);},time);
									 }
								 }
							 );
							 $box.find('#' + id + '_C_' + anyPlay.justNow(id,cls)).attr('class','tabBg_1'); //根据当前帧ID序号值设置当前状态

							 //如果type[1]的值为1开始自动播放
							 if (type[1] == 1) {
								 //根据type[3]的值控制是否等待图片加载完毕在开始自动播放
								 //默认不等待 0:只有第一次的时候等待
								 switch (type[3]) {
									 case '0' :
										 $('#' + id + ':visible #' + id + '_' + anyPlay.justNow(id,cls) + ' img').load(
											 function() {
												 t = setInterval(function() {anyPlay.change(id,type,cls,max);},time);
											 }
										 );
										 break;
									 default :
										 t = setInterval(function() {anyPlay.change(id,type,cls,max);},time);
										 break;
								 }
							 }
							 break;

						 default :
							 for (i = 0; i < max; i++) {
								 $box.children('.control').append('<a id="' + id + '_C_' + (i + 1) + '" class="' + cls[3] +'" href="javascript:void(0);" title="" hidefocus="true">' + (i + 1) + '</a>');
							 }
							 $box.children('.control').addClass('page');

							 //获取自动播放控制参数type[1]，如果值未定义或不为0或1，赋值默认为1
							 anyPlay.test(type[1]) && type[1].length == 1 && (type[1] == 0 || type[1] == 1) ? type[1] = type[1] : type[1] = 1;

							 //绑定单击事件
							 $box.find('.control a').click(
								 function() {
									 clearInterval(t); //清空自动播放变量
									 var obj = $(this); //声明当前对象
									 anyPlay.change(id,type,cls,max,'1',obj);
									 if (type[1] == 1) { //如果type[1]的值为1开始自动播放
										 t = setInterval(function() {anyPlay.change(id,type,cls,max);},time);
									 }
								 }
							 );
							 $box.find('#' + id + '_C_' + anyPlay.justNow(id,cls)).attr('class',cls[2]); //根据当前帧ID序号值设置当前状态

							 //如果type[1]的值为1开始自动播放
							 if (type[1] == 1) {
								 //根据type[3]的值控制是否等待图片加载完毕在开始自动播放
								 //默认不等待 0:只有第一次的时候等待
								 switch (type[3]) {
									 case '0' :
										 $('#' + id + ':visible #' + id + '_' + anyPlay.justNow(id,cls) + ' img').load(
											 function() {
												 t = setInterval(function() {anyPlay.change(id,type,cls,max);},time);
											 }
										 );
										 break;
									 default :
										 t = setInterval(function() {anyPlay.change(id,type,cls,max);},time);
										 break;
								 }
							 }
							 break;
					 }
				 },

	test : function(t) {
			   //判断参数t是否是不为空的字符串并返回布尔值，真表示这个参数是一个非空字符串  /^\s*$/g.test(cls)是判断t是否为空
			   return (typeof(t) != 'undefined' && !/^\s*$/g.test(t));
		   },

	cleanBlank : function(obj) {
			   //去除参数obj对象html
			   var $text = obj.text();
			   $text = $text.replace(/\s/g,'');
			   obj.text($text);
		   },

	change : function(id,type,cls,max,mode,obj) {
				 //mode:表示事件的类型：默认表示下一个，0表示上一个，1表示跳转到当前单击的
				 //根据type[2]的值设定不同切换特效
				 //obj传递的是当前对象
				 
				 //如果当前可见帧正在动画中，事件退出
				 var $idNum = anyPlay.justNow(id,cls);
				 if ($('#' + id + ':visible .playArea').is(':animated')) {
					 return;
				 }
				 if ($('#' + id + ':visible #' + id + '_' + $idNum).is(':animated')) {
					 return;
				 }
				 //根据mode值初始化$idNum值
				 switch (mode) {
					 case '0' :
						 $idNum > 1 ? $idNum-- : $idNum = max;
						 break;
					 case '1' :
						 //如果参数obj不是非空字符串，或者已经是当前可见的帧的时候退出
						 if (!anyPlay.test(obj) || obj.hasClass('tabBg_1') || obj.hasClass(cls[2])) {
							 return;
						 }
						 var $idNum = anyPlay.justNow(id,cls,obj); //获取当前点击对象的ID序号
						 break;
					 default :
						 $idNum < max ? $idNum++ : $idNum = 1;
						 break;
				 }

			 //根据type[2]值处理动画
				 //初始化所有控制器状态，根据当前可见帧ID序号获取对应控制器，并根据其className操作
				 switch ($('#' + id + ':visible #' + id + '_C_' + anyPlay.justNow(id,cls)).attr('class')) {
				     case cls[2] :
				  	     $('#' + id + ':visible #' + id + '_C_' + anyPlay.justNow(id,cls)).attr('class',cls[3]);
				  	     break;
				     default :
				  	     $('#' + id + ':visible #' + id + '_C_' + anyPlay.justNow(id,cls)).attr('class','tabBg');
				  	     break;
				 }
				 //获取$idNum值对应的对象，并设置为激活状态
				 switch ($('#' + id + ':visible #' + id + '_C_' + $idNum).attr('class')) {
					 case cls[3] : //如果单击的对象className = cls[3]，则替换为cls[2]
						 $('#' + id + ':visible #' + id + '_C_' + $idNum).attr('class',cls[2]);
						 break;
					 default :
						 $('#' + id + ':visible #' + id + '_C_' + $idNum).attr('class','tabBg_1');
						 break;
				 }
				 //处理帧动画效果
				 switch (type[2]) {
					 case '0' :
						 $('#' + id + ':visible .' + cls[0])
							 .css('opacity','0')
							 .removeClass(cls[1]);
						 $('#' + id + ':visible #' + id + '_' + $idNum)
							 .addClass(cls[1])
							 .animate({opacity : '1'},500);
						 break;
					 case '1' :
						 var $box = $('#' + id);
						 var $tabWidth = $('#' + id + ':visible .' + cls[0]).width();
						 var $playArea = $('#' + id + ':visible .playArea');
						 var $idHtml = Array();
						 for (i = 0; i < max; i++) {
							 $idHtml[i] = $('#' + id + ':visible #' + id + '_' + (i + 1));
						 }
						 $playArea.empty();
						 for (i = 0; i < max; i++) {
							 $playArea.append($idHtml[i]);
						 }
						 $box.css({
								 'width':$tabWidth,
								 'overflow-x':'hidden'
							 });
						 $playArea
							 .width($tabWidth * max)
							 .addClass('cf');
						 $('#' + id + ':visible .' + cls[0]).css({
								 'display':'block',
								 'float':'left'
							 });
						 $playArea.animate({marginLeft:- $tabWidth * ($idNum - 1)},500);
						 //获取当前可见的帧并隐藏
						 $('#' + id + ':visible .' + cls[0]).removeClass(cls[1]);
						 //获取ID序号对应的帧并设置可见
						 $('#' + id + ':visible #' + id + '_' + $idNum).addClass(cls[1]);
						 break;
					 default :
						 //获取当前可见的帧并隐藏
						 $('#' + id + ':visible .' + cls[0]).removeClass(cls[1]);
						 //获取ID序号对应的帧并设置可见
						 $('#' + id + ':visible #' + id + '_' + $idNum).addClass(cls[1]);
						 break;
				 }

			 },

	justNow : function(id,cls,obj) {
				  //obj传递的是当前对象
				  //返回当前帧容器的ID值的序号
				  var $idNum;
				  //如果参数obj是一个非空字符串，则根据obj值返回当前单击的元素ID序号。否则则返回当前可见帧的ID序号。
				  if (anyPlay.test(obj)) {
					  $idNum = obj.attr('id').slice((id + '_C_').length); //获取单击元素的ID序号
				  } else {
					  if ($('#' + id + ':visible .' + cls[0]).length < 1) {
						  $idNum = 1; //如果当前帧都是不可见的，则默认赋值为1
						  return;
					  }
					  $('#' + id + ':visible .' + cls[0]).each(
						  function() {
							  if ($(this).hasClass(cls[1])) {
								  $idNum = $(this).attr('id').slice((id + '_').length); //获取当前可见帧的ID序号
							  }
						  }
					  );
				  }
				  return $idNum;
			  }

}


