// EventSelectors 
// Copyright (c) 2005-2006 Justin Palmer (http://encytemedia.com)
// Examples and documentation (http://encytemedia.com/event-selectors)
var EventSelectors = {
	version: '1.0_pre',
	cache: [],
	start: function(rules) {
		this.rules = rules || {};
		this._extendRules();
		this.assign(this.rules);
	},
	assign: function(rules) {
		this._unloadCache();
		rules._each(function(rule) {
			$A(rule.key.split(',')).each(function(selector) {
				var pair = selector.split(':');
				var event = pair[1];
				$$(pair[0]).each(function(element) {
					if(pair[1] == '' || pair.length == 1)
						return rule.value(element);
					this.cache.push([element, event, rule.value]);
					element.observe(event, rule.value);
				}.bind(this));
			}.bind(this));
		}.bind(this));
	},
	_unloadCache: function() {
		if (!this.cache) return;
		for (var i = 0; i < this.cache.length; i++) {
			Event.stopObserving.apply(this, this.cache[i]);
			this.cache[i][0] = null;
		}
		this.cache = [];
	},	
	_extendRules: function() {
		this.rules._each =function(iterator) {
			 for (key in this)
				 if(key != '_each')
				 	iterator({key: key, value: this[key]});
		};
	}
};
