/**
 * ClickHandler
 * This is a generic click-handler that inspects clicks on links
 *
 * @requires Event The event class as defined by prototype js framework
 * v2.0.090302
 * author: Martin Reurings
 */
var ClickHandler = {
	aRE:/^a$/i,
	hRE:/^(h[0-9])|(fieldset)|(th)$/i,
	buttonRE:/\bbutton\b/i,
	classVars:function(cn) {
		var re = /\b\w+:\w+\b/gi;
		var r = null;
		var results = new Array();
		while (r = re.exec(cn)) {
			var v = r[0].split(":");
			results[v[0]] = v[1];
		}
		return results;
	},
	/**
	 * Registration of the ClickHandler object with the document. All it's event-handlers
	 * will be cleaned up internally upon leaving the page.
	 */
	init:document.observe("dom:loaded", function() {
		//Initialize
		ClickHandler.onclick = document.observe("click", function(e) {
			ClickHandler.clickHandler(e);
		});
	}),
	/**
	 * A 'hashmap' containing the methods link to certain rel-values.
	 * If a rel-value is found within this hashmap it's method will
	 * be called with the link supplied as an argument.
	 */
	handlers:{
		popup:function(target) {
			var vars = ClickHandler.classVars(target.className);
			var options = "resizable=no,toolbar=no,statusbar=no,scrollbars=yes,width=" + vars.width + ",height=" + vars.height;
			var win = window.open(target.href+((vars.id) ? "/"+vars.id : '')+((vars.type) ? "/"+vars.type : ''), vars.name?vars.name:"", options);
			return true;
		},
		blank:function(target) {
			var vars = ClickHandler.classVars(target.className);
			var options = "";
			var win = window.open(target.href, vars.name?vars.name:"", options);
			return true;
		}
	},
	/**
	 * This method is the clickhandler logic which will determine whether or not
	 * to act upon a click-event which is passed to it.
	 */
	clickHandler:function(e){
		var e = e||event;
		var target = e.target||e.srcElement;
		//In case an element is clicked that might have a link in it, or an element inside a link...
		if (!this.aRE.test(target.nodeName)) {
			if (this.hRE.test(target.nodeName) || this.buttonRE.test(target.className)) {
				if (target.getElementsByTagName("a").length > 0)
					target = target.getElementsByTagName("a")[0];
				else
					return true;
			} else {
				target = target.parentNode;
			}
		}
		//Check if a link is clicked and rel is known
		if (this.aRE.test(target.nodeName) && target.rel && !target.dblFire) {
			target.dblFire = true; //Some browser had a double-fire bug, event fired twice
			window.setTimeout(function() {target.dblFire = false;}, 0); //These two lines are a work-around to prevent this.
			if (this.handlers[target.rel]) {
				if (this.handlers[target.rel](target)) {
					return Event.stop(e);
				} else
					return true;
			} else
				return true;
		} else if (target.nodeName == "MAP"){
			//TODO: Check if there is a better way to implement this...
			if (target.getElementsByTagName("area").length > 0) {
				target = target.getElementsByTagName("area")[0];
				var rel = target.getAttribute("rel");
				target.dblFire = true;
				window.setTimeout(function() {target.dblFire = false;}, 0);
				if (this.handlers[rel] && this.handlers[rel](target))
					return Event.stop(e);
				else
					return true;
			}
		}
	},
	/**
	 * Using this method you can add handlers to the ClickHandler.
	 * The ident identifier should be a string matching the rel-attribute
	 * of the links you want to target. The method should be a function
	 * taking one parameter, the link, and return false when the normal
	 * event-flow should be followed or true when you need to cancel
	 * the normal flow of events.
	 */
	addHandler:function(ident, method) {
		this.handlers[ident] = method;
	}
};

(document.body||document.documentElement).className += " click_ENABLED";
