/*  ImageNewsPlayer 
	Author:boyce(youngsoul@126.com | QQ:277841301)
	Date:2010.08.05
*/

/*
	主要属性*************************************************************
	jsonObj.jqImage:      要滚动的jQuery图像对象
	jsonObj.jqText:       滚动的图像对应的文字说明
	jsonObj.imageSub:     滚动图像内容的子元素标签
	jsonObj.textSub:      滚动文字内容的子元素标签
	jsonObj.visibleArea   显示区域尺寸，当元素总尺寸小于显示区域尺寸时不处理，不滚动
	interval:             滚动间隔时间	
	step:                 一次滚动的步长
	speed:                滚动速度，jQuery速度参数（normal,fast,slow 或 毫秒数值）
	axis:                 滚动的坐标轴（h：水平滚动，v：垂直滚动）
	direction             滚动的正负方向：向左向上为true,向右向下为false;
*/

function ImageNewsPlayer(jsonObj,interval,step,speed,axis,direction){
/*
	posA：      当前位置	
	crtNum:     当前显示内容的序号（从0 开始计）
	targetNum:  下一位置对应内容的序号（从0 开始计）
	total:      子内容的个数
	direction:	滚动方向（正负分别用true,false表示）
	intervalID: 事件ID
*/
	var classObj = this;
	var posA = 0;
	var crtNum = 0;	
	var total;
	this.intervalID;
	classObj.auto = true;
	
	var moveToPos = function(){}
	var calPos = function(){}
	
	//初始化:复制原滚动内容代码，用于实现无缝循环，如果是水平重新设置滚动内容的样式让其水平依次排开
	this.init = function(){		
		total = jsonObj.jqImage.children(jsonObj.imageSub).length;		
		if(total*step >jsonObj.visibleArea){			
			initContent();
		}else{
			return;
		}		
	}
	
	function initContent(){
		if(axis == "h"){			
			jsonObj.jqImage.html(jsonObj.jqImage.html()+jsonObj.jqImage.html());
			total = jsonObj.jqImage.children(jsonObj.imageSub).length;
			//设置水平元素的ID及位置		
			setSub(jsonObj.jqImage,jsonObj.imageSub);
			if(jsonObj.jqText){
				jsonObj.jqText.html(jsonObj.jqText.html() + jsonObj.jqText.html());
				setSub(jsonObj.jqText,jsonObj.textSub);
			}
			
			moveToPos = moveToPosH;
			calPos = calPosH;
		}else if(axis == "v"){			
			jsonObj.jqImage.html(jsonObj.jqImage.html()+jsonObj.jqImage.html());
			total = jsonObj.jqImage.children(jsonObj.imageSub).length;
			moveToPos = moveToPosV;
			calPos = calPosV;
		}
		
		//鼠标感应，移到滚动元素上时停止滚动，移出继续滚动
		jsonObj.jqImage.hover(function(){
			classObj.pause();
		},function(){
			classObj.resume();
		})
		
		if(jsonObj.jqText){
			jsonObj.jqText.hover(function(){
				classObj.pause();
			},function(){
				classObj.resume();
			})
		}
	}
	
	this.init();
	this.intervalID = setInterval(autoMove,interval);
	
	
	//水平滚动的时候设置各滚动元素及其子元素的属性位置等
	function setSub(obj,subEle){
		obj.css({width:total * step});
		obj.children(subEle).each(function(i){
			var tPos = i*step;
			jQuery(this).css({left:tPos});
		})
	}
	
	//计算水平滚动下一位置,n为几个步长step	
	function calPosH(n){
		if(direction){
			//向左移动时，如果滚动内容的当前位置left绝对值等于滚动内容的宽度一半时，重置left为0
			if(Math.abs(posA) == jsonObj.jqImage.width()/2){
				posA = 0;
				jsonObj.jqImage.css({left:posA});
				if(jsonObj.jqText)jsonObj.jqText.css({left:posA});
				posA = posA - n*step;
			}else{
				posA = posA - n*step;
			}
		}else{
			//向右滚动时，如果滚动内容的当前位置为0，重置left为滚动内容宽度的一半
			if(posA == 0){
				posA = -jsonObj.jqImage.width()/2;
				jsonObj.jqImage.css({left:posA});
				if(jsonObj.jqText)jsonObj.jqText.css({left:posA});
				posA = posA + n*step;
			}else{
				posA = posA + n*step;
			}
		}
	}
	
	//计算垂直滚动下一位置,n为几个步长step		
	function calPosV(n){
		if(direction){
			//向上移动时，如果滚动内容的当前位置top绝对值等于滚动内容的高度一半时，重置top为0
			if(Math.abs(posA) == jsonObj.jqImage.height()/2){
				posA = 0;
				jsonObj.jqImage.css({top:posA});
				if(jsonObj.jqText)jsonObj.jqText.css({top:posA});
				posA = posA - n*step;
			}else{
				posA = posA - n*step;
			}
		}else{
			//向下滚动时，如果滚动内容的当前位置为0，重置top为滚动内容高度的一半
			if(posA == 0){
				posA = -jsonObj.jqImage.height()/2;
				jsonObj.jqImage.css({top:posA});
				if(jsonObj.jqText)jsonObj.jqText.css({top:posA});
				posA = posA + n*step;
			}else{
				posA = posA + n*step;
			}
		}
	}
	
	//水平滚动函数
	function moveToPosH(){	
		activeButton();
		jsonObj.jqImage.animate({left:posA},speed);
		if(jsonObj.jqText)
		jsonObj.jqText.animate({left:posA},speed);		
	}
	//垂直滚动函数
	function moveToPosV(){
		activeButton();
		jsonObj.jqImage.animate({top:posA},speed);
		if(jsonObj.jqText)
		jsonObj.jqText.animate({top:posA},speed);
	}
	
	//功能函数,对外接口
	//next() 下一个，即：向右或向下的下一个
	this.next = function(){		
		direction = false;
		calPos(1);
		moveToPos();
	}
	//prev() 上一个，即：向左或向上的上一个
	this.prev = function(){
		direction = true;
		calPos(1);
		moveToPos();
	}
	//gotoPos()  移动到第几个，n 为滚动子元素序号，从0 开始计
	this.gotoPos = function(n){
		var tLen;		
		
		crtNum = Math.abs(posA/step);
		crtNum = crtNum % (total/2);		
		tLen = n - crtNum;
		
		//判断点击的序号与当前序号的位置关系，看需要向左向上还是向右向下移动		
		if(tLen > 0){
			direction = true;
		}else if(tLen < 0){
			direction = false;
		}
		//点击的序号不是当前就超上面确定的方向移动tLen个步长
		if(tLen){
			calPos(Math.abs(tLen));
			moveToPos();
		}
	}
	
	this.pause = function(){clearInterval(this.intervalID);}
	this.resume = function(){this.intervalID = setInterval(autoMove,interval);}
	
	//自动播放
	function autoMove(){		
		if(classObj.auto){
			calPos(1);
			moveToPos();
		}else{
			clearInterval(this.intervalID);
			return;
		}
	}
	
	function activeButton(){
		
		crtNum = Math.abs(posA/step);
		crtNum = crtNum % (total/2);
		
		if(jsonObj.jqBtn){
			jsonObj.jqBtn.each(function(i){
				if(i == crtNum){
					jQuery(this).addClass("active");
				}else{
					jQuery(this).removeClass("active");
				}
			})
		}else{
			return;
		}
	}
	
	
	this.fixID = function(idstr){
		var fixLen = jsonObj.jqImage.find(idstr).length;
		jsonObj.jqImage.find(idstr).each(function(i){			
			if(i >= fixLen/2){
				var tID = jQuery(this).attr("id") + "add";				
				jQuery(this).attr("id", tID);
			}
		})
	}
	
}
