﻿// Useage: HCmarquee(滚动对象id, 可见范围宽度, 可见范围高度,行数， 滚动速度, 停留时间, 方向); 方向有left和up两种
function marquee(id, mwidth, mheight, mrows, mspeed, pause, direction) 
{
    var obj = document.getElementById(id);
    obj.stoptag = false; //stop tag
    obj.mrows = mrows; //marquee rows
    obj.mwidth = mwidth; //marquee width
    obj.mheight = mheight; //marquee height
    obj.mspeed = mspeed; //marquee speed
    obj.pause = pause; //pause time
    obj.preTop = 0; //pre top
    obj.stoptime = 0; //stop time
    obj.direction = direction; //direction

    with (obj) {
        style.width = mwidth + "px";
        style.height = mheight + "px";
        noWrap = false;
        onmouseover = stopm;
        onmouseout = startm;
        scrollTop = 0 + "px";
        scrollLeft = 0 + "px";
    }

    if (obj.mrows != 1) {
        switch (obj.direction) {
            case ("up"):
                obj.totalheight = mheight * mrows;
                obj.currentIop = mheight;
                setInterval(scrollUp, obj.mspeed); break;
            default: //("left"):
                obj.totalheight = mwidth * mrows;
                obj.currentIop = mwidth;
                obj.innerHTML = '<div style="width:' + (obj.totalheight * 2) + 'px;"><div style="float:left;">' + obj.innerHTML + '</div><div style="float:right;">' + obj.innerHTML + '</div></div>';
                document.write('<style type="text/css">#' + id + ' table{width:' + mwidth * mrows + 'px;} #' + id + ' td{width:' + mwidth + 'px;}</style>');
                setInterval(scrollLeft, obj.mspeed); break;
        }
    }
    function scrollUp() {
        if (obj.stoptag == true) return;

        obj.currentIop += 1;
        if (obj.currentIop == obj.mheight + 1) {
            obj.stoptime += 1; obj.currentIop -= 1;

            if (obj.stoptime == obj.pause)
            {
                obj.currentIop = 0; obj.stoptime = 0;
            }
        }

        else {            
            obj.scrollTop++;
            if (obj.preTop != obj.scrollTop)
             {
                 obj.preTop = obj.scrollTop;
            }
            else {
                obj.scrollTop = 0 ;
                obj.preTop = 0;
                obj.currentIop = obj.mheight ;
            }
        }
    }


    function scrollLeft() {
        if (obj.stoptag == true) return;
        obj.currentIop += 1;
        if (obj.currentIop == obj.mwidth + 1) {
            obj.stoptime += 1; obj.currentIop -= 1;
            if (obj.stoptime == obj.pause) { obj.currentIop = 0; obj.stoptime = 0; }
        } else {
            obj.preTop = (++obj.scrollLeft);
            if (obj.preTop == obj.totalheight) { obj.scrollLeft = 0; }
        }
    }
    function stopm() { obj.stoptag = true; }
    function startm() { obj.stoptag = false; }
}

