
/**
 * simple observer pattern class
 */


var events = {


	/**
	 * list of events and callbacks
	 */

	eventList: {},


	/**
	 * add $callback to the list of $name events
	 *
	 * @param	string   name       the name of the event
	 * @param	function callback   the function
	 *
	 * @return	nothing
	 */

	attach: function(name, callback) {
		if (this.eventList[name] === undefined) {
			this.eventList[name] = [];
		}
		this.eventList[name].push(callback);
	},


	/**
	 * notify observers of $name event
     *
	 * @param string name    the name of the event to be triggered
	 * @param array params   any extra useful information throwned by the event caller
	 *
	 * note: a false value returned from any of the observers will stop further notifications
	 *
	 * @return	false or nothing
	 */

	fire: function(name, params) {
		if (this.eventList[name] === undefined) {
			return;
		}

		//if one of the listeners returns false
		//call off the rest and return false as well
		var returned = null;
		for (var i in this.eventList[name]) {
			returned = this.eventList[name][i](params);
			if (returned === false) {
				//pass the false value along
				return false;
			}
		}
	}
};

