/**********************************************************
Author:
Adam Barry
Klestrup | partners
www.klestrup-partners.dk

Date: October 16 2005

© 2007 Adam Barry, all rights reserved
-----------------------------------------------------------

Name:
e-mail script

-----------------------------------------------------------
Description:
Function that enables you not to display correct email
addresses in the markup.

-----------------------------------------------------------
Usage:
Simply place a link to the this script in the head-section
of the XHTML page. The script will then automatically
execute on page load.

<script type="text/javascript" src="e-mail.js"></script>

-----------------------------------------------------------
Example:
<script type="text/javascript" src="e-mail.js"></script>

<a href="user[at]domain.com" rel="email">send mail</a>

-----------------------------------------------------------
Dependencies:
This script depends on the windowOnLoad-script to execute

**********************************************************/

function emailLinks() {
	if (!document.getElementsByTagName) return;
 	var anchors = document.getElementsByTagName("a");

 	for (var i=0; i < anchors.length; i++) {
   		var anchor = anchors[i];

   		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "email" && anchor.getAttribute("href").indexOf("[at]") > 0) {
     		var href = anchor.getAttribute("href");
     		href = href.reverse();
			href = href.split("/");
			href = href[0].reverse();
     		href = href.replace("[at]","@");
     		anchor.setAttribute("href","mailto:"+href);
		}
 	}
}
addLoadEvent(function(){emailLinks();});


/*: Prototype
----------------------------------------------------------*/
String.prototype.reverse = function () {
	var s = "";
	var i = this.length;

	while (i>0) {
		s += this.substring(i-1,i);
		i--;
	}
    return s;
}
