/*
Copyright (c) 2007, Caridy Patiņo. All rights reserved.
Portions Copyright (c) 2007, Yahoo!, Inc. All rights reserved.
Code licensed under the BSD License:
http://www.bubbling-library.com/eng/licence
version: 1.3.2
*/
(function() {

    var $B = YAHOO.CMS.Bubble,
		$E = YAHOO.util.Event,
		$L = YAHOO.lang,
	    $D = YAHOO.util.Dom;

	/**
	* @singleton Selector - The Selector Manager will monitoring the areas using the className value and will highlighting the "yui-cms-item" areas on mouseover.
	* Apply visual enhanced to an area
	* @constructor
	*/
	YAHOO.util.Selector = function() {
		var obj = {},
			_areas = {},
			_listClass = 'yui-cms-selector',
			_itemClass = 'yui-cms-item',
			_selector = 'selected',
			_defConf   = {
							persistent: false, // true if you want to keep an item selected even when the mouse itīs out of the area
							onReset: null,
							onSelect: null
						 };

		function reset (area, conf) {
			var resetItem = function ( ref ) {
				$D.removeClass(ref, _selector);
			};
			var items = $D.getElementsByClassName(_itemClass,'*',area);
			if (items.length > 0) {
			  $D.batch (items, resetItem, obj, true);
			}
			if ($L.isFunction(conf.onReset)) {
				conf.onReset.apply ( conf, [area] );
			}
		}
		// pasive behavior...
		$B.on('rollover', function (layer, args) {
		  var area = null, item = null, onWayOut;
		  area = $B.getOwnerByClassName( args[1].target, _listClass );
		  if (area) {
			for (item in _areas) {
				if ((_areas.hasOwnProperty(item)) && $D.hasClass(area, item)) { // match with a certain area
			        var c = _areas[item];
					item = $B.getOwnerByClassName( args[1].target, _itemClass );
					if (!$D.hasClass(area, _selector)) {
						$D.addClass(area, _selector);
						onWayOut = function (e) {
						    if (!$B.virtualTarget(e, area)) {
						      reset(area, c);
						    }
						};
						if (!c.persistent) {
						    $E.removeListener ( area, 'mouseout', onWayOut );
							$E.addListener ( area, 'mouseout', onWayOut, obj, true );
						}
					}
					if (item && !$D.hasClass(item, _selector)) {
						// is over a new item...
						reset (area, c);
						$D.addClass(item, _selector);
						if ($L.isFunction(c.onSelect)) {
							c.onSelect.apply ( c, [item, area] );
						}
					}
				}
			}
		  }
	    });

		// public vars
		// public methods
		/**
		* * add new area to the monitoring list...
		* @public
		* @param {string} className
		* @return void
		*/
		obj.add = function ( className, conf ) {
			if ($L.isString(className) && (className !== '')) {
			  obj.remove(className);
			  _areas[className] = conf || _defConf;
			}
		};
		/**
		* * Remove an area from the monitoring list...
		* @public
		* @param {object} id	className
		* @return void
		*/
		obj.remove = function ( className ) {
			if ($L.isString(className) && (className !== '') && (_areas[className])) {
				_areas[className] = null; // discarding the area...
			}
			_areas[className] = null;
		};
		return obj;
	}();
})();
YAHOO.register("selector", YAHOO.util.Selector, {version: "1.4.0", build: "206"});
