﻿// JScript 文件
/*
    使用例子：
    <div id="marquee" direction="up" style="overflow:hidden;height:200px;width:250px">
      <table style="border:0px;padding:0px;"><tr><td>
        <!-- Marquee Body Head -->
        <a href="#" target="_blank">经典论坛</a><br>
        <a href="#" target="_blank">163.com</a><br>
        <a href="#" target="_blank">21cn.com</a><br>
        <a href="#" target="_blank">Happy new year</a>
        <!-- Marquee Body Bottom -->
      </td></tr></table>
    </div>
    
    <script src="move.js"></script>
    <script language="JavaScript" defer>
        var repeat = null;
        var marquee_spd = 50;
        var obj_marquee = document.getElementById("marquee");
        //默认移动方向
        obj_marquee.direction='left'
        Move();

        function Move(){
	        marquee_init(obj_marquee);
	        repeat = setInterval("marquee_doit(obj_marquee)",marquee_spd);

	        obj_marquee.onmouseover = function() {clearInterval(repeat);}
	        obj_marquee.onmouseout = function() {repeat=setInterval("marquee_doit(obj_marquee)",marquee_spd);}
        }
   </script>
*/
function marquee_init(obj) {
	var obj_unit = obj.firstChild;
	var marquee_high = parseInt(obj.style.height);
	var marquee_wide = parseInt(obj.style.width);
	var unit_high = obj_unit.offsetHeight;
	var unit_wide = obj_unit.offsetWidth;
	var m = 0, n = 0, i = 0;
	var tmp = null;
	
	m = Math.ceil(marquee_wide / unit_wide);
	n = Math.ceil(marquee_high / unit_high);

	obj_unit.style.width = marquee_wide * (m+1);
	obj_unit.rows[0].cells[0].style.width = obj.style.width;
	
	for(i=0; i<m; i++) {
		tmp = obj_unit.rows[0].insertCell(-1);
		tmp.innerHTML = obj_unit.rows[0].cells[0].innerHTML;
		tmp.style.width = marquee_wide;
		tmp = null;
	}

	for(i=0; i<n; i++) {
		obj.appendChild(obj_unit.cloneNode(true));
	}

	return;
}

function marquee_show(direction,obj) {
	switch(direction) {
		case "up":
			if(obj.scrollTop >= obj.children[1].offsetTop) {
				obj.scrollTop -= obj.firstChild.offsetHeight;
			} else {
				obj.scrollTop++;
			}
			break;
		case "down":
			if(obj.scrollTop <= 0) {
				obj.scrollTop += obj.firstChild.offsetHeight;
			} else {
				obj.scrollTop--;
			}
			break;
		case "left":
			if(obj.scrollLeft >= obj.firstChild.rows[0].cells[0].offsetWidth) {
				obj.scrollLeft -= obj.firstChild.rows[0].cells[0].offsetWidth;
			} else {
				obj.scrollLeft++;
			}
			break;
		case "right":
			if(obj.scrollLeft <= 0) {
				obj.scrollLeft += obj.firstChild.rows[0].cells[0].offsetWidth;
			} else {
				obj.scrollLeft--;
			}
			break;
		default:
			break;
	}
	return;
}

function marquee_doit(obj) {
	var direction = "";
	direction = obj.getAttribute("direction");
	if(direction != null) marquee_show(direction,obj);
	direction = obj.getAttribute("direction2");
	if(direction != null) marquee_show(direction,obj);
	return;
}
