/*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=50;//滚动速度
Roll.prototype.frame=1;//滚动帧数
Roll.prototype.direction="UP";//滚动方向
Roll.prototype.height=null;//滚动体高度
Roll.prototype.width=null;//滚动体宽度
Roll.prototype.initTime=null;//实例化时间
Roll.prototype.interval=null;//方法容器
Roll.prototype.initState=0;//监测实例加载情况

Roll.prototype.start=function(){//开始滚动
	var base=this;
	if(base.initState<2)
		return Roll.onError(base.initState);
	else{
		this.stop();
		base.interval=setInterval(function(){eval("base."+base.direction.toUpperCase()+"()")},base.speed);
	}
};

Roll.prototype.stop=function(){//停止滚动
	var base=this;
	if(base.initState<2)
		return Roll.onError(base.initState);
	else
		clearInterval(base.interval);
};

Roll.prototype.UP=function(){
	this.div.scrollTop+this.frame>this.div.scrollHeight-this.div.offsetHeight?this.div.scrollTop+=this.frame-this.div.scrollHeight/2:this.div.scrollTop+=this.frame;
};

Roll.prototype.DOWN=function(){
	this.div.scrollTop-this.frame<=0?this.div.scrollTop+=this.div.scrollHeight/2-this.frame:this.div.scrollTop-=this.frame;
};

Roll.prototype.LEFT=function(){
	this.div.scrollLeft+this.frame>this.div.scrollWidth-this.div.offsetWidth?this.div.scrollLeft+=this.frame-this.div.scrollWidth/2:this.div.scrollLeft+=this.frame;
};

Roll.prototype.RIGHT=function(){
	this.div.scrollLeft-this.frame<=0?this.div.scrollLeft+=this.div.scrollWidth/2-this.frame:this.div.scrollLeft-=this.frame
};

Roll.prototype.init=function(){//实例化
	Roll.checkInit(this);
	if(this.initState<1)
		return Roll.onError(this.initState);
	Roll.setDiv(this);
};

//==============================静态方法==============================
Roll.checkInit=function(roll){//初始化验证
	var source=roll.source=this.$(roll.source);
	var panel=roll.panel=this.$(roll.panel);
	if(!source)
		return roll.initState=-1;
	else if(source.innerHTML=="")
		return roll.initState=-2;
	roll.panel=panel=panel?panel:source;
	roll.height=roll.height?roll.height:(panel.style.height!=""?parseInt(panel.style.height):(source.style.height!=""?parseInt(source.style.height):null));
	roll.width=roll.width?roll.width:(panel.style.width!=""?parseInt(panel.style.width):(source.style.width!=""?parseInt(source.style.width):null));
	if(!roll.width && !roll.height)
		return roll.initState=-3;
	if(roll.initTime==null){
		roll.initTime=new Date();
		while(Roll.$(panel.id+"_"+roll.initTime.getTime()))
			roll.initTime.setTime(roll.initTime.getTime()+1);
	}
	roll.initState=1;
};

Roll.setDiv=function(roll){//创建滚动DIV
	if(roll.panel.id==roll.source.id){
		var source=roll.source;
		var sD=source.style.display; 
		if(sD!="")
			source.style.display="block";
		if(source.scrollHeight<=0 && source.scrollWidth<=0)
			return this.setWaitOnLoad(function(){roll.init()});
		if(sD!="")
			source.style.display=sD;
	}
	var div=this.$(roll.panel).id+"_"+roll.initTime.getTime();
	var html=roll.source.innerHTML;
	roll.panel.innerHTML="<div id=\""+div+"\"></div>";
	roll.div=div=this.$(div);
	div.style.height=roll.height+"px";
	div.style.width=roll.width+"px";
	var table=this.CE("table");
	table.cellpadding=table.cellspace=table.border=0;
	var tbody=this.CE("tbody");
	var tr=this.CE("tr");
	var td=this.CE("td");
	td.innerHTML=html;
	this.AC(div,this.AC(table,this.AC(tbody,this.AC(tr,td))));
	if(div.scrollHeight<=0 && div.scrollWidth<=0)
		return this.setWaitOnLoad(function(){roll.init()});
	if(roll.direction.toUpperCase()=="UP" || roll.direction.toUpperCase()=="DOWN"){
		this.AC(tbody,tr.cloneNode(true));
		div.style.overflowY="hidden";
		while(div.scrollHeight<roll.height*2 && div.scrollHeight>0){
			for(var i=0;i<2;i++)
				this.AC(tbody,tr.cloneNode(true));
		}
	}
	else{
		this.AC(tr,td.cloneNode(true));
		div.style.overflowX="hidden";
		while(div.scrollWidth<roll.width*2 && div.scrollWidth>0)
			for(var i=0;i<2;i++)
				this.AC(tr,td.cloneNode(true));
	}
	roll.initState=2;
	roll.start();
};

Roll.movie=function(roll,a,b,c){
	var div=roll.div;
	var frame=roll.frame;
	eval(a)?eval(b):eval(c);
}

Roll.$=function(t){//获取对象
	if(typeof(t)!="object")
		t=document.getElementById(t);
	return t;
};

Roll.CE=function(tag){//document.createElement
	return document.createElement(tag);
};

Roll.AC=function(a,b){//appendChild
	a.appendChild(b);
	return a;
};

Roll.setWaitOnLoad=function(fun){//添加延迟执行
	if(window.attachEvent)//IE
		window.attachEvent("onload",fun);
	if(window.addwaitListener)//FF
		window.addwaitListener("load",fun,false);
};

Roll.onError=function(e){//错误执行
	switch(e){
		case -3:alert("无法检测任何高或宽的设置");break;
		case -2:alert("source内容为空!");break;
		case -1:alert("没设置source属性");break;
		case 0:alert("程序未初始化，请先执行init方法");break;
		case 1:window.status="程序等候载入中";return true;
		default:alert(e);break;
	}
	return false
};