﻿var ADAM_VERSION = "";
var ADAM_FILEVERSION = "";
var DEFAULT_REGISTRATIONNAME = "";
var EXCEPTION_TEXT = "";

function createDelegate(target, targetMethod) {
    return function() {
        targetMethod.apply(target);
    }
}

function DOSConsole(path) {
    this.path = path;
    this._actions = [];
    this._cursor = null;
    this._currentAction = 0;
    this.isRunning = false;
}
DOSConsole.prototype = {
    addAction: function(action) {
        this._actions.push(action);
        action.console = this;
    },
    clearActions: function() {
        this._actions = [];
    },
    close: function() {
        this._currentAction = this._actions.length;
        if (this._currentTimer) {
            window.clearTimeout(this._currentTimer);
            this._currentTimer = null;
        }
        this.panel.parentNode.parentNode.style.display = 'none';
    },
    execute: function() {
        this.isRunning = true;
        this._currentAction = 0;
        if (!this.panel) {
            this.panel = document.getElementById("divError");
            this.panel.innerHTML = "";
            document.body.appendChild(this.panel.parentNode.parentNode);
        }
        this.panel.parentNode.parentNode.style.display = '';
        // Create a cursor.
        if (!this._cursor) {
            this._cursor = document.createElement("span");
            this._cursor.innerHTML = "_";
            this.panel.appendChild(this._cursor);
            window.setInterval(createDelegate(this, this._blinkCursor), 500);
        }
        this._executeNext();
    },
    _blinkCursor: function() {
        if (this._cursor) {
            if (this._cursor.style.visibility == "hidden") {
                this._cursor.style.visibility = "visible";
            } else {
                this._cursor.style.visibility = "hidden";
            }
        }
    },
    _executeNext: function() {
        if (this._actions.length <= this._currentAction) {
            // End
            this.isRunning = false;
            if (this.onfinish) {
                this.onfinish();
            }
            return;
        }
        var oAction = this._actions[this._currentAction];
        if (oAction.execute() == true) {
            this.actionCompleted();
        }

    },
    _doNextAction: function() {
        this._currentTimer = null;
        this._currentAction++;
        this._executeNext();
    },
    actionCompleted: function() {
        var oDelegate = createDelegate(this, this._doNextAction);
        var oAction = this._actions[this._currentAction];
        this._currentTimer = window.setTimeout(oDelegate, oAction.delay);
    },
    addText: function(text) {
        var oSpan = document.createElement("span");
        oSpan.innerHTML = text.replace(/\n/g, "<br/>").replace(/ /g, "&nbsp;");
        this.panel.insertBefore(oSpan, this._cursor);

        var iScrollTop = this.panel.scrollHeight - this.panel.clientHeight; // (this._cursor.offsetTop + this._cursor.offsetHeight) - this.panel.clientHeight;
        if (iScrollTop > 0) {
            this.panel.scrollTop = iScrollTop;
        }

        return oSpan;
    }
}

function DOSAction() {
    this.delay = 10;
    this.console = null;
}

function DOSLine(text, delay, showTime) {
    this.text = text;
    if (delay) {
        this.delay = delay;
    }
    this._showTime = showTime ? true : false;
}
DOSLine.prototype = new DOSAction();
DOSLine.prototype.execute = function() {

    var sText = this._createTimeIfNeeded() + this.text;

    while (sText.length > 80) {
        this.console.addText(sText.substr(0, 80) + "\n");
        sText = sText.substr(80);
    }
    this.console.addText(sText);
    return true;
}
DOSLine.prototype._createTimeIfNeeded = function() {
    if (this._showTime == true) {
        var oNow = new Date();
        return this._formatNumber(oNow.getHours(), 2) + ":" + this._formatNumber(oNow.getMinutes(), 2) + ":" + this._formatNumber(oNow.getSeconds(), 2) + ":" + this._formatNumber(parseInt(oNow.getMilliseconds() / 10), 3) + " ";
    }
    return "";
}
DOSLine.prototype._formatNumber = function(number, size) {
    var sText = "000" + number;
    return sText.substr(sText.length - size);
}

function DOSStatusLine(text) {
    this.text = text;
    this.delay = 1;
    this._showTime = false;
}
DOSStatusLine.prototype = new DOSLine();
DOSStatusLine.prototype.execute = function() {
    var sText = this.text;
    while (sText.length > 80) {
        var oSpan = this.console.addText(sText.substr(0, 80) + "\n");
        oSpan.style.backgroundColor = 'yellow';
        oSpan.style.fontWeight = 'bold';
        oSpan.style.color = 'red';
        sText = sText.substr(80);
    }
    var oSpan = this.console.addText(sText);
    oSpan.style.backgroundColor = 'yellow';
    oSpan.style.fontWeight = 'bold';
    oSpan.style.color = 'red';
    return true;
}

function DOSCounter(text, maxStepSize, max, showTime) {
    this.delay = 1;
    this.text = text;
    this._maxStepSize = maxStepSize <= 1 ? 2 : maxStepSize;
    this._max = max;
    this._currentValue = 1;
    this._showTime = showTime ? true : false;
}
DOSCounter.prototype = new DOSLine();
DOSCounter.prototype.execute = function() {
    var sText = this._createTimeIfNeeded() + this.text;
    var sSplitted = sText.split("{0}");
    this.console.addText(sSplitted[0]);
    this._counterElement = this.console.addText("1");
    sSplitted = sSplitted[1].split("{1}");
    this.console.addText(sSplitted[0]);
    if (sSplitted[1]) {
        this.console.addText(this._max.toString());
        this.console.addText(sSplitted[1]);
    }
    this._nextNumberDelegate = createDelegate(this, this._showNextNumber);
    this._showNextNumber();
    return false;
}
DOSCounter.prototype._showNextNumber = function() {
    var randomnumber = Math.floor(Math.random() * this._maxStepSize);
    this._currentValue += randomnumber;
    if (this._currentValue > this._max) {
        this._currentValue = this._max;
        this.console.actionCompleted();
    } else {
        this.console._currentTimer = window.setTimeout(this._nextNumberDelegate, 50);
    }
    this._counterElement.innerHTML = this._currentValue;

}

function DOSPath() {
    this.delay = 1;
}
DOSPath.prototype = new DOSAction();
DOSPath.prototype.execute = function() {
    this.console.addText(this.console.path);
    return true;
}

function DOSKeyInput(text) {
    this.text = text;
    this._textBlock = null;
    this._currentKey = 0;
}
DOSKeyInput.prototype = new DOSAction();
DOSKeyInput.prototype.execute = function() {
    this._textBlock = this.console.addText("");
    this._keyDelegate = createDelegate(this, this._nextKey);
    this._nextKey();
    return false;
}
DOSKeyInput.prototype._nextKey = function() {
    if (this._currentKey == this.text.length) {
        this.console.actionCompleted();
    } else {
        this._currentKey++;
        this._textBlock.innerHTML = this.text.substr(0, this._currentKey).replace(/\n/g, "<br/>");
        this.console._currentTimer = window.setTimeout(this._keyDelegate, 90);
    }
}
