var AsynchConduit = Class.create();

AsynchConduit.prototype = 
{
	initialize: function (url, container_id) 
	{
		this.initConduit(url, container_id);
		this.initConduitOptions(arguments[2]);
	},
	
	initConduit: function (url, container_id)
	{
		this.service = url || '';
		this.container = (container_id
			? $(container_id) 
			: document.createElement('DIV'));
	},
	
	initConduitOptions: function (options)
	{
		this.options = 
		{
			insertion: this.captureResponse.bind(this),
			onSuccess: this.onSuccess.bind(this),
			onComplete: this.onComplete.bind(this),
			onFailure: this.onFailure.bind(this),
			onException: this.onException.bind(this),
			method: 'get',
			evalScripts: false
		};
		
		if (options) this.setOptions(options);
	},
	
	setService: function (url) { this.service = url; },
	
	setContainer: function (container) { this.container = $(container); },
	
	setOptions: function (options) { Object.extend(this.options, options); },
	
	sendRequest: function (params, options)
	{
		var controls = 
		{
			insertion: (options && options.captureResponse 
				? options.captureResponse
				: (this.options.captureResponse 
					|| this.options.insertion)),
			onSuccess: (options && options.onSuccess 
				? options.onSuccess
				: this.options.onSuccess),
			onComplete: (options && options.onComplete 
				? options.onComplete
				: this.options.onComplete),
			onFailure: (options && options.onFailure 
				? options.onFailure
				: this.options.onFailure),
			onException: (options && options.onException 
				? options.onException
				: this.options.onException)
		}
		
		if ((options && options.evalScripts) || this.options.evalScripts)
		{
			controls.evalScripts = true;
			this.evalScripts = true;
		}
		
		if ((options && options.method 
			? options.method 
			: this.options.method).toLowerCase() == 'post')
		{
			controls.method = 'post';
			controls.postBody = params;
		}
		else
		{
			controls.method = 'get';
			controls.parameters = params;
		}
		
		if (!options) options = {};
				
		//	sig: Ajax.Updater(container, service, options)
		this.ajaxObj = new Ajax.Updater(
			(options.container || this.options.container || this.container), 
			(options.service || this.options.service || this.service), 
			Object.extend(Object.extend({}, this.options), controls));
	},
	
	captureResponse: function (element, response)
	{
	//	alert("AsynchConduit's captureResponse");
		$(element).innerHTML = response;
		if (this.evalScripts) response.evalScripts();
	},
	
	onSuccess: function ()
	{
	//	alert('onSuccess');
	}, 
	
	onComplete: function ()
	{
	//	alert('onComplete');
	},
	
	onFailure: function ()
	{
	//	alert('onFailure');
	},
	
	onException: function ()
	{
	//	alert('onException');
	}
}