/*Roll无缝滚动
接受参数:无
返回类型:无

应用技术：
javascript

制作人：
黄若儒 Roy.Huang

注意:
1、全部采用静态/prototype写法，方便继承重载
2、通过IE\FireFox\Opera测试
3、太多啦~自己看着办吧！
*/
function Roll(){};

//==============================实例化方法==============================
Roll.prototype.source=null;//滚动源对象

Roll.prototype.panel=null;//滚动载入对象

Roll.prototype.div=null;//存放滚动体DIV

Roll.prototype.speed=100;//滚动速度

Roll.prototype.frame=1;//滚动帧数

Roll.prototype.direction="UP";//滚动方向

Roll.prototype.sH=0;//源对象高

Roll.prototype.sW=0;//源对象宽

Roll.prototype.height=null;//滚动体高度

Roll.prototype.width=null;//滚动体宽度

Roll.prototype.initTime=null;//实例化时间

Roll.prototype.interval=null;//方法容器

Roll.prototype.waitTime=1;//重载检测时间；

Roll.prototype.initState=0;//监测实例加载情况

Roll.prototype.start=function(){//开始滚动
	var base=this;
	switch(base.initState){
		case 0:return Roll.onError(0);
		case 1:setTimeout(function(){base.start()},base.waitTime);break;
		case 2:base.interval=setInterval(function(){eval("base."+base.direction.toUpperCase()+"()")},base.speed);break;
	}
	return true;
};

Roll.prototype.stop=function(){//停止滚动
	var base=this;
	switch(base.initState){
		case 0:return Roll.onError(0);
		case 1:setTimeout(function(){base.stop()},base.waitTime);break;
		case 2:clearInterval(base.interval);break;
	}
	return true;
};

Roll.prototype.UP=function(){
	Roll.movie(this,"(div.scrollTop+frame)>=(div.scrollHeight-div.offsetHeight)","div.scrollTop=div.scrollHeight/2-div.offsetHeight+((div.scrollTop+frame)-(div.scrollHeight-div.offsetHeight))","div.scrollTop+=frame");
};

Roll.prototype.DOWN=function(){
	Roll.movie(this,"(div.scrollTop-frame)<=0","div.scrollTop=div.scrollHeight/2-(frame-div.scrollTop)","div.scrollTop-=frame");
};

Roll.prototype.LEFT=function(){
	Roll.movie(this,"(div.scrollLeft+frame)>=(div.scrollWidth-div.offsetWidth)","div.scrollLeft=div.scrollWidth/2-div.offsetWidth+((div.scrollLeft+frame)-(div.scrollWidth-div.offsetWidth))","div.scrollLeft+=frame");
};

Roll.prototype.RIGHT=function(){
	Roll.movie(this,"(div.scrollLeft-frame)<=0","div.scrollLeft=div.scrollWidth/2-(frame-div.scrollLeft)","div.scrollLeft-=frame");
};

Roll.prototype.init=function(){//实例化
	this.initState=Roll.checkInit(this);
	if(this.initState<=1)
		return Roll.onError(this.initState);
	Roll.setDiv(this);
};

//==============================静态方法==============================
Roll.onError=function(e){//错误执行
	switch(e){
		case -2:alert("source内容为空!");break;
		case -1:alert("没设置source属性");break;
		case 0:alert("程序未初始化，请先执行init方法");break;
		case 1:return true;//等候重载
		default:alert(e);break;
	}
	return false
};

Roll.checkInit=function(roll){//初始化验证
	var source=roll.source=this.$(roll.source);
	var panel=roll.panel=this.$(roll.panel);
	var sD="";
	if(!source)
		return -1;
	if(source.innerHTML=="")
		return -2;
	sD=source.style.display;
	if(sD!="")
		source.style.display="block";
	roll.sH=source.scrollHeight;
	roll.sW=source.scrollWidth;
	if(sD!="")
		source.style.display=sD;
	if(roll.sH==0 || roll.sW==0){
		setTimeout(function(){roll.init()},roll.waitTime);
		return 1;
	}
	if(!panel)
		roll.panel=panel=source;
	if(!roll.height)
		roll.height=(panel.offsetHeight!=0?panel.offsetHeight:(source.offsetHeight!=0?source.offsetHeight:0));
	if(!roll.width)
		roll.width=(panel.offsetWidth!=0?panel.offsetWidth:(source.offsetWidth!=0?source.offsetWidth:0));
	if(roll.initTime==null){
		roll.initTime=new Date();
		while(Roll.$("Roll_div_"+roll.initTime.getTime()))
			roll.initTime.setTime(roll.initTime.getTime()+1);
	}
	return 2
};

Roll.$=function(t){//获取对象
	if(typeof(t)!="object")
		t=document.getElementById(t);
	return t;
};

Roll.setDiv=function(roll){//创建滚动DIV
	var div="Roll_div_"+roll.initTime.getTime();
	var double=0;
	var html="";
	html+="<table cellpadding=\"0\" cellspace=\"0\" border=\"0\">";
	if(roll.direction.toUpperCase()=="UP" || roll.direction.toUpperCase()=="DOWN"){
		div="<div id=\""+div+"\" style=\"overflow-Y:hidden\"></div>";
		double=Math.ceil(roll.height/roll.sH)*4;
		for(var i=0;i<double;i++)
			html+="<tr><td>"+roll.source.innerHTML+"</td></tr>";
	}
	else{
		div="<div id=\""+div+"\" style=\"overflow-X:hidden\"></div>";
		double=Math.ceil(roll.width/roll.sW)*4;
		html+="<tr>";
		for(var i=0;i<double;i++)
			html+="<td>"+roll.source.innerHTML+"</td>";
		html+="</tr>";
	}
	html+="</table>";
	roll.panel.innerHTML=div;
	div=this.$("Roll_div_"+roll.initTime.getTime());
	div.style.height=roll.height+"px";
	div.style.width=roll.width+"px";
	div.innerHTML=html;
	roll.div=div;
};

Roll.movie=function(roll,a,b,c){
	var div=roll.div;
	var frame=roll.frame;
	eval(a)?eval(b):eval(c);
}
