// This looks for our specially encoded email addresses and replaces them with a simple mailto on the fly.
// The hope is that spambots run on the downloaded source, which doesn't contain the address, but a
// tenth of a second after the page finishes loading we will decode the email address so the user can click
// on it normally.
// The node should look like one of the following:
//		<a class="nospam" href="encoded_address">$$$$</a>
//		<a class="nospam" href="encoded_address">anything</a>
// The first form is where the visible text is the email address. The function changes both.
// The second form is where the visible text is not the email address. For instance, it could be "click here to email us".
// The encoded_address replaces the @ and the . with spaces. For instance, if this is encoded "me example com", then the address is "me@example.com".

/*global document, setTimeout */

YUI().use('node', function(Y) {
	var getElementsByClassName = function(cl) {
		var retnode = [];
		var myclass = new RegExp('\\b'+cl+'\\b');
		var elem = document.getElementsByTagName('*');
		for (var i = 0; i < elem.length; i++) {
			var classes = elem[i].className;
			if (myclass.test(classes)) retnode.push(elem[i]);
		}
		return retnode;
	};

 	setTimeout(function() {
		var links = getElementsByClassName("no_spam");
		for (var i = 0; i < links.length; i++) {
			var link = links[i];
			var raw_addr = link.href;
			var arrParts = raw_addr.split('/');
			var arr = arrParts[arrParts.length-1].split('SPAM');
			if (arr.length === 3)
			{
				var addr = arr[1] + '@' + arr[0] + '.' + arr[2];
				link.href = "mailto:" + addr;
				if (link.innerHTML === '$$$$')
					link.innerHTML = addr;
			}
		}
	}, 100);
 });

