(function() {

var SPEED_BUMP_URI = "speedbump.php";

/**
* Attach a click listener to the body after the document loads.
*/
var attachClickListener = function() {
	if(window.attachEvent) {
		document.body.attachEvent("onclick", linkClickHandler);
	}
	else if(window.addEventListener) {
		document.body.addEventListener("click", linkClickHandler, false);
	}
	else {
		document.body.onclick = linkClickHandler;
	}
};

/**
* If the item clicked is a link, let it pass through if it is a link
* on the site, but redirect to the "speed bump" page if it is an
* external link.
*/
var linkClickHandler = function(ev) {
	var target = ev.target || ev.srcElement;
	
	try {
	    if (target && 3 == target.nodeType) {
	        target =  target.parentNode;
	    }
	} 
	catch(e) {
	}
	
	while(target && target.tagName && target.tagName.toLowerCase() != "a") {
		target = target.parentNode;
	}
	
	if(!target) return;

	if(target.href && target.href.toLowerCase().indexOf(DOMAIN) == -1) {	
		// Cancel the link action and redirect to the speedbump page.
		if (ev.preventDefault) {
		    ev.preventDefault();
		} 
		else {
		    ev.returnValue = false;
		}
		
		var redirect = target.href;
		document.location.href = SPEED_BUMP_URI+"?_redirect="+encodeURIComponent(redirect);
	}
}

if(window.attachEvent) { // IE
	window.attachEvent("onload", attachClickListener);
}
else if(window.addEventListener) {
	window.addEventListener("load", attachClickListener, false);
}
else {
	window.onload = attachClickListener;
}


})();