/*
* Highlight-Nofollow.js
* Highlights all text links that have a rel=nofollow attribute set
* - underlines the link in orange/dashed
* - adds a warning image
* - adds a tooltip to the warning image and links to an info-page
*
* Tested on IE 6.x, IE 7.0, FF 2.0.2, Opera 9.01
*
* Created by John Mueller, http://johnmu.com/
* Version 1.0 - 2007-07-28
* 
* Released into public domain
*
* Requires: nofollow.gif + adjusted settings below
*/

// settings, modify as desired
var myInfoPage ="http://#";				// local page with nofollow information
var myNofollowImg = "nofollow.gif";		// source of nofollow image (check for full path!)
var myWarnMsg = "Beware: this link is potentially bad or not trusted! Click here to find out more."; 
										// warning must be html-formatted
// code used to wrap nofollows
// may also be modified. 
// Be careful to make sure that the code is valid - you cannot check it with a
// html validator once the page is live.
var nofollowPrefix = "<span class='nofollow' style='border-bottom: dashed 2px orange;'>";
nofollowPrefix += "<a href='" + myInfoPage + "' class='nofollowimglink' title='" + myWarnMsg + "' style='text-decoration: none;'>";
nofollowPrefix += "<img src='"+myNofollowImg+"' class='nofollowimg' alt='" + myWarnMsg + "' style='border:0; margin-right: 2px;' />";
nofollowPrefix += "</a>";
var nofollowSuffix = "</span>";


// the actual code
// unless you know what you are doing, don't modify anything below here :-)

var isIE = (window.navigator.userAgent.indexOf("MSIE") > 0);

// pseudo compatibility layer 
if (!isIE) {			// putter for "outerHTML"
	function setHtml(mNode, sHTML) {
		var r=mNode.ownerDocument.createRange();
		r.setStartBefore(mNode);
		var df=r.createContextualFragment(sHTML);
		mNode.parentNode.replaceChild(df,mNode);
		return sHTML;
	}
} else {	   		 	// already available on IE
	function setHtml(mNode, sHTML) {
		mNode.outerHTML = sHTML;
	}
}

function getHtmlA(mNode, newRel) { // getter for "outerHTML" for links only
	var attr;
	var attrs=mNode.attributes;
	var str="<"+mNode.tagName;
	for(var i=0;i<attrs.length;i++){
		attr=attrs[i];
		if(attr.specified) {
			if (attr.name.toLowerCase() == "rel") {
				if (newRel.my_trim()!="") 
					str+=" "+attr.name+'="'+newRel+'"';
			} else 	str+=" "+attr.name+'="'+attr.value+'"';
		}
	}
	return str+">"+mNode.innerHTML+"";
}

if (!isIE) {						  // getter for "innerText"
	function getInnerText(mNode) {
		var r=mNode.ownerDocument.createRange();
		r.selectNodeContents(mNode);
		return r.toString();
	} 
} else {
	function getInnerText(mNode) {	// already available on IE
		return mNode.innerText;
	}
}

// just in case this javascript doesn't have trim()
String.prototype.my_trim =      function() {
  return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""));
}

// main banana
function markNofollow() {
	// get all links in the current document
	var allLinks = document.getElementsByTagName('a');
    var i;
	for (i=0; i<allLinks.length;i++) {				  		   // check each link
		var isNofollow = false;
		var rels = allLinks.item(i).getAttribute("rel");			// get "rel" attributes
		if (rels) {												// .. if any
			var rela = rels.toLowerCase().split(" ");				// split into space terminated list
			var j;
			for (j=0;(j<rela.length) && (!isNofollow);j++)		// check for 'nofollow'
				if (rela[j]=="nofollow") { isNofollow=true; rela[j]=" "; }
			
			if (isNofollow) {									// got a nofollow
				if (getInnerText(allLinks.item(i)).my_trim()!="") {	// check to make sure it's a text link
					var newrel = rela.join();					// rejoin into one value, sauf le nofollow
					var newhtml;								// create replacement for nofollowed text link
					newhtml = nofollowPrefix;
					newhtml += getHtmlA(allLinks.item(i), newrel);
					newhtml += nofollowSuffix;
					setHtml( allLinks.item(i), newhtml);		// replace existing code
				}
			} // if (isNofollow)
		} // if (rels)
	} // for ...
}

markNofollow();		  // actually do it now.
