/**************************************************
 *
 *	TextEmail2Link, turn text emails to usable links.
 *	It will suppress any space, replace "at" by "@" and suppress parenthesis.
 *	Call the script with "?tag=affected_tag&amp;class=affected_class" after its name.
 *
 *	Example
 *	Call: <script type="text/javascript" src="textemail2link.js?tag=em&amp;class=0okei"></script>
 *	Use: <em class="0okei"> m y    em a i l  at my d o mai n . co m (suppress spaces and replace "at" with "@")</em>
 *
 *	WARNING: DO NOT CHANGE THIS FILE'S NAME UNLESS YOU KNOW WHAT YOU DO.
 *
 *
 *	@author    Neck - http://www.eikylon.net
 *	@version   1.0.2
 *	@licence   available under the Creative Commons Attribution 3.0 Unported - http://creativecommons.org/licenses/by/3.0/
 *	           WITHOUT ANY WARRANTY, use it "at your own risks"
 *	@created   december 2007
 *	@updated   february 2008 - corrected an infinite while() with non matching tags in page
 *             december 2007 - corrected a bug when more than one email was in the page
 *
 **************************************************/
textEmail2Link = {
	/**
	 * The affected tag.
	 *
	 * @var string
	 */
	tag : null,
	/**
	 * The affected class.
	 *
	 * @var string
	 */
	class : null,

	/**
	 * Init the class.
	 *
	 */
	init: function() {
		if (!document.getElementById || !NK_framework.version) return;

		var get = NK_framework.pseudoGet('text_email_to_link');
		this.tag = get['tag'];
		this.class = get['class'];

		this.parseNode(document);
	},

	/**
	 * Look through the given node for all matching tags.
	 * You can call this methode if needed to re-parse something
	 *
	 * @param DOMNode theNode node to use
	 */
	parseNode: function(theNode) {
		if(!this.tag || !this.class || !theNode.getElementsByTagName) return;

		// get matching tags
		var e;
		var emailText;
		var emailLink;
		var emails = theNode.getElementsByTagName(this.tag);
		var i = 0;

		while((e = emails[i]) !== undefined) {
			// if matching
			if(e.className.toLowerCase().match(this.class)) {
				// clean the text
				emailText = e.firstChild.nodeValue;
				if(emailText == null) continue;
				emailText = emailText.replace(/ at /, '@').replace(/\(.*\)/, '').replace(/\s/g, '');

				// create the link
				emailLink = document.createElement('a');
				emailLink.setAttribute('href', 'mailto:'+emailText);
				emailLink.setAttribute('title', emailText);
				emailLink.setAttribute('class', this.class);

				// append it
				emailLink.appendChild(document.createTextNode(emailText));
				e.parentNode.replaceChild(emailLink, e);
			}
			// else forget it
			else {
				i++;
			}
		}
	}
}

// autoload
window.onload = function () {
	textEmail2Link.init();
}