var g_objRequests = new Array();

trimString = function(in_strString) {

    in_strString = in_strString.replace(/^\s+/g, '');
    return in_strString.replace(/\s+$/g, '');

}

shadeObject = function(in_strDestination, in_blnShade) {

    if (!document.getElementById(in_strDestination)) {
        in_strDestination = 'view';
    }

    if (in_blnShade) {

        if (document.getElementById(in_strDestination + '_shade')) {
            return;
        }

        var objContainerNode = document.createElement('div');

        objContainerNode.id = in_strDestination + '_shade';
        objContainerNode.className = 'LoadingIcon';
        objContainerNode.style.marginTop = -1 * ($(in_strDestination).offsetHeight) + 'px';
        objContainerNode.style.width = $(in_strDestination).offsetWidth + 'px';
        objContainerNode.style.height = $(in_strDestination).offsetHeight + 'px';
        $(in_strDestination).appendChild(objContainerNode);

    } else {

        var objContainerNode = $(in_strDestination + '_shade');

        if (objContainerNode) {
            objContainerNode.parentNode.removeChild(objContainerNode);
        }

    }

}

getObject = function(in_strDestination, in_strPath) {

    shadeObject(in_strDestination, true);
    //g_objRequests.push(new Request(in_strPath, in_strDestination, false));
    var obj_r = new Request(in_strPath, in_strDestination, false);
}

