/* **************************************
Sebo JavaScripts
Copyright (c) 2005 - 2010 Sebo Marketing, Inc. All Rights Reserved
************************************** */


/* **************************************
Function to stack up onload scripts
************************************** */

/*
This handy addLoadEvent function from Simon Willison allows you to stack up 'window.onload' events 
without them stepping on each other's toes. It's explained here - http://www.sitepoint.com/blog-post-view.php?id=171578
*/

function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}


/* **************************************
Encode Email
************************************** */

/*
All of the email addresses on the site are obfuscated using spans and [at] instead of @ to protect them from spam spiders.
		<span class="email"><span class="user">john.doe</span> [at] <span class="domain">anonymous.com</span></span>
	This function takes the obfuscated email addresses and rebuilds them as real mailto links
	The mailto links replace the spans for browsers with javascript
	The obfuscation is still useful for those who don't have javascript
*/


/* **************************************
Handle external links and popups
************************************** */
function windowLinks()
{
	if(!document.getElementsByTagName)
	{
		return;
	}

	var anchors = document.getElementsByTagName("a");
	for (var i = 0; i < anchors.length; i++)
	{
		var anchor = anchors[i];
		var relIndex = anchor.rel;
		if (relIndex)
		{
			// Split our REL value into parts 
			var relSplit = relIndex.split("|");
			
			// XHTML compliant target attribute 
			if (relSplit[0] == "external")
			{
				// Set the 'target' attribute to '_blank'
				anchor.target = "_blank";
				
				// Add a CSS class to it to allow us to style it
				if (anchor.className)
				{
					anchor.className += " external"; 
				}
				else
				{
					anchor.className = "external";
				}
				
				// Add a new title attribute to warn the users of a new window
				anchor.title = "Load in new window: "+ anchor.href;
			}
			
			// XHTML compliant popup attribute
			else if (relSplit[0] == "popup")
			{
				var strOptions="";
				
				// Attach a CSS class to it to allow us to style it
				anchor.className = "popup";
				
				// Add a new title attribute to warn the users of a new window
				anchor.title = "Loads in a Popup Window";
				
				// Modify the link to act as a popup
				anchor.popupWidth = relSplit[1]; 
				anchor.popupHeight = relSplit[2];
				anchor.onclick = function() {
					strOptions="resizable=yes, height="+this.popupHeight+", width="+this.popupWidth+", scrollbars=yes";
					window.open(this.href, '', strOptions); return false;
				};
			}
		}
	}
}


function emailEncode()
{
	// Build a list of spans and walk through them
	var spans = document.getElementsByTagName("span");
	for (var i=0; i < spans.length; i++)
	{
		span = spans[i];
		// If you find an email span
		if (span.className == "email")
		{
			var user = "";
			var domain = "";
			var emailText = "";
			var emailLink = "";
			
			// Walk through children nodes to find the user and domain
			for (var t=0; t < span.childNodes.length; t++)
			{
				var token = span.childNodes[t];
				if (token.className == "user")
				{
					user = token.firstChild.nodeValue;
				}
				if (token.className == "domain")
				{
					domain = token.firstChild.nodeValue;
				}
			}
			
			// If we have good values for the user and domain, build the new anchor
			if ((user != "") && (domain != ""))
			{
				emailText = document.createTextNode(user + "@" + domain);
				emailLink = "mailto:" + user + "@" + domain;
			}
			var anchor = document.createElement('a');
			anchor.href = emailLink;
			anchor.appendChild(emailText);
			
			// Replace the original email span with the new anchor
			span.parentNode.replaceChild(anchor, span);
			
			// Step the counter back because we removed some nodes.
			if ((i-3) < -1) i = -1; 	// Go to -1 because the loop is going to add one - we need to end up at zero, not 1, if we remove the first span.
			else i = i - 3;				// This function, unfortunately, assumes that the number of spans in the email is 3. Need a general solution
		}
	}
}


/* **************************************
Stack up the onload events
************************************** */
addLoadEvent(function() {
	windowLinks();
	emailEncode();
});

