﻿function Marquee2(target, width, height, direction, speed, distance, delay){
	var _T=this, scrollId=0, scrollIt=null, clear=function(isAutoStart){clearInterval(scrollId);if(isAutoStart)scrollId=setTimeout(_T.start, delay);};
	(function(){
		var table = document.createElement("table"), tr, td, html = target.innerHTML;
		table.border = table.cellPadding = table.cellSpacing = 0;
		var step = (distance?distance:1), t, r, b, l;
		var 
		scrollT=function(){target.scrollTop+=step;if(t==target.scrollTop)target.scrollTop=0;t=target.scrollTop;if(t%height==0){clear(true);}if(t>=target.scrollHeight/2)target.scrollTop=0;},
		scrollR=function(){if(target.scrollLeft<=0)target.scrollLeft=target.scrollWidth/2;target.scrollLeft-=step;if(target.scrollLeft%width==0){clear(true);}},
		scrollB=function(){if(target.scrollTop<=0)target.scrollTop=target.scrollHeight/2;target.scrollTop-=step;if(target.scrollTop%height==0){clear(true);}},
		scrollL=function(){target.scrollLeft+=step;if(l==target.scrollLeft)target.scrollLeft=0;l=target.scrollLeft;if(l%width==0){clear(true);}if(l>=target.scrollWidth/2)target.scrollLeft=0;};
		var i = 0, j = 2;
		switch(direction){
			case "T":
			case "B":
				html = '<div style="height:auto!important;height:'+height+'px;min-height:'+height+'px">'+html+'</div>';
				for(;i<j;i++){tr=table.insertRow(-1);td=tr.insertCell(-1);td.innerHTML=html;}
				break;
			case "L":
			case "R":
				html = '<div style="width:auto!important;width:'+width+'px;min-width:'+width+'px">'+html+'</div>';
				tr=table.insertRow(-1);for(;i<j;i++){td=tr.insertCell(-1);td.innerHTML=html;}
				break;
			default:
				throw "Not specify a valid direction";
				break;
		}
		scrollIt = eval("scroll"+direction);
		target.innerHTML = "";
		target.appendChild(table);
	})();
	target.onmouseover = this.stop = function(){clear(false)};
	target.onmouseout = this.start = function(){clearTimeout(scrollId);scrollId=setInterval(scrollIt,speed)};
	clear(true);
}

