// **********************************************************************
// * Functions list:
// * ar(module_instance, command, params, extraParam) 	- Create query for some 'module'. Define 'command' and 'params'.
// * ar2(query, extraParam) 							- Send request to server side. 'query' - array of associative arrays
// *													created by 'ajax_makeQuery()' function
// *
// **********************************************************************
// * Event processing functions (optional):
// * onRespond__{module name}(response)				- Function which will be called after obtaining response from some module
// *													'module name'
// * onShowWait()									- Wait state switch on (called after sending request to server with given delay 'ajax_ShowWaitDelay')
// *
// * onShowMessage(code)							- Message handler (one for all modules)
// **********************************************************************
var csf = {
	ajax_ShowWaitDelay : 1000,
	ajax_WaitTimerID : false,

	ajax_makeQuery : function(receiver, command, params) {
		return {receiver:receiver,command:command,params:params};
	},
	
	ajax_doRequest : function(query) {
		if (!jQuery || !jQuery.ajaxSettings.url) return;
		if (!query[0]) query = new Array(query);
	
		var enc_query='';
		for (i=0; i<query.length; i++) {
			var item=query[i];
		  	enc_query+='&receiver[]='+this.esc('&receiver='+item['receiver']+'&command='+item['command']+'&'+item['params']);
		}
		if (typeof(arguments[1]) == 'function') {
			ajax_afterResponse=arguments[1];
		} 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[1]) == 'boolean') {
			document.location.href=jQuery.ajaxSettings.url + (jQuery.ajaxSettings.url.indexOf('?')!=-1?'&':'?') + enc_query;
		} else {
			if (this.onRequestBegin) this.onRequestBegin(query);
			$.ajax({data:enc_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 'd':
					for (module in req.d) {
						try	{
							if (a=eval('window.onRespond__'+module)) a(req.d[module]);
						}catch(e) {console.error(e);}	
					}
					break;
				case 'h':
					for (area in req.h) {
						$('.module_area_'+area).html(req.h[area]);
					}
					break;
				case 'js':
					try	{
						eval(req.js);
						response_js();
					}catch(e) { console.error(e);}				
					break;
				case 'mc':
					try	{
						if (window.onShowMessage) window.onShowMessage(parseInt(req.mc));
					}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(q) {this.ajax_coverArea('module_area_'+q[0]['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(csf.ajax_makeQuery(receiver, command, params), arguments[3]);
}

function ar2(query) {
	csf.ajax_doRequest(query, arguments[1]);
}