var AjaxRequest = new Class({
	options: {
		target: null
	},
	Location: document.location.protocol+"//"+document.location.host,
	busy : false,
	queue : [],
	Implements: [Options, Events],
	
	initialize: function(opt) {
		this.setOptions(opt);
	},
	
	xCall : function(params,callback,ajaxLoader) {
		var url = this.Location;
		if(this.options.target != null) url = url+"/"+this.options.target;
		var arguments = {
				url : url,
				onFailure: function() {
				//alert('request failed');
				}.bind(this),
				onComplete: function(result) {
					this.xHandleResult(result,callback);
					if(ajaxLoader == true){
						mwdental_products.setAjaxLoader(false);
					}
				}.bind(this)
		};
		$extend(params,{xCall:1});
		if(ajaxLoader == true){
			mwdental_products.setAjaxLoader(true);
		}
		var jsonrequest = new Request.JSON(arguments).post(params);
	},
	
	xHandleResult : function(result,callback){
		// delete previous erros...
		//this.xCleanupErrors();
			callback(result);    	
	},
	
	xDispatch : function(setAjax){
		if(this.busy || this.queue.length == 0)
			return;
		this.busy = true;
		var currentRequest = this.queue.pop();
		if(setAjax == true){
			this.xCall(currentRequest[0],currentRequest[1],true);
		} else {
			this.xCall(currentRequest[0],currentRequest[1]);
		}
		this.busy = false;
		this.xDispatch();
	}
});

