//TODO: some of the base functionality implemented here can be moved to the
//coss namespace/toolkit for reuse.


//define global shaw 'namespace' if it doesn't already exist
if (('shaw' in window) == false) window.shaw = {};


/**
 * @class shaw.Module
 * Site-level module class.
 */

shaw.Module = function () {};

shaw.Module.prototype.handleMainConduitAjaxEnd = function (aResult) {
	if (aResult.error == null) {
		gModule.getMainConduit().bindData(aResult.data);
	}
	else {
		gModule.getMainConduit().bindData({
			system : {
				notifications : [
					{
						body :
							"An error has ocurred, please try again."
					}
				]
			}
		});
	}
};

shaw.Module.prototype.getMainConduit = function () {
	return this._module.mainConduit;
};

shaw.Module.prototype.getHashManager = function () {
	if (this._module.hashManager == null) this._module.hashManager = new coss.HashManager();
	return this._module.hashManager;
};

shaw.Module.prototype.getPendingData = function () {
	return this._module.pendingData;
};

shaw.Module.prototype.setPendingData = function (aData) {
	this._module.pendingData = aData;
};

shaw.Module.prototype.getHelper = function (aCode) {
	//TODO: check if exists and throw exception
	return this._module.helpers[aCode];
};

shaw.Module.prototype.setHelper = function (aCode, aHelper) {
	this._module.helpers[aCode] = aHelper;
};

shaw.Module.prototype.resolveHelpers = function () {
	
};

/**
 * Attaches event listeners to UI, and otherwise connects script to the elements
 * of the document.
 */
shaw.Module.prototype.hookUp = function () {
	
};

/**
 * Binds pending data, or copies data from pending data into global variables
 * for later use, sets keyboard focus to a default element, etc.
 */
shaw.Module.prototype.init = function () {
	gModule.getMainConduit().bindData(gModule.getPendingData());
};

shaw.Module.prototype.construct = function () {
	var t;
	
	/**
	 * Since JS does not support private members, we store all members we wish to
	 * keep private in an object named in such a way to avoid naming collisions
	 * when this class is extended. All 'private' member names should be prefixed
	 * with an underscore.
	 */
	this._module = {
		pendingData : null,
		hashManager : null,
		helpers : {},
			
		/**
		 * The main function that handles binding of data from ajax, to the UI.
		 */
		dataBinder : function (aData) {
			var i;
			
			if (aData) {
				if (aData.system && aData.system.notifications) {
					for (i = 0; i < aData.system.notifications.length; i++) {
						alert(aData.system.notifications[i].body);
					}
				}
			}
		}
	};
	
	t = new coss.Conduit();
	t.construct();
	t.addDataBinder(this._module.dataBinder);
	this._module.mainConduit = t;
	
	t = this.getMainConduit().getAjaxTunnel();
	t.addEventListener('end', this.handleMainConduitAjaxEnd);
	
	$(window).bind('load', function() {
		gModule.hookUp();
		gModule.init();
		
		gModule.setPendingData(null); //clear pending data for memory reasons
	});
	
	gModule.resolveHelpers();
};
