﻿
function marquee(id, direction, speed, delay)
{
    var tag = typeof (id) == 'string' ? document.getElementById(id) : id;
    if (!tag) return;
    if (tag.__marquee)
    {
        var marquee = tag.__marquee;
        if (!direction) return;
        direction = direction.toString().toLowerCase();
        switch (direction)
        {
            case 'pause': marquee.pauseCommand = true; break;
            case 'play': marquee.pauseCommand = false; break;
            case 'speed': marquee.speed = speed; break;
            case 'delay': marquee.delay = speed; break;
            case 'left':
            case 'right':
            case 'up':
            case 'down':
                if (marquee.direction == direction) return;
                if ((direction == 'left' || direction == 'right') && marquee.vertical) return;
                var ratio = Math.ceil(tag.parentNode[marquee.vertical ? 'clientHeight' : 'clientWidth'] / marquee.size);
                if (ratio < 1) ratio = 1;
                var maxSize = ratio * marquee.size;
                switch (direction)
                {
                    case 'left': tag.parentNode.scrollLeft = tag.parentNode.scrollLeft - maxSize; break;
                    case 'right': tag.parentNode.scrollLeft = maxSize + (tag.parentNode.scrollLeft - maxSize); break;
                    case 'up': tag.parentNode.scrollTop = tag.parentNode.scrollTop - maxSize; break;
                    case 'down': tag.parentNode.scrollTop = maxSize + (tag.parentNode.scrollTop - maxSize); break;
                }
                marquee.direction = direction;
            default: ;
        }
        return;
    }

    delay = parseInt(delay);
    speed = parseInt(speed);

    if (!delay || delay < 0) delay = 60;
    if (!speed || speed < 0) speed = 3;
    direction = (direction || 'left').toLowerCase();
    var marquee = tag.__marquee = {};
    marquee.direction = direction;
    marquee.speed = speed;
    marquee.delay = delay;
    marquee.vertical = marquee.direction == 'up' || marquee.direction == 'down';
    marquee.pause = false;

    marquee.onMouseOver = function() { marquee.pause = true; };
    marquee.onMouseOut = function() { marquee.pause = false; };

    if (tag.attachEvent)
    {
        tag.attachEvent('onmouseover', marquee.onMouseOver, false);
        tag.attachEvent('onmouseout', marquee.onMouseOut, false);
    }
    else
    {
        tag.addEventListener('mouseover', marquee.onMouseOver, false);
        tag.addEventListener('mouseout', marquee.onMouseOut, false);
    }

    var parent = tag.parentNode;
    if (marquee.vertical)
    {
        var reverseDirection = 'down';
        var clientProp = 'clientHeight';
        var scrollProp = 'scrollTop';
        marquee.size = tag.offsetHeight;

        if (!tag.rows) return;

        var rows = tag.rows.length;
        var times = Math.ceil(parent.clientHeight / marquee.size) * 3 - 1;

        for (var i = 0; i < times; i++)
        {
            for (var j = 0; j < rows; j++) tag.tBodies[0].appendChild(tag.rows[j].cloneNode(true));
        }
    }
    else
    {
        var reverseDirection = 'right';
        var clientProp = 'clientWidth';
        var scrollProp = 'scrollLeft';
        marquee.size = tag.offsetWidth;

        if (!tag.rows || !tag.rows[0]) return;

        var row = tag.rows[0];
        var columns = row.cells.length;
        var times = Math.ceil(screen.availWidth / marquee.size) * 3 - 1;

        for (var i = 0; i < times; i++)
        {
            for (var j = 0; j < columns; j++) row.appendChild(row.cells[j].cloneNode(true));
        }
    }

    parent[scrollProp] = marquee.direction == reverseDirection ? parent[clientProp] * 2 : 0;
    marquee.handle = function()
    {
        window.clearTimeout(marquee.timer);
        if (!marquee.pause && !marquee.pauseCommand)
        {
            var ratio = Math.ceil(parent[clientProp] / marquee.size);
            if (ratio < 1) ratio = 1;
            var maxSize = ratio * marquee.size;

            var offset = parent[scrollProp] - maxSize;
            if (marquee.direction == reverseDirection)
            {
                parent[scrollProp] -= marquee.speed;
                if (parent[scrollProp] <= maxSize)
                {
                    parent[scrollProp] = maxSize * 2 + offset //- marquee.speed;
                }
            }
            else
            {
                parent[scrollProp] += marquee.speed;
                if (parent[scrollProp] >= maxSize)
                {
                    parent[scrollProp] = offset + marquee.speed;
                }
            }
        }
        marquee.timer = window.setTimeout(arguments.callee, marquee.delay);
    };

    marquee.timer = window.setTimeout(marquee.handle, marquee.delay);
}
