﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("AirQuestSite");

AirQuestSite.ScriptCallbackControl = function(element) {
    AirQuestSite.ScriptCallbackControl.initializeBase(this, [element]);
}

AirQuestSite.ScriptCallbackControl.prototype = {

    _cbk: '',
    //_fnCallback: null,
    _respCall: null,
    onLoad$handler: null,

    initialize: function() {
        AirQuestSite.ScriptCallbackControl.callBaseMethod(this, 'initialize');

        this.onLoad$handler = Function.createDelegate(this, this._onLoad);
        Sys.Application.add_load(this.onLoad$handler);
    },

    doPostBack: function(arg, completionCall, contextData) {
        var key = "C" + (AirQuestSite.ScriptCallbackControl._id_counter++).toString();
        AirQuestSite.ScriptCallbackControl._callTable[key] = { compl: completionCall, ctx: contextData };

        var fnCallback = new Function("arg", this._cbk.replace("'__@@@@__'", "arg") + ";");
        fnCallback(key + ":" + arg);
    },

    serverResponse: function(callid, response) {
        var c = AirQuestSite.ScriptCallbackControl._callTable[callid];
        if(c) {
            try {
                if(c && (typeof (c.compl) == "function")) {                    
                    c.compl(response, c.ctx);
                }
            } catch(ex) {
                if(confirm('V kontrolu ScriptCallback dolo k potížím (response), spustit debug? Chyba: ' + ex)) {
                    debugger;
                }
            }
            AirQuestSite.ScriptCallbackControl._callTable[callid] = null;
            delete AirQuestSite.ScriptCallbackControl._callTable[callid];
        }
    },

    _onLoad: function() {               
        
        
        if(this._respCall) {
            try {
                this._respCall();
            } catch(ex) {
                if(confirm('V kontrolu ScriptCallback dolo k potížím (load), spustit debug? Chyba: ' + ex)) {
                    debugger;
                }
            }
            this._respCall = null;
        }
        

        Sys.Application.remove_load(this.onLoad$handler);
        this.onLoad$handler = null;
    },

    dispose: function() {
        AirQuestSite.ScriptCallbackControl.callBaseMethod(this, 'dispose');
    }
}
AirQuestSite.ScriptCallbackControl.registerClass('AirQuestSite.ScriptCallbackControl', Sys.UI.Control);

if(!AirQuestSite.ScriptCallbackControl._callTable) {
    AirQuestSite.ScriptCallbackControl._callTable = {};
    AirQuestSite.ScriptCallbackControl._id_counter = 1;
}

if(typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

