/*
 * @file
 * This file holds a utility class that manages Event Management.
 */

(function() {
	// variable holds a hendle to the event manager
	var _em = (m2.util.hasToolkit() && MQA.EventUtil) ? MQA.EventUtil : {

		/**
		 * Function adds events handles in case the MQA.EventManager is not defined.  Should not be used.  
		 * Will log errors if this is used.
		 * 
		 * @private
	     * @param {HTMLElement} el      the element to bind the handler to
	     * @param {string}      eType   the type of event handler
	     * @param {function}    fn      the callback to invoke
		 */
		observe : function(el,eType,fn) {
			m2.util.addEventListener(el,eType,fn);
		},

		/**
		 * Function removes events handles in case the MQA.EventManager is not defined.  Should not be used.  
		 * Will log errors if this is used.
		 * 
		 * @param {Object} el
		 * @param {Object} eType
		 * @param {Object} fn
		 */
		stopObserving : function(el,eType,fn) {
			m2.util.removeEventListener(el,eType,fn);
		},

		/**
		 * Function will add a function to run when the window loads
		 * 
		 * @param {Object} fn
		 */
		addOnLoad : function(fn) {
			this.addListener(window,"load",fn);
		}
	};


	/**
	 * @namespace
	 * This namespace holds common utility functions, vars, and classes used to manage events on MapQuest.com
	 * The object overall just serves as a bridge to functions contained in the MapQuest Toolkit used to manage
	 * events.
	 */
	m2.util.Event = {

		/**
		 * 
		 * @param {HTMLElement} el
		 * @param {String} eventType
		 * @param {String} handler
		 * @param {Object} target
		 */
		add : function(el, eventType, handler, target) {
			return _em.observe(m2.$(el),eventType, handler,target);
		},
		
		/**
		 * 
		 * @param {HTMLElement} source
		 * @param {Object} eventType
		 * @param {Object} handler
		 * @param {Object} target
		 */
		remove : function(el, eventType,  handler, target) {
			return _em.stopObserving(m2.$(el),eventType, handler,target) ;
		}

	};

})();
 
