/**
 * Plotter effects
 *
 * @changes 2005-11-08 / marc / Created
 *
 * @copyright Copyright © 2005 / CHILIBYTES Böhm & Welte GbR / www.chilibytes.com
 * @author marc
 * @version 0.0.1
 * @package Javascript
 * @subpackage Plotter
 *
 */

/**
 *
 */
var _globalThisA = new Array();

/**
 * Constructor
 */
function CB_Plotter() {
    this.constructor = CB_Plotter;
    // Plotter
    this._plotSettings = new Array();
    this._plotSettings["text1A"] = null;
    this._plotSettings["text2A"] = null;
    this._plotSettings["oddRun"] = null;
    this._plotSettings["timeout"] = null;
    this._plotSettings["objId"] = null;
    this._plotSettings["timeStep"] = null;
}

/**
 *
 */
CB_Plotter.prototype.Plot = function( objId, textValue, timeStep, appendText ) {
    _globalThisA[objId] = this;
    if ( appendText != true ) {
        this._PlotObjClear( objId );
    }
    this._plotSettings["text1A"] = textValue.split( "" );
    this._plotSettings["text2A"] = new Array();
    this._plotSettings["oddRun"] = true;
    this._plotSettings["objId"] = objId;
    this._plotSettings["timeStep"] = parseInt( timeStep );
    this._plotSettings["timeout"] = window.setTimeout( "_globalThisA['" + objId + "']._PlotThread()", 0 );
}

/**
 *
 */
CB_Plotter.prototype._PlotThread = function() {
    var objId = this._plotSettings["objId"];
    var obj = document.getElementById( objId );
    var actElem = null;
    var primA = null;
    var secA = null;
    if ( obj ) {
        this._BufferOverflowClear( 1024, obj );
        if ( this._plotSettings["oddRun"] == true ) {
            primA = this._plotSettings["text1A"];
            secA = this._plotSettings["text2A"];
        }
        else {
            primA = this._plotSettings["text2A"];
            secA = this._plotSettings["text1A"];
        }
        actElem = primA.shift();
        secA.push( actElem );
        if ( primA.length == 0 ) this._plotSettings["oddRun"] = !this._plotSettings["oddRun"];
        if ( actElem ) {
            obj.firstChild.data += actElem;
            this._plotSettings["timeout"] = window.setTimeout( "_globalThisA['" + objId + "']._PlotThread()", this._plotSettings["timeStep"] );
        }
    }
}

/**
 *
 */
CB_Plotter.prototype._PlotObjClear = function( objId ) {
    var obj = document.getElementById( objId );
    if ( obj ) {
        while( obj.hasChildNodes() ) {
            obj.removeChild( obj.firstChild );
        }
        obj.appendChild( document.createTextNode( "" ) );
    }
}

/**
 *
 */
CB_Plotter.prototype._BufferOverflowClear = function( bufferSize, obj ) {
    if ( obj && obj.firstChild ) {
        var buffer = obj.firstChild.data;
        if ( buffer.length > bufferSize ) {
        	obj.firstChild.data = buffer.substring( buffer.length - bufferSize, buffer.length );
        }
    }
}
