/*
==================版权信息==================
欢迎转贴、研究、使用（商业及个人目的）、修改，本文无任何版权要求
唯一的的希望是如果你对这个跑马灯有任何改进意见，欢迎写信到xmqeasy@gmail.com进行探讨交流，共同进步。
v2.0版本记录（2008-2-29）
因为在HTC里使用非常的不方便，需要在页面注册XML名称空间，导入实现的HTC文件等，
实际使用起来的确很麻烦，因此便将所有的代码采集到此js文件里来，简化为一个函数Marquee()
虽然代码骨干部分其实都是一样(-_-!!
当然，这所谓的一样也经历了替换变量名，整理代码缩进等工作，偶又不擅长写js，自然是不知道有
什么比较好用的js编辑/调试器，所以最苦的还是在改进代码对MOZILLA浏览器（靠，其实就是FF啦）
的支持上，重点提名批评FF对style.height/width属性和insertRow/insertCell方法上的挑剔用法，林北会搞
到这么晚80巴仙的原因就是在调试这两个鸟问题（啊，谁人能告诉我下为什么在FF下如果脚本有
错误它连提示都不提示直接性很干脆的罢工呢？）很多人说FF好用云云，我只想说，我顶你个肺啊！！

算了，不理这傻狐了，但愿有一天基地组织改炸谷割大楼，FF的开发人员一起死光光，万岁~~！
在此版本实现跑马灯范例如下：
<div width="580px" height="210px" style="height:210px;width:580px" onreadystatechange="if(this.readyState=='complete') Marquee(this,'up',30);">
跑~~马灯内容...
</div>

注意上面偶同时指定了width和style.width，原因就是IE认style.width而FF只认width，
众口难调，真不知道如果还有其他系列的浏览器这两个都不支持，偶还要写出多奇怪的代码来？
——By Micah.Dion
*/

function Marquee(obj,dir,spd){
	if(obj==null) return null;

	if(spd<30) spd=30;

	this.createMarquee = function() {
	    if (obj == null) return;

	    //记录跑马灯板原始的宽度和高度，便于后来设置放置的表格宽度和高度//
	    var orgWdt = obj.scrollWidth;
	    var orgHgt = obj.scrollHeight;


	    //设置跑马灯板的样式，以及设置长宽，当然如果没设置长宽导致不会滚动，那就是你自己家的事情了//
	    obj.style.overflow = "hidden";
	    //***命苦，FF这货不支持style.height属性的读取，因此在FF里必须先读取width/height属性来设置长宽，IE里得从style来（它偏不认width/height属性定义，屌），真晕***
	    if (obj.getAttribute) {
	        if (obj.getAttribute("width"))
	            obj.style.width = obj.getAttribute("width");
	        else
	            obj.style.width = obj.offsetWidth;

	        if (obj.getAttribute("height"))
	            obj.style.height = obj.getAttribute("height");
	        else
	            obj.style.height = obj.offsetHeight;
	    } else {
	        if (!obj.style.height) obj.style.height = obj.offsetHeight;
	        if (!obj.style.width) obj.style.width = obj.offsetWidth;
	    }

	    //如果内容还不足够进行滚动，那就直接退出得了//
	    //除盲小帮手:offsetHeight指的是实际在容器中显示的高度，而scrollHeight则是加上被容器遮蔽部分的高度//
	    if ((dir == "up" || dir == "down") && obj.offsetHeight >= obj.scrollHeight) return;
	    if ((dir == "left" || dir == "right") && obj.offsetWidth >= obj.scrollWidth) return;

	    //读取并清空内容//
	    var html = obj.innerHTML;
	    obj.innerHTML = "";

	    if (dir == "left" || dir == "right") {//横向，为了并排就只能用表格//
	        var tbl = document.createElement("TABLE");
	        tbl.style.width = orgWdt * 2 + "px";
	        tbl.style.height = orgHgt + "px";

	        var row = tbl.insertRow(-1); //啊啊啊啊~~FF我XX你个OO，你怎么那么多挑剔啊，insertRow和insertCell都需要参数！！！
	        var o1 = row.insertCell(-1);
	        o1.style.width = orgWdt + "px";
	        o1.style.height = orgHgt + "px";
	        var o2 = row.insertCell(-1);
	        o2.style.width = orgWdt + "px";
	        o2.style.height = orgHgt + "px";
	        o1.innerHTML = o2.innerHTML = html;

	        obj.appendChild(tbl);

	    } else {//纵向,2008-2-28修正：为了统一，还是改用一列两行的表格吧-_-!!//
	        var tbl = document.createElement("TABLE");
	        tbl.style.width = orgWdt + "px";
	        tbl.style.height = orgHgt * 2 + "px";

	        var row = tbl.insertRow(-1);
	        var o1 = row.insertCell(-1);
	        o1.style.width = orgWdt + "px";
	        o1.style.height = orgHgt + "px";
	        var row2 = tbl.insertRow(-1);
	        var o2 = row2.insertCell(-1);
	        o2.style.width = orgWdt + "px";
	        o2.style.height = orgHgt + "px";
	        o1.innerHTML = o2.innerHTML = html;

	        obj.appendChild(tbl);
	    }

	    //设定初始移动位置//
	    if (dir == "down")
	        obj.scrollTop = obj.scrollHeight;
	    else if (dir == "up")
	        obj.scrollTop = 0;
	    else if (dir == "right")
	        obj.scrollLeft = obj.scrollWidth;
	    else if (dir == "left")
	        obj.scrollLeft = 0;
	        
	    //设立定时，以及鼠标事件//
	    this.pid = setInterval(this.moveMarquee, spd);
	}

	this.stopMarquee=function(){
		clearInterval(this.pid);
	}

	this.startMarquee=function(){
		this.pid=setInterval(this.moveMarquee,spd);
	}

	this.moveMarquee = function() {
	    if (dir == "down") {
	        obj.scrollTop -= 2;
	        if (obj.scrollTop <= o1.offsetHeight - obj.offsetHeight + 5) obj.scrollTop = obj.scrollHeight;
	    }
	    else if (dir == "up") {
	        obj.scrollTop += 2;
	        if (obj.scrollTop >= o1.offsetHeight - 3) obj.scrollTop = 0;
	    }
	    else if (dir == "right") {
	        obj.scrollLeft -= 2;
	        if (obj.scrollLeft <= o1.offsetWidth - obj.offsetWidth + 5) obj.scrollLeft = obj.scrollWidth;
	    }
	    else if (dir == "left") {
	        obj.scrollLeft += 2;
	        if (obj.scrollLeft >= o1.offsetWidth - 3) obj.scrollLeft = 0;
	    }
	}

	this.createMarquee();

}
