// 
// @name marquee.js
// @author miya2000
// @namespace http://d.hatena.ne.jp/miya2000/
// @version 1.0.0
// 
// @version 2.0.0 by Matteo Chessa www.matteochessa.it (Added Directions)
// Directions: 1=left; 2=right; 3=up; 4=down;
//


var Marquee = function ( id, opt ){
    if( ! document.getElementById(id) ) throw 'invalid id. [' + id + ']';
    var option = opt || {};
    this.id     = id;
	this.direction = (option.direction && option.direction>=1 && option.direction<=4)?option.direction:1;
    this.amount = Math.abs(option.amount || 6);
	if(!(this.direction%2)) this.amount = -this.amount;
    this.delay  = option.delay  || 100;
    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. */
		if(this.direction==1 || this.direction==2) {
			w.noWrap = "nowrap";
			w.style.whiteSpace = "nowrap";
		}
        w.style.position = 'absolute';
        this.minWidth = w.offsetWidth;
        this.minHeight = w.offsetHeight;
        /* put back */
        w.style.position = 'relative';
        w.style.width = '100%'; // for ie6.
		t.style.zIndex = 10;
    },
    start : function() {
        this.stop();
        this._next();
    },
    _next : function() {
        var t = document.getElementById( this.id );
        this.curWidth = Math.min(t.offsetWidth,t.parentNode.offsetWidth); // dirty. (for "overflow:hidden" parent)
        this.curHeight = Math.min(t.offsetHeight,t.parentNode.offsetHeight); // dirty. (for "overflow:hidden" parent)
        this.position = this.position - this.amount;
        if ( this._isOutOfRange() ) {
            this.position = this._startPosition();
        }
        var w = t.firstChild;
		if(this.direction==3 || this.direction==4)
	        w.style.top = this.position + 'px';
		else
	        w.style.left = this.position + 'px';
        var self = this;
        this.tid = window.setTimeout(
            function(){ self._next(); },
            this.delay
        );
    },
    _startPosition : function() {
		     if(this.direction==4) return -this.minHeight;
		else if(this.direction==3) return this.curHeight;
		else if(this.direction==2) return -this.minWidth;
		else                       return this.curWidth;
        //return (this.amount>0)?this.curWidth:-this.minWidth;
    },
    _isOutOfRange : function() {
        //return this.position < -this.minWidth || this.curWidth < this.position || this.position < -this.minHeight|| this.curHeight < this.position;
		if(this.direction==3 || this.direction==4)
        	return this.position < -this.minHeight || this.curHeight < this.position;
		else
        	return this.position < -this.minWidth || this.curWidth < this.position;
    },
    stop : function() {
        if ( this.tid ) window.clearTimeout( this.tid );
        this.tid = null;
    },
    isMarqueeing : function() {
        return ( this.tid ) ? true : false;
    }
}
