/**
 *	ElementListener
 *	---------------------------------------------
 *	(c) Lost Boys - http://www.lostboys.nl
 *	Requires:
 *		EventListener.js
 */


function ElementListener(nodeName, attribute, eventType) {
	this.nodeName = nodeName;
    this.attribute = attribute;
    this.relations = [];
	EventListener.addEvent(document, eventType, this.handleEvent, this);
}

ElementListener.prototype = {
	handleEvent:function(e) {
		var target = EventListener.getTarget(e, this.nodeName);
        var attribute = target? target[this.attribute] : null;
		var relation = target? this.getHandler(target, attribute) : null;
		if(relation && relation.handler.apply(relation.scope, [target, attribute])) {
			EventListener.preventDefault(e);
		}
	},

	getHandler:function(target, attribute) {
		for (var rel,i=0; i<this.relations.length; i++) {
			rel = this.relations[i];
			if(rel.expression.test(attribute)) {
				return rel;
			}
		}
	},

	register:function(expression, handler, scope) {
		this.relations.push({
			expression:expression,
			handler:handler,
			scope:(scope || window)
		});
	}
}

/**
 *	LinkListener
 *	--------------------------
 */

var LinkListener = new ElementListener('a', 'rel', 'click');

// var InputListener = new ElementListener('input', 'className', 'click');
// var SelectListener = new ElementListener('select', 'className', 'change');