/*
	gv.js - some general-purpose DOM routines used, among other things, to make a link
		out of "email" references.
		An anonymous object is created containing all functions and its activateEmails()
		routine is called at window.onload
	website - http://www.gold-vermilion.com
	designed - Paul Buller, info@gold-vermilion.com, August 2010
*/

window.onload = function() { 
({
	// Checks if the node has a class attribute containing the string className.
	// This will find eg. "obfusc" in such cases as <span class="annotated obfusc">
	hasClass: function(node, className)
	{
		if (node && node.className && className && node.className.indexOf(className) >= 0)
		{
			var nodeClasses = node.className.split(' ');
			for (var i = 0; i < nodeClasses.length; i++)
			{
				if (nodeClasses[i] === className) return true;
			}
		}
		return false;
	},

	// Removes the className from the node's class attribute, if the string is present.
	// This will modify eg. <span class="annotated obfusc"> to <span class="annotated">
	removeClass: function(node, className)
	{
		if (this.hasClass(node, className))
		{
			var newClassName = "";
			var nodeClasses = node.className.split(' ');
			for (var i = 0; i < nodeClasses.length; i++)
			{
				if (nodeClasses[i] !== className)
				{
					if (newClassName.length > 0) newClassName += ' ';
					newClassName += nodeClasses[i];
				}
			}
			node.className = newClassName;
		}
	},

	// Adds the className to the node's class attribute, testing if the string is already present.
	// This will modify eg. <span class="annotated"> to <span class="annotated obfusc">
	addClass: function(node, className)
	{
		if (!this.hasClass(node, className))
		{
			if (node.className && node.className.length > 0)
			{
				node.className += ' ' + className;
			}
			else
			{
				node.className = className;
			}
		}
	},

	// Removes the childNodes from the source node and appends them to the destination node.
	// This is equivalent to: nodeDest.innerHTML = nodeSrc.innerHTML;
	// when the destination node is initially empty. The difference is
	// that the childNodes are removed from the source rather than copied,
	// and that, unlike innerHTML, the function is guaranteed to work in application/xhtml+xml mode
	appendChildNodes: function(nodeDest, nodeSrc) 
	{
		if (nodeDest && nodeSrc)
		{
			while(nodeSrc.hasChildNodes())
			{
				nodeDest.appendChild(nodeSrc.firstChild);
			}
		}
	},

	makeEmailLink: function(emailNode)
	{
		if (document.createElement)
		{
			var a = document.createElement('a');
			a.href = 'mailto:info@gold-vermilion.com';
			a.title = 'eine Email verschicken';
			this.appendChildNodes(a, emailNode);
			emailNode.appendChild(a);
		}
	},

	activateEmails: function()
	{
		if (document.getElementsByTagName)
		{
			var all = document.getElementsByTagName('*');
			for (var i = 0; i < all.length; i++)
			{
				if (this.hasClass(all[i], 'email')) this.makeEmailLink(all[i]);
			}
		}
	}
}).activateEmails(); }

