﻿<!--
/***************************
Marquee Object
***************************/
var Marquee = function(id, direction, opt)
{
    if (!document.getElementById(id)) throw 'invalid id. [' + id + ']';
    // Direction of marquee (0=left, 1=up)?
    if (!direction)
        direction = 0;
    if (direction && direction != 0 && direction != 1) throw 'invalid direction. [' + direction + ']';
    
    this.dir = direction;
    var option = opt || {};
    this.id     = id;
    this.amount = option.amount || 1;
    this.delay  = option.delay  || 40;
    this.position = Number.POSITIVE_INFINITY; // means out of range.
    this._wrap();
    this.start();
}
Marquee.prototype = 
{
    /* wrap child nodes */
    _wrap : function() 
    {
        var t = document.getElementById(this.id);
        with (t.style)
        {
            position = 'relative'; // for ie6.
            overflow = 'hidden';
        }
        var w = document.createElement('div');
        with (w.style)
        {
            margin     = '0';
            padding    = '0';
            background = 'transparent';
            border     = 'none';
        }
        t.normalize();
        while (t.firstChild)
        {
            w.appendChild(t.removeChild(t.firstChild));
        }
        
        t.appendChild(w);
        
        /* get minimum width / height. */
        w.style.position = 'absolute';
        this.minWidth = w.offsetWidth;
        this.minHeight = w.offsetHeight;
        /* put back */
        w.style.position = 'relative';
        w.style.width = '100%'; // for ie6.
        w.style.height = '100%'; // for ie6.
    },
    start : function() 
    {
        this.stop();
        this._next();
    },
    _next : function() 
    {
        var t = document.getElementById(this.id);
        switch (this.dir)
        {
            case 0:
                this.curWidth = Math.min(t.offsetWidth, t.parentNode.offsetWidth); // dirty. (for "overflow:hidden" parent)
                break;
            case 1:
                this.curHeight = Math.min(t.offsetHeight, t.parentNode.offsetHeight); // dirty. (for "overflow:hidden" parent)
                break;    
        }
        
        this.position = this.position - this.amount;
        if (this._isOutOfRange()) 
        {
            this.position = this._startPosition();
        }
        var w = t.firstChild;
        
        switch (this.dir)
        {
            case 0:
                w.style.left = this.position + 'px';
                break;
            case 1:
                w.style.top = this.position + 'px';
                break;    
        }
        
        var self = this;
        this.tid = window.setTimeout
        (
            function() {self._next();},
            this.delay
        );
    },
    _startPosition : function() 
    {
        switch (this.dir)
        {
            case 0:
                return (this.amount > 0) ?  this.curWidth : -this.minWidth;
            case 1:
                return (this.amount > 0) ?  this.curHeight : -this.minHeight;
        }
    },
    _isOutOfRange : function() 
    {
        switch (this.dir)
        {
            case 0:
                return this.position < -this.minWidth || this.curWidth < this.position;
            case 1:
                return this.position < -this.minHeight || this.curHeight < this.position;
        }
    },
    stop : function() 
    {
        if (this.tid) window.clearTimeout(this.tid);
        this.tid = null;
    },
    isMarqueeing : function() 
    {
        return (this.tid) ? true : false;
    }
}
