/* Provide a queueing system to stop 
   > too many AJAX requests being fired concurrently
*/
function ajax_queue(a_script){
	this.ajax_script = a_script;
	this.ajax_processing = new Array();
	this.ajax_history = new Array();
	this.ajax_queue = new Array();
	this.process_max = 3;
	this.use_jsonp = false;
	this.sendAjaxReq_callback = 'ajax_handle';
	
	// remove process from queue and fire ajax request
	this.start_process = function(){
		if(this.ajax_queue.length >= 1 && this.ajax_processing.length < this.process_max){	
			var process = new ajax_process(this.ajax_queue.shift());
			
			this.ajax_history[process.pid] = process;
			this.ajax_processing.push(process.pid);
	
			if( this.use_jsonp != true ){
				// must uri encode search value before attempting to submit
				sendAjaxReq( '/ajaxx/' + this.ajax_script + '.php?' + process.uri_str, this.sendAjaxReq_callback );
			}else{
				
				var domhost = window.location.hostname;
				
				switch( window.location.hostname ){
					case 'www.hl.co.uk':
						domhost = 'online.hl.co.uk';
						break;
					case 'qa.hl.co.uk':
						domhost = 'qa-online.hl.co.uk';
						break;
				}

				$.ajax({
					url: 'https://'+domhost+'/ajaxx/'+this.ajax_script + '.php',
					data: process.uri_str+'&format=jsonp',
					type: 'GET',
					dataType: 'jsonp',
					timeout: 5000,
					error: function(xmlhttp,msg){}
				});
			}

			
		}
	}
	
	// insert process into queue awaiting processing
	this.queue_process = function(params, enable_history){
		if(enable_history == true){
			for(var pid in this.ajax_history){
				if(is_numeric(pid) == true){ 
					if(this.ajax_history[pid].process_match(params) == true){
						// this is a re-run, return existing data
						return this.ajax_history[pid].response;
					}
				}
			}
		}

		this.ajax_queue.push(params);

		return true;
	}

	// manage and close processes
	this.close_process = function(pid, response){
		// locate & remove job from processing
		for(var p in this.ajax_processing){
			if(this.ajax_processing[p] == pid){
				this.ajax_history[pid].set_response(response);
				this.ajax_processing.splice(p, 1);
				break;
			}
		}

		 this.start_process(); // fire next process in queue
	}
	
	// how many requests are queued?
	this.get_queue_length = function(){
		return this.ajax_queue.length;
	}
}

// for every AJAX request create a process
function ajax_process(params){
	this.pid = (new Date()).getTime();
	this.params = params;
	
	// format request params
	var uri_params = new Array('pid=' + this.pid); 

	for(var key in params) uri_params.push(key + '=' + params[key]);

	this.uri_str = uri_params.join('&');

	// all attributes have to match to re-use previous data
	this.process_match = function(attrib){
		for(var key in this.params){
			if(this.params[key] != attrib[key]) return false
		}
		return true;
	}

	// get request param
	this.get_param = function(i){
		if(this.params[i]) return this.params[i];
	}
	
	// record completion of process
	this.set_response = function(response){
		this.response = response;
		this.status = true;
	}
}

// append AJAX process request to queue
function add_to_queue(params, enable_history){
	var result = ajax_queue.queue_process(params, enable_history); // queue process

	if(typeof(result) == 'object'){
		ajax_handle(result);
	}else{
		ajax_queue.start_process(); // attempt to fire process
	}
}
