var otm = otm ? otm : {};
otm.widget = otm.widget ? otm.widget : {};
otm.widget.text = otm.widget.text ? otm.widget.text : {};

otm.widget.text.AlternatingMessageText = function(divId) {
    this._divId = divId;
    this._textMessageList = new Array();
    this._textDisplayDurationList = new Array();
    this._currentDisplayIndex = -1;
    this._isPlaying = false;

    this.addTextMessage = function(textMessage, duration) {
        var i = this._textMessageList.length;
        this._textMessageList[i] = textMessage;
        this._textDisplayDurationList[i] = duration;
    }

    this._showNextMessageCallback = function(alternatingMessageTextObject) {
        alternatingMessageTextObject._showNextMessage();
    }

    this._showNextMessage = function() {
        var showIndex = this._currentDisplayIndex + 1;
        if(showIndex >= this._textMessageList.length) {
            showIndex = 0;
        }

        var obj = this;
        var method = this._showNextMessageCallback;
        var delay = this._textDisplayDurationList[showIndex];

        document.getElementById(this._divId).innerHTML = this._textMessageList[showIndex];

        this._currentDisplayIndex = showIndex;
        
        if(this._isPlaying) {
            setTimeout( function() { method(obj); }, delay );
        }
    }

    this.start = function() {
        this._isPlaying = true;
        this._showNextMessage();
    }
}
