﻿var Jack;
if(!Jack) Jack={};
Jack.marquee=function(id,speed,wait,Direction,autoScroll){
    var SELF=this;
	SELF.StepNum=0;
	if(speed==null){
	   SELF.speed=10
	}
	else{
	   SELF.speed=speed;
	}
	if(wait==null){
	   SELF.wait=3000
	}
	else{
	   SELF.wait=wait;
	}
	if(Direction==null){
	   Direction="L"
	}
	Direction=Direction.toUpperCase()
	if(Direction!='L' && Direction!='R' && Direction!='U' && Direction!='D'){
	   SELF.Direction="L"
	}
	else{
	   SELF.Direction=Direction
	}
	if(autoScroll==null){
	   SELF.autoScroll=true;
	}
	else{
	   SELF.autoScroll=autoScroll;
	}
	SELF.MyMar=null;
	SELF.demo=document.getElementById(id)
	SELF.innerHTML=SELF.demo.innerHTML
	if(SELF.Direction=="L" || SELF.Direction=="R"){
	   SELF.demo.innerHTML="<table cellspacing=0 cellpadding=0 border=0 ><tr><td id='" + id + "_demo1'>"+ SELF.innerHTML +"</td><td id='" + id + "_demo2'></td></tr></table>"
	}
	else{
	   SELF.demo.innerHTML="<table cellspacing=0 cellpadding=0 border=0 ><tr><td id='" + id + "_demo1'>"+ SELF.innerHTML +"</td></tr><tr><td id='" + id + "_demo2'></td></tr></table>"
	}
	SELF.demo1=document.getElementById(id+"_demo1")
	SELF.demo2=document.getElementById(id+"_demo2")
	SELF.toLeft=function(){
	    if(SELF.Direction=="L" || SELF.Direction=="R"){
	       SELF.Direction="L";
		   SELF.reStart()
		}
	}
	
	SELF.toRight=function(){
	    if(SELF.Direction=="L" || SELF.Direction=="R"){
	       SELF.Direction="R";
		   SELF.reStart()
		}
	}
	
	SELF.toUp=function(){
	    if(SELF.Direction=="U" || SELF.Direction=="D"){
	       SELF.Direction="U";
		   SELF.reStart()
		}
	}
	
	SELF.toDown=function(){
	    if(SELF.Direction=="U" || SELF.Direction=="D"){
	       SELF.Direction="D";
		   SELF.reStart()
		}
	}
	
	SELF.start=function(){
	   if(document.all){
		 if(document.onreadystatechange==null){
	         document.onreadystatechange = SELF.reStart;
	     }
	     else{
			var oldEnt=document.onreadystatechange;
			document.onreadystatechange=function(){
			   oldEnt();
		       SELF.reStart();
	        }
	     }
	   }
	   else{
	      SELF.reStart()
	   }
	}
	
	SELF.reStart=function(){
	   clearTimeout(SELF.MyMar);
	   if(SELF.Direction=="L" || SELF.Direction=="R"){
	      if(SELF.demo1.offsetWidth>SELF.demo.offsetWidth){
	         SELF.demo2.innerHTML=SELF.demo1.innerHTML;	//克隆demo1为demo2
		     if(SELF.Direction=="L"){
		        SELF.LeftMarquee()
		     }
		     else{
		        SELF.RightMarquee()
		     }
	      }
	   }
	   else{
	      if(SELF.demo1.offsetHeight>SELF.demo.offsetHeight){
	         SELF.demo2.innerHTML=SELF.demo1.innerHTML;	//克隆demo1为demo2
		     if(SELF.Direction=="U"){
		        SELF.UpMarquee()
		     }
		     else{
		        SELF.DownMarquee()
		     }
	      }
	   }
	}

	SELF.LeftMarquee=function(){
	   if(SELF.demo2.offsetWidth  <= SELF.demo.scrollLeft){	//当滚动至demo1与demo2交界时
	      SELF.demo.scrollLeft = (SELF.demo.scrollLeft - SELF.demo1.offsetWidth);		//demo跳到最顶端
	   }
	   SELF.demo.scrollLeft++;
	   SELF.StepNum++;
	   if(SELF.StepNum%SELF.demo.offsetWidth==0){
	      SELF.StepNum=0;
		  if(SELF.autoScroll){
		     SELF.MyMar=setTimeout(SELF.LeftMarquee,SELF.wait);
		  }
		  else{
		     clearTimeout(SELF.MyMar);
		  }
	   }
	   else{
		  SELF.MyMar=setTimeout(SELF.LeftMarquee,SELF.speed);
	   }
	}
	
	SELF.RightMarquee=function(){
	   if( SELF.demo.scrollLeft<=0){	//当滚动至demo1与demo2交界时
	     SELF.demo.scrollLeft = SELF.demo1.offsetWidth	
	   }
	   SELF.demo.scrollLeft--;
	   SELF.StepNum--;
	   if(SELF.StepNum%SELF.demo.offsetWidth==0){
	      SELF.StepNum=0;
		  if(SELF.autoScroll){
		     SELF.MyMar=setTimeout(SELF.RightMarquee,SELF.wait);
		  }
		  else{
		     clearTimeout(SELF.MyMar);
		  }
	   }
	   else{
		  SELF.MyMar=setTimeout(SELF.RightMarquee,SELF.speed);
	   }
	}
	
	SELF.UpMarquee=function(){
	   if(SELF.demo2.offsetHeight<= SELF.demo.scrollTop){	//当滚动至demo1与demo2交界时
	      SELF.demo.scrollTop = (SELF.demo.scrollTop - SELF.demo1.offsetHeight);		//demo跳到最顶端
	   }
	   SELF.demo.scrollTop++;
	   SELF.StepNum++;
	   if(SELF.StepNum%SELF.demo.offsetHeight==0){
	      SELF.StepNum=0;
		  if(SELF.autoScroll){
		     SELF.MyMar=setTimeout(SELF.UpMarquee,SELF.wait);
		  }
		  else{
		     clearTimeout(SELF.MyMar);
		  }
	   }
	   else{
		  SELF.MyMar=setTimeout(SELF.UpMarquee,SELF.speed);
	   }
	}
	
	SELF.DownMarquee=function(){
	   if(SELF.demo.scrollTop<=0){	//当滚动至demo1与demo2交界时
	      SELF.demo.scrollTop = SELF.demo1.offsetHeight		//demo跳到最顶端
	   }
	   SELF.demo.scrollTop--;
	   SELF.StepNum--;
	   if(SELF.StepNum%SELF.demo.offsetHeight==0){
	      SELF.StepNum=0;
		  if(SELF.autoScroll){
		     SELF.MyMar=setTimeout(SELF.DownMarquee,SELF.wait);
		  }
		  else{
		     clearTimeout(SELF.MyMar);
		  }
	   }
	   else{
		  SELF.MyMar=setTimeout(SELF.DownMarquee,SELF.speed);
	   }
	}

	SELF.demo.onmouseover=function() {  //鼠标移上时清除定时器达到滚动停止的目的
	    if(!SELF.autoScroll){return;}
		clearTimeout(SELF.MyMar);
    }
	SELF.demo.onmouseout=function() {
	   if(!SELF.autoScroll){return;}
	   clearTimeout(SELF.MyMar);
	   switch(SELF.Direction){
		       case "L":
			       SELF.MyMar=setTimeout(SELF.LeftMarquee,SELF.wait);
				   break;
			   case "R":
			       SELF.MyMar=setTimeout(SELF.RightMarquee,SELF.wait);
				   break;
			   case "U":
			       SELF.MyMar=setTimeout(SELF.UpMarquee,SELF.wait);
				   break;
			   case "D":
			       SELF.MyMar=setTimeout(SELF.DownMarquee,SELF.wait);
				   break;
			   default:
	   }
	}//鼠标移开时重设定时器
	if(SELF.autoScroll){
       SELF.start();
	}
}	
