// **********************************************************************
// * Functions list:
// * ajax_makeQuery(area, module, command, params) 	- Create query for 'area' to some 'module'. Define 'command' and 'params'.
// * ajax_doRequest(query) 							- Send request to server side. 'query' - array of associative arrays
// *													created by 'ajax_makeQuery()' function
// * ajax_getFormQuery(theForm)						- Create query on base of form contents
// * ajax_setShowWaitDelay(delay)					- Set delay before calling 'onShowWait()' 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 ajax_ShowWaitDelay=1000;
var ajax_WaitTimerID;

function ajax_makeQuery(area, module, command, params) {
	return {area:area,module:module,command:command,params:params};
}

function ajax_doRequest(query) {
	if (!jQuery || !jQuery.ajaxSettings.url) return;
	if (!query[0]) query = new Array(query);

	if (window.onRequestBegin) {
		onRequestBegin(query);
	}

	var enc_query='';
	for (i=0; i<query.length; i++) {
		var item=query[i];
	  	enc_query+='&area[]='+esc('&area='+item['area']+'&module='+item['module']+'&command='+item['command']+'&'+item['params']);		
	}
	
	$.ajax({data:enc_query, success:function(req){

		if (ajax_WaitTimerID) {clearTimeout(ajax_WaitTimerID);ajax_WaitTimerID=0;}
		
		processed_areas=req.processed_areas;
		for (i=0; i<processed_areas.length; i++) {
			if ((code=req[processed_areas[i]].message_code) && window.onShowMessage) {
				onShowMessage(parseInt(code),req[processed_areas[i]]);
			}
			if (req[processed_areas[i]].refresh) {
				$('#'+processed_areas[i]).html(req[processed_areas[i]].html);
			}
			try	{
				if (a=eval('window.onRespond__'+req[processed_areas[i]]['module_name'])) {
					a(req[processed_areas[i]]);
				}
			}catch(e) {console.error(e);}
		}
		try	{
			eval(req.js_function);
			response_js();
		}catch(e) { console.error(e);}
		
		
	}});
	
	if (window.onShowWait) {
		if (ajax_WaitTimerID)
			clearTimeout(ajax_WaitTimerID);
		ajax_WaitTimerID=window.setTimeout(onShowWait, ajax_ShowWaitDelay);
	}
	
}

function ajax_setShowWaitDelay(delay) { ajax_ShowWaitDelay=delay; }
function ajax_getFormQuery(theForm)	{ return $(theForm).serialize(); }

var ajax_search_timer;
var ajax_search_form;
function ajax_searchOnKeyUp(theForm,e)	{
	if(e['keyCode']==40 || e['keyCode']==38 || e['keyCode']==13 || e['keyCode']==27) return;
	
	if (ajax_search_timer)clearTimeout(ajax_search_timer); 
	ajax_search_form = theForm;
	ajax_search_timer=window.setTimeout(
		function(){ajax_search_timer=0;if (ajax_search_form.onsubmit)ajax_search_form.onsubmit();else ajax_search_form.submit();},500);
}

function ajax_coverArea(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');
		$('#cover').show();
	}
}

function onRequestBegin(q) { ajax_coverArea(q[0]['area']); }
function onShowWait() { $('#cover').addClass('wait_state'); }
function ajax_uncoverArea()	{ $('#cover').hide(); }
function esc(s){return escape(s).replace(new RegExp('\\+','g'),'%2B');}