(function() { 

	/**
	 * List of widgets accessed by name.
	 * @private
	 * @type Object
	 */
	var _widgets = {},

	/**
	 * Counter incrementing to create unique widget id's
	 * @private
	 * @type Int
	 */
	_idCounter = 0,

    /**
     * Returns an ID and applies it to the element "el", if provided.
     * @private
     * @return {String} The generated ID
     */
    _generateId = function() {
		return "widget_" + _idCounter++;
    }

	/**
	 * @namespace
	 * Namespace holds all the javascript based widgets that are used on the MapQuest site.
	 */
	m2.widget = {
		
		/**
		 * Adds a widget to the private widgets array
		 * @param  {String} name     (optional) What we want to name the widget.  If the name is not provided a
		 *                           unique id will be created for the widget.
		 * @param  {Object} widget   The widget could be an actual m2.widget or event just an object representing
		 *                           data that is necessary to operate a widget.
		 * @return {String}          the id of the stored widget
		 */
		add : function(name, widget) {
			name = name || _generateId();
			_widgets[name] = widget;
			return name;
		},

		/**
		 * Gets a widget with the specified name
		 * @param {String} name
		 */		
		get : function(name) {
			return _widgets[name];
		},
		
		/**
		 * Deletes a widget with the specified name
		 * @param {String} name
		 */
		remove : function(name) {
			delete _widgets[name];
		},
		
		/**
		 * Unloads all the widgets
		 */
		unload : function() {
			for (var i in _widgets) {
				this.remove(i)
			}
		}
	};

})();