Request = Class.create();
Request.prototype = {

    m_intId: null,

    m_strUrl: null,

    m_strDestination: null,

    m_blnIsWorking: false,

    m_strResources: new Array(),

    m_intResourcesCount: 0,

    m_intTries: 0,

    m_strBody: null,

    initialize: function (in_strUrl, in_strDestination, in_blnPost) {

        if (!document.getElementById(in_strDestination)) {
            in_strDestination = 'view';
        }

        if (!document.getElementById(in_strDestination)) {
            this.reload();
        }

        this.m_intId = g_objRequests.length;
        this.m_strUrl = in_strUrl.replace('&amp;', '&');
        this.m_strDestination = in_strDestination;

        //prompt('init: ' + in_strDestination + '; method: ' + (in_blnPost != null ? 'post' : 'get'), in_strUrl);

        g_objRequests.push(this);

        var objRequest = new Ajax.Request(

            this.m_strUrl, {
                asynchronous: true,
                method: in_blnPost ? 'post' : 'get',
                parameters: (in_blnPost && document.getElementById(this.m_strDestination + '_form') ? Form.serialize($(this.m_strDestination + '_form')) : '') + '&_phase=init&_dest=' + this.m_strDestination,
                onSuccess: new Function('objRequest', 'g_objRequests[' + this.m_intId + '].loadResources(objRequest.responseXML, objRequest.responseText)'),
                onFailure: new Function('objRequest', 'g_objRequests[' + this.m_intId + '].reload()'),
                onException: new Function('o, e', 'alert(e)')
            }

        );

        if (! objRequest) {
            this.reload();
        }

        this.m_blnIsWorking = true;

    },

    showError: function (in_strMessage) {

        objPage = window.open('', 'error', 'toolbar=0, scrollbars=1, location=0, statusbar=0, menubar=0, resizable=0, width=640, height=480');
        objPage.document.write('<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf8" \/><title>Error!<\/title><\/head><body>' + in_strMessage + '<\/body><\/html>');
        objPage.document.close();
        shadeObject(this.m_strDestination, false);

    },

    reload: function () {

        document.location.href = document.location.href.replace('/E', '');

    },

    loadResources: function (in_objXml, in_strText) {

        if (in_objXml == null) {
            this.showError(in_strText);
            return;
        }

        var objXml = in_objXml.documentElement;

        if (objXml.tagName == 'parsererror') {
            this.showError(in_strText);
        }

        var objElements = objXml.getElementsByTagName("error");

        if (typeof(objElements[0]) != 'undefined' && objElements[0] != null) {

            objElements[0].firstChild.data = trimString(objElements[0].firstChild.data);
			alert(objElements[0].firstChild.data);
            this.reload();

        }

        var objElements = objXml.getElementsByTagName("redirect");

        if (typeof(objElements[0]) != 'undefined' && objElements[0] != null) {

            objElements[0].firstChild.data = trimString(objElements[0].firstChild.data);
            document.location.href = objElements[0].firstChild.data;
            return;

        }

        objElements = objXml.getElementsByTagName("getform");

        if (typeof(objElements[0]) != 'undefined' && objElements[0] != null) {

            objElements[0].firstChild.data = trimString(objElements[0].firstChild.data);
            return getForm(objElements[0].getAttribute('id'), objElements[0].firstChild.data);

        }

        objElements = objXml.getElementsByTagName("getobject");

        if (typeof(objElements[0]) != 'undefined' && objElements[0] != null) {

            objElements[0].firstChild.data = trimString(objElements[0].firstChild.data);
            return getObject(objElements[0].getAttribute('id'), objElements[0].firstChild.data);

        }

        objElements = objXml.getElementsByTagName("file");

        for (var i = 0; i < objElements.length; i++) {

            if (g_strResource.indexOf(objElements[i].firstChild.data) != -1) {
                continue;
            }

            this.m_strResources.push(new Array(objElements[i].firstChild.data, objElements[i].getAttribute('type')));

        }

        this.m_strResources.reverse();
        this.m_intResourcesCount = this.m_strResources.length;

        objElements = objXml.getElementsByTagName("body");

        if (Prototype.Browser.IE) {
	        this.m_strBody = objElements[0].firstChild.data;
	    } else {
		    this.m_strBody = objElements[0].childNodes[1].data;
	    }

        if (this.m_intResourcesCount == 0) {
            return this.show();
        }

        var strResourceInfo = this.m_strResources.pop();
        this.loadResource(strResourceInfo[0], strResourceInfo[1]);

    },

    loadResource: function (in_strFile, in_strType) {

        new Ajax.Request(

            in_strFile, {

                method: 'get',
                onComplete: new Function(
                    'objRequest',
                    'g_objRequests[' + this.m_intId + '].evalResource("' + in_strFile + '", "' + in_strType + '", objRequest.responseText)'
                )

            }

        );

    },

    evalResource: function (in_strFile, in_strType, in_strText) {

        try {

            if (in_strType == 'text/javascript') {

                var objLink = document.createElement('script');
                objLink.setAttribute('type', in_strType);
                objLink.setAttribute('src', in_strFile);

                document.getElementsByTagName('head').item(0).appendChild(objLink);

            } else if (in_strType == 'text/css') {

                var objLink = document.createElement('link');
                objLink.setAttribute('rel', 'stylesheet');
                objLink.setAttribute('type', in_strType);
                objLink.setAttribute('href', in_strFile);

                document.getElementsByTagName('head').item(0).appendChild(objLink);

            }

        } catch (strError) {

            if (this.m_intTries < 2) {

                this.loadResource(in_strFile, in_strType);

            } else {

                this.reload();
                return;

            }

            this.m_intTries++;

        }

        g_strResource += in_strFile + ' ';

        if (--this.m_intResourcesCount == 0) {

			if (Prototype.Browser.IE) {
			     return setTimeout('g_objRequests[' + this.m_intId + '].show()', 1);
			}

            return this.show();

        }

        var strResourceInfo = this.m_strResources.pop();
        this.loadResource(strResourceInfo[0], strResourceInfo[1]);

    },

    show: function () {

        this.m_strBody = this.m_strBody.replace(']ENDOFCDATA]>', ']]>');
        $(this.m_strDestination).innerHTML = this.m_strBody;
        this.m_strBody.evalScripts();
        this.m_blnIsWorking = false;

    }

}