// -------- trida pro provadeni server side prikazu --------
function executor_frame(iParent, iIndex) {
    this.index      = iIndex;
    this.parent     = iParent;
    this.callObject = null;;
    this.trgDiv     = null;
    this.trgFrame   = null;

    this.create = function(iForceId, iCallObject) {
        if (this.trgDiv == null) {
            this.trgDiv = document.createElement("DIV");
            document.body.appendChild( this.trgDiv );
        }

        var putId = iForceId == null ? ("executor_frame" + this.index + "no") : "executor_frame";
        this.trgDiv.innerHTML = "<iframe id=\"" + putId + "\" name=\"" + putId + "\" src=\"\" frameBorder=\"0\" width=\"0\" height=\"0\" style=\"width : 0px;height : 0px\"></iframe>";
        this.callObject = iCallObject;

        do {
            this.trgFrame = document.getElementById(putId);
        } while (this.trgFrame == null || this.trgFrame.contentWindow == null);

        return this.trgFrame;
    }

    this.destroy = function() {
        if (this.trgFrame != null) {
            this.trgDiv.removeChild(this.trgFrame);
            this.trgFrame = null;
        }
    }
}

function executor(iTrgFrameObj) {
	this.trgFrame    = iTrgFrameObj;
	this.trgDiv      = null;
	this.initFrame   = (this.trgFrame != null);

	this.frameBuffer    = new Array();
	this.frameBufferLen = 5;

    for (var f = 0; f < this.frameBufferLen; f++)
        this.frameBuffer[f] = new executor_frame(this, f);

	this.post_form = document.getElementById("executor_post_form");
	this.post_cmd = document.getElementById("executor_post_cmd");

	this.callObject = null;
	this.lastRedir = "";

    this.getCallObject = function(iIndex) {
        if (iIndex == null) return this.callObject;
        else return this.frameBuffer[iIndex].callObject;
    }

	this.make = function(iCmd, iCallObj) {
        this.makeGet(iCmd, iCallObj, "");
	}

    this.makeGet = function(iCmd, iCallObj, iParams) {
        var uFrame;

        if ((uFrame = this.createFrame(null, iCallObj)) != null) {
            var redir = pathPrefix + "executor.php?cmd=" + iCmd;
            if (iParams != "") redir += "&" + iParams;
            redir += "&index=" + this.getIndex(uFrame);

    		this.callObject = iCallObj;
            uFrame.contentWindow.document.location = redir;
		}
    }

	this.makePost = function(iCmd, iCallObj) {
        var uFrame;
        if ((uFrame = this.createFrame("executor_frame", iCallObj)) != null) {
    		this.callObject = iCallObj;
    		if (this.post_form != null && this.post_cmd != null) {
    			this.post_cmd.value = iCmd;

    			this.post_form.submit();
    		}
		}
	}

    this.getIndex = function(iFrameRef) {
        var pos = 0;
        var founded = false;

        while (!founded && pos < this.frameBuffer.length)
            if (this.frameBuffer[pos].trgFrame == iFrameRef) founded = true;
            else pos++;

        return founded ? pos : null;
    }

	this.createFrame = function(iForceId, iCallObject) {
        if (this.initFrame) return this.trgFrame;
        else {
            var pos = 0;

            while (pos < this.frameBuffer.length && this.frameBuffer[pos].trgFrame != null) pos++;

            if (pos < this.frameBuffer.length)
                return this.frameBuffer[pos].create(iForceId, iCallObject);
        }
        return null;
    }

    this.destroyFrame = function(iIndex) {
        if (!this.initFrame && iIndex != null)
            this.frameBuffer[iIndex].destroy();
    }

	this.makeEnd = function(iIndex) {
        if (!this.initFrame)
            this.destroyFrame(iIndex);
    }
}
