var EffectTypewriter = Class.create();

EffectTypewriter.prototype = {

    initialize: function(speed)
    {
        this.list  = [];
        if (speed < 0) {
            speed *= -1;
        }
        this.speed = speed
    },


    register: function(records)
    {
        for (var i = 0; i < records.length; i++) {
            this.list.push({
                               "id": records[i].id,
                               "string": records[i].string,
                               "no_effect": records[i].no_effect
                           });
        }
    },

    start: function()
    {
        this.index = 0;
        this._effect();
    },

    _effect: function()
    {
        if (this.list.length > 0 && this.index < this.list.length) {
            this.screen = $(this.list[this.index].id);
            this.output = this.list[this.index].string;
            this.pos    = 1;

            this.screen.innerHTML = "";
            this._display();
        }
    },

    _display: function()
    {
        if (this.list[this.index].no_effect == true) {
            this.screen.innerHTML = this.output;
            this.index++;
            this._effect();
        } else if (this.pos <= this.output.length) {
            this.screen.innerHTML = this.output.substr(0, this.pos++);
            setTimeout(this._display.bind(this), this.speed);
        } else {
            this.index++;
            this._effect();
        }
    }

};
