// **********************************************************************
// * Functions list:
// * ar(module_instance, command, params, extraParam)   - query some 'module'. Define 'command' and 'params'.
// *
// **********************************************************************
// * Event processing functions (optional):
// * ajax.onShowWait() - Wait state switch on (called after sending request to server with given delay 'ajax.showWaitDelay')
// *
// **********************************************************************
var ajax = {
    showWaitDelay : 1000,
    waitTimerId : false,
    msgWaitDelay : 5000,
    msgTimerId : {},

    doRequest : function(q) {
        if (!jQuery || !jQuery.ajaxSettings.url) return;
        first_target = q[0].t;
        if (q.length > 1) {
            query = '';
            for ( var i = 0; i < q.length; i++) {
                query += '&c[]=' + encodeURIComponent(q[i]['p'] + '&t=' + q[i]['t'] + '&c=' + q[i]['c']);
            }
        } else {
            query = q[0]['p'] + '&t=' + q[0]['t'] + '&c=' + q[0]['c'];
        }
        if (typeof(arguments[1]) == 'function') {
            afterResponse=arguments[1];
        } else {
            afterResponse=function(){};
        }
        if (this.onShowWait) {
            if (this.waitTimerId)
                window.clearTimeout(this.waitTimerId);
            this.waitTimerId=window.setTimeout(this.onShowWait, this.showWaitDelay);
        }
        if (typeof(arguments[1]) == 'boolean') {
            document.location.href=jQuery.ajaxSettings.url + (jQuery.ajaxSettings.url.indexOf('?')!=-1?'&':'?') + query;
        } else {
            if (this.onRequestBegin) this.onRequestBegin(first_target);
            $.ajax({data:query, success:function(req){ ajax.processResponse(req); afterResponse(req)}});
        }
    },

    processResponse : function(req) {

        if (ajax.waitTimerId) {window.clearTimeout(ajax.waitTimerId);ajax.waitTimerId=0;}

        for(action in req) {
            switch(action) {
                case 'h':
                    for (query in req[action]) {
                        $(query).html(req[action][query]);
                    }
                    break;
                case 'jsb':
                case 'jsa':
                    try {
                        eval('response_js = function() {'+req[action]+'}');
                        response_js(req);
                    } catch(e) { if (console) console.error(e);}
                    break;
                case 'mc':
                    try {
                        this.onShowMessage(req[action][0], req[action][1]);
                    } catch(e) { if (console) console.error(e);}
                    break;
                case 'jump':
                    document.location.href=req.jump;
                    break;
            }
        }
    },

    coverArea : function(which) {
        this._attachCover();
        if (which && $('.'+which).length && $('#cover').length) {
            w = $('.'+which).width();
            h = $('.'+which).height();
            $('#cover').css({
                left:$('.'+which).offset().left,
                top:$('.'+which).offset().top,
                width:w,
                height:h
            });
            $('#cover').removeClass('wait_state').show();
        }
    },

    onRequestBegin : function(receiver) {this.coverArea(receiver.replace(/.*\//,'') + '_area');},
    onShowWait : function() { $('#cover').addClass('wait_state'); },
    onShowMessage : function (code, widget_id) {
        var selector = '.'+widget_id+'_msg_'+code;
        $(selector).fadeIn();
        if (this.msgTimerId[selector]) window.clearTimeout(this.msgTimerId[selector]);
        this.msgTimerId[selector]=window.setTimeout(function(){$(selector).fadeOut()}, this.msgWaitDelay);
    },
    _attachCover : function() {
        if (!$('#cover').length) {
            $('body').append('<div id="cover"><img src="/r/i/progressbar.gif"><span><img src="/r/i/0.gif" width="1"></span></div>');
        }
    }
}

function ar(target, command, params) {
    ajax.doRequest([{'t':target,'c':command,'p':params}], arguments[3]);
}