
/*
 * function:	removeSelfLinks
 * description: Removes all links referring to the current page.
 *				Tags inside the link kann implement an 
 *				onRemoveLink handler to be notified if a is removed.
 *				In addition, an onMatchLink handler of the link
 *              and its children is called, for links that are a 
 *              true substring of the current pages URI.
 * parameters:	-
 * attention:	URL comparision is done by misusing an Image object.
 *				This may result in unnecessary loads of linked pages.
 */
function removeSelfLinks()
{
	var aAllLinks	= document.getElementsByTagName("a");

	var	oTester	= new Image();
	for (var i=0; i<aAllLinks.length; i++) {
		var	oA	= aAllLinks[i];
		//	Dirty trick to convert relative URLs
		oTester.src	=  oA.getAttribute('href');	

		//	Is the current link a self link
		if (oTester.src == document.URL) {			
			var	oParent	= oA.parentNode;

			//	Move the child nodes before the link tag
			while (oA.hasChildNodes()) {
				var	oFirstChild	= oA.firstChild;
				//	On firefox, the remove is done
				//	implicitely by insertBefore.
				oA.removeChild(oFirstChild);
				oParent.insertBefore(oFirstChild, oA);
				
				//	call onRemoveLink
				callHandler(oFirstChild, 'onRemoveLink');
			}
			
			var	aLen	= aAllLinks.length;
			//	remove link tag
			oParent.removeChild(oA);
			//	if aAllLinks was shortened (eg. Firefox)
			i	-= aLen-aAllLinks.length;
		}
		else if (	oTester.src 
				== 	document.URL.substr(0, oTester.src.length)) {
			var	aCN	= oA.childNodes;
			// call onMatchLink for oA and all child nodes	
			callHandler(oA, 'onMatchLink');
			for (var j=0; j<aCN.length; j++) {
				callHandler(aCN[j], 'onMatchLink');
			}
		}	
	}
}

/*
 * function:	callHandler
 * description: calls a nodes event handler
 * parameters:	oNode - the node
 *				sHandlerName - the handlers name
 *
 */
 
 function callHandler(oNode, sHandlerName) {
 	//	check if the handler exists
	if (	oNode.getAttribute 
		&&	oNode.getAttribute(sHandlerName)) {	
		//	onRemoveLink event	
		var fHandler	= new Function(
								oNode.getAttribute(sHandlerName));
		fHandler.apply(oNode);
	}
}

