// **********************************************************************
// * Functions list:
// * ar(module_instance, command, params, extraParam) 	- Create query for some 'module'. Define 'command' and 'params'.
// *
// **********************************************************************
// * Event processing functions (optional):
// * csf.onShowWait() - Wait state switch on (called after sending request to server with given delay 'ajax_ShowWaitDelay')
// *
// **********************************************************************
var csf = {
	ajax_ShowWaitDelay : 1000,
	ajax_WaitTimerID : false,

	ajax_doRequest : function(receiver, command, params) {
		if (!jQuery || !jQuery.ajaxSettings.url) return;

		var query='t='+receiver+'&c='+command+'&'+params;

		if (typeof(arguments[3]) == 'function') {
			ajax_afterResponse=arguments[3];
		} else {
			ajax_afterResponse=function(){};
		}
		if (this.onShowWait) {
			if (this.ajax_WaitTimerID)
				window.clearTimeout(this.ajax_WaitTimerID);
			this.ajax_WaitTimerID=window.setTimeout(this.onShowWait, this.ajax_ShowWaitDelay);
		}
		if (typeof(arguments[3]) == 'boolean') {
			document.location.href=jQuery.ajaxSettings.url + (jQuery.ajaxSettings.url.indexOf('?')!=-1?'&':'?') + query;
		} else {
			if (this.onRequestBegin) this.onRequestBegin(receiver);
			$.ajax({data:query, success:function(req){ csf.ajax_processResponse(req); ajax_afterResponse(req)}});
		}
	},

	ajax_processResponse : function(req) {

		if (csf.ajax_WaitTimerID) {window.clearTimeout(csf.ajax_WaitTimerID);csf.ajax_WaitTimerID=0;}

		for(action in req) {
			switch(action) {
				case 'h':
					for (query in req.h) {
						$(query).html(req.h[query]);
					}
					break;
				case 'js':
					try	{
						eval(req.js);
						response_js(req);
					}catch(e) { console.error(e);}
					break;
				case 'mc':
					try	{
						if (window.onShowMessage) window.onShowMessage(parseInt(req.mc.code), req.mc.module);
					}catch(e) {console.error(e);}
					break;
				case 'jump':
					document.location.href=req.jump;
					break;
			}
		}
	},

	ajax_coverArea : function(which)	{
		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.ajax_coverArea('module_area_'+receiver+'_area'); },
	onShowWait : function() { $('#cover').addClass('wait_state'); },
	esc : function(s){return escape(s).replace(new RegExp('\\+','g'),'%2B');},

	delay_timer:null,
	delaySubmit : function(theForm,e)	{
		if(e['keyCode']==40 || e['keyCode']==38 || e['keyCode']==13 || e['keyCode']==27) return;
		if (this.delay_timer)clearTimeout(this.delay_timer);
		this.delay_timer=window.setTimeout( function(){theForm.submit();},500);
	}
}

function ar(receiver, command, params) {
	csf.ajax_doRequest(receiver, command, params, arguments[3]);
}
