if(!AjaxRequest) throw('AjaxQueue betoltesehez hianyzik: AjaxRequest');

var AjaxQueue = {
	queue : new Array,
	Request : null,
	isUpdating : false,
	timeout : null,
	rIdx : 0,
	delay : 100,

	init : function(){
		//this.timeout=setTimeout('AjaxQueue.execute();',100);
	},

	add : function(url,callBack,params){
		var idx=this.queue.length;
		this.queue[idx]=new Array();
		this.queue[idx]['url']=url+(url.indexOf('?')!=-1?'&':'?')+'rnd='+new Date().getTime()*Math.random();
		this.queue[idx]['callBack']=callBack;
		this.queue[idx]['params']=(params)?params:null;

		var str='';
		for(var i=0;i<this.queue.length;i++) str+=this.queue[i].url+'\r\n';

		this.execute();
	},

	onReady : function(response){
		AjaxQueue.isUpdating=false;
		AjaxQueue.rIdx++;
		AjaxQueue.queue[AjaxQueue.rIdx-1].callBack(response);
	},

	execute : function(){
		if(!this.hasQueuedCommand()){
			this.queue=new Array();
			this.Request=null;
			this.rIdx=0;
		} else {
			if(!this.isUpdating){
				this.isUpdating=true;

				this.Request=new AjaxRequest();
				this.Request.onReadyHandler=this.onReady;
				if(this.queue[this.rIdx].params) this.Request.sendPOST(this.queue[this.rIdx].url,this.queue[this.rIdx].params);
				else this.Request.send(this.queue[this.rIdx].url);
			}
			this.timeout=setTimeout('AjaxQueue.execute();',this.delay);
		}
	},

	hasQueuedCommand : function(){ return (this.queue.length>this.rIdx); }
}

Utils.addInit('AjaxQueue.init');