//TODO: some of the base functionality implemented here can be moved to the
//coss namespace/toolkit for reuse.


//define global coss 'namespace' if it doesn't already exist
if (('coss' in window) == false) window.coss = {};

coss.Conduit = function () {};

coss.Conduit.prototype.getAjaxTunnel = function () {
	if (this._conduit.ajaxTunnel == null) {
		this._conduit.ajaxTunnel = new coss.AjaxTunnel();
		this._conduit.ajaxTunnel.construct();
	}
	
	return this._conduit.ajaxTunnel;
};

coss.Conduit.prototype.addDataBinder = function (aBinder) {
	this._conduit.dataBinders.push(aBinder);
};

coss.Conduit.prototype.bindData = function (aData) {
	var i;
	
	for (i = 0; i < this._conduit.dataBinders.length; i++) {
		//call data binder function in global/window scope
		this._conduit.dataBinders[i].apply(window, [aData]);
	}
};

coss.Conduit.prototype.construct = function () {
	this._conduit = {
		dataBinders : [],
		ajaxTunnel : null,
		iframeTunnel : null
	};
};




coss.AjaxTunnel = function () {};

coss.AjaxTunnel.prototype.execute = function (aData, aOptions) {
	//TODO: much of this should be configurable
	
	if (this._ajaxTunnel.url == null) {
		throw "Invalid URL: '" + this._ajaxTunnel.url + "'";
	}
	
	this.dispatchEvent('begin');
	
	jQuery.ajax({
		'context' : this,
		'dataType' : 'json',
		'data' : aData,
		'type' : this._ajaxTunnel.method,
		'url' : this._ajaxTunnel.url,
		'success' : this._ajaxTunnel.jquerySuccessHandler,
		'error' : this._ajaxTunnel.jqueryErrorHandler
	});
};

coss.AjaxTunnel.prototype.setMethod = function (aMethod) {
	this._ajaxTunnel.method = aMethod;
};

coss.AjaxTunnel.prototype.getUrl = function () {
	return this._ajaxTunnel.url;
};

coss.AjaxTunnel.prototype.setUrl = function (aUrl) {
	this._ajaxTunnel.url = aUrl;
};

coss.AjaxTunnel.prototype.addEventListener = function (aEventType, aFunction) {
	if ((aEventType in this._ajaxTunnel.eventListeners) == false) {
		throw "Unknown event type: '" + aEventType + "'.";
	}
	
	this._ajaxTunnel.eventListeners[aEventType].push(aFunction);
};

coss.AjaxTunnel.prototype.dispatchEvent = function (aEventType, aEvent) {
	var i;
	
	if ((aEventType in this._ajaxTunnel.eventListeners) == false) {
		throw "Unknown event type: '" + aEventType + "'.";
	}
	
	for (i = 0; i < this._ajaxTunnel.eventListeners[aEventType].length; i++) {
		this._ajaxTunnel.eventListeners[aEventType][i].apply(this, [aEvent]);
	}
};

coss.AjaxTunnel.prototype.construct = function () {
	this._ajaxTunnel = {
		method : null,
		url : null,
		
		eventListeners : {
			'begin' : [],
			'end' : []
		},
		
		jquerySuccessHandler : function (aJson) {
			this.dispatchEvent('end', {
				error : null,
				data : aJson
			});
		},
		
		jqueryErrorHandler : function (aXhr, aStatus) {
			this.dispatchEvent('end', {
				error : {type : aStatus},
				data : null
			});
		}
	};
	
	this.setMethod('post');
	this.setUrl('index.php');
};
