// Functionalities based on anchor relationsships,
// like external links
// by Lost Boys - www.lostboys.nl

RelationalLinks = function() {
	var self = this;
	var application = /application/i;
	var external = /external|enclosure/;
	
	var defaultIcon = "static/images/icons/external.gif";
	var applicationIcon = "static/images/icons/external-app.gif";
	
	var anchors = document.getElementsByTagName("a");
	var node, anchor, threshold, icon, src;
	var dutch = /nl/i.test(getLanguage());
	
	for(var i = 0; i < anchors.length; i++) {
		node = anchor = anchors[i];
		threshold = 10;
		if(anchor.rel && external.test(anchor.rel)) {
			icon = document.createElement("img");
			src = defaultIcon;
			
			while(node && threshold--) {
				if(node.className && application.test(node.className)) {
					src = applicationIcon;
					break;
				}
				node = node.parentNode;
			}

			icon.src = src;
			
			icon.className = "icon";
			icon.alt = dutch? " (Opent in nieuw browservenster)" : " (Opens a new browser window)" ;
			icon.width = 22;
			icon.height = 15;
			anchor.appendChild(icon);
		}
	}	
	// set event
	EventListener.addEvent(document, 'click', function(e) {
		return self.checkForRelation(e);				
	});
}

RelationalLinks.prototype.checkForRelation = function(e) {
	// cancel further action when modifier keys are pressed or click is not a left click
	if(e.shiftKey || e.altKey || e.ctrlKey || (e.metaKey && e.metaKey != "undefined") || (e.button != 0 && e.button != 1)) return true;
	var target = EventListener.getTarget(e, 'a');
	var self = this;
	if(target && target.rel) {
		var relation = target.rel;
		if(/external|enclosure/i.test(relation)) {
			this.doExternal(target);
			return EventListener.cancelEvent(e);
		}
	}
	return true;	
}

RelationalLinks.prototype.doExternal = function(target) {
	var properties = "";
	var w = window.open(target.href, "", properties);
}

EventListener.addEvent(window, "load", function() {
	window.relationalLinks = new RelationalLinks();
});