var Cathmhaol = window.Cathmhaol || {};

/**
* @library     Cathmhaol.Event
* @description Creates a user-defined event with a handler mechanism
* @author      Robert King
*/
Cathmhaol.Event = {
	/**
	* @method	Listens for a specific event on an object. When the event occurs, the function (fn) is executed
	* @returns	{void}
	* @param	{string|node} o
	* @param	{string} evt
	* @param	{function} fn
	* @param	{object} obj
	*/
	listen: function (o, evt, fn, obj) {
		var n = (typeof(o) == "string") ? document.getElementById(o) : o;
		if (!n || !n.nodeName) { return; }
		if (!fn || !fn.call) { return; }

		if (window.addEventListener) {
			n.addEventListener(evt, function(e) { fn.call((obj || n), e, obj); }, false);
		} else if (window.attachEvent) {
			n.attachEvent("on" + evt, function(e) { fn.call((obj || n), e, obj); });
		}
	},

	/**
	* @method	Returns the event that occurred.
	* @returns	{object}
	*/
	triggered: function(e) {
		var evt = e || window.event;
		if (!evt) {
			var c = this.get.caller;
			while (c) {
				evt = c.arguments[0];
				if (evt && Event == ev.constructor) {
					break;
				}
				c = c.caller;
			}
		}
		return evt;
	}
};
