// CASA

// marquee box
marquee = function(name) {
    // default
    this.speed = 40;        // scroll speed
    this.delay = 2000;      // delay timeout
    this.rows = 1;          // rows of keep
    this.cols = 1;          // cols of keep
    this.height = 15;       // height of block
    this.width = 100;       // width of block
    this.direct = 'up';     // up,down,left,right

    this.current = 0;
    this.name = name;       // marquee box name,ascii
    this.container = null;
    this.items = new Array();
    this.delayInterval = null;
    this.scrollInterval = null;
}

// drawing
marquee.prototype.init = function(items){
    this.items = items;

    // container
    var height = parseInt(this.height) + 'px';
    var totalHeight = this.rows * parseInt(this.height) + 'px';
    var width = parseInt(this.width) + 'px';
    var totalWidth = this.cols * parseInt(this.width) + 'px';

    document.write('<div id="' + this.name + '_container" style="overflow:hidden;height:' + totalHeight + ';width:' + totalWidth + ';"></div>');
    this.container = document.getElementById(this.name + '_container');

    // first item
    var item = document.createElement('DIV');
    item.style.overflow = 'hidden';
    item.style.height = height;
    item.style.width = width;
    item.innerHTML = this.items[0];
    this.container.appendChild(item);

    // set container handle
    var me = this;
    this.container.onmouseover = function(){
        clearInterval(me.delayInterval);
    };
    this.container.onmouseout = function(){
        me.delayInterval = setInterval(me.name + ".next()",me.delay + me.speed * parseInt(me.height));
    };

    // scroll to next
    this.delayInterval = setInterval(this.name + ".next()",this.delay + this.speed * parseInt(this.height));
};
// show next
marquee.prototype.next = function(){
    this.current++;
    if(this.current > this.items.length - 1) this.current = 0;

    if (this.container.childNodes.length <= this.rows) {
        // fill
        var item = document.createElement('DIV');
        item.style.overflow = 'hidden';
        item.style.height = parseInt(this.height) + 'px';
        item.style.width = parseInt(this.width) + 'px';
        item.innerHTML = this.items[this.current];
        this.container.appendChild(item);
    } else {
        // re-scrolling container
        this.container.childNodes[0].innerHTML = this.items[this.current];
        this.container.appendChild(this.container.childNodes[0]);
        this.container.scrollTop = 0;
    }
    clearInterval(this.scrollInterval);
    this.scrollInterval = setInterval(this.name + ".scroll()",this.speed);
};
// scroll item
marquee.prototype.scroll = function(){
    this.container.scrollTop++;
    // delay
    if(this.container.scrollTop % this.height == 0){
        clearInterval(this.scrollInterval);
    }
};


