String.prototype.trim = function() {
	return this.replace(/^\s*|\s*$/g, "");
}

/**
 * General utility functions
 */
var HTMLUtil = new function() {

	/**
	 * Add unlimited number of events to any element
	 */
	this.addEvent = function(obj, evType, fn) {
		if (obj.addEventListener) {
			obj.addEventListener(evType, fn, false);
			return true;
		} else if (obj.attachEvent) {
			var r = obj.attachEvent("on" + evType, fn);
			return r;
		} else {
			return false;
		}
	}
}

/**
 * DOM utils for HTML documents
 */
var HTMLDOMUtil = new function() {
	/**
 	* Dynamically add a class to an element
 	*
 	* element  - The element to add a class to
 	* classAdd - The class to add to the element
	*/
	this.addClass = function(element, classAdd) {
		if (element.className.indexOf(classAdd) >= 0)
			return;

		element.className += element.className ? ' ' + classAdd : classAdd;
	}

	/**
	 * Dynamically remove a class from an element
	 *
	 * element		- The element to remove a class from
	 * className	- The class to remove, if it exists
	 */
	this.removeClass = function(element, className) {
		if (element.className.indexOf(className) < 0)
			return;

		var replace = element.className.match(' ' + className) ? ' ' + className : className;
		element.className = element.className.replace(replace, '');
	}

	/**
	 * Get all elements by class name.
	 *
	 * className	- The class name to fetch elements by
	 * tagName		- Optional, the tag name of the elements returned
	 * rootNode		- Optional, the root node to look form
	 */
	this.getElementsByClassName = function(className, tagName, rootNode) {
		if (tagName == null) tagName = '*';
		if (rootNode == null) rootNode = document;

		var elements = rootNode.getElementsByTagName(tagName);
		var nodes = new Array();

		for (var i = 0; i < elements.length; i++)
			if (elements[i].className.indexOf(className) >= 0)
				nodes[nodes.length] = elements[i];

		return nodes;
	}

	/**
	 * Shorthand for creating DOM nodes. Allows for easily creating
	 * new nodes.
	 *
	 * tagName		   - The tag name of the new node
	 * className	   - Optional, the element class
	 * id			   - Optional, the element id
	 * content		   - Optional, content of the node. If a string it is set
	 *				     to the innerHTML property, else it's appended as a child
	 * extraAttributes - An array of attributes to add: new Array(attribute, value, attr, val, ...)
	 */
	this.createElement = function(tagName, className, id, content, extraAttributes) {
		className = className || null;
		id = id || null;
		content = content || null;
		var attr = extraAttributes || null;

		var el = document.createElement(tagName);

		if (className != null) el.className = className;
		if (id != null) el.id = id

		if (content != null)
			if (typeof(content) == 'string')
				el.innerHTML = content;
			else
				el.appendChild(content);

		if (attr != null)
			for (var i = 0; i < attr.length; i += 2)
				el[attr[i]] = attr[i+1];

		return el;
	}


     /** 
      * Check if an element exists, and remove it if it does 
      * 
      * id     - The id of the element 
      */ 
     this.removeIfExists = function(id) { 
          var el = document.getElementById(id); 
 
          if (el != null) 
               el.parentNode.removeChild(el); 
     }
}

HTMLUtil.addEvent(window, 'load', init_emails);

function init_emails() {
	if (!document.getElementById || !document.createElement)
		return false;

	var links = document.getElementsByTagName('a');

	for (var i = 0, link = null, email = null, em = null; (link = links[i]); i++) {
		if (link.rel == 'email') {
			em = link.innerHTML.trim().split(' a ');
			email = em[0] + '@' + em[1];
			link.href = 'mailto:' + email;
			link.innerHTML = email;
		}
	}
}