// This code takes an e-mail address obfucated in the form:
// <em>name [at] address [dot] com</em>
// Code from Jeremy Keith - http://www.adactio.com/articles/1119/
function fixEmail()
{
	var emailEmsArray = new Array();
	var ems = document.getElementsByTagName("em");
	var j = 0;	
	for (var i = 0; i < ems.length; i++)
	{
		if (ems[i].firstChild && ems[i].firstChild.nodeValue.match( /\s+?\[at]\s+?/g ))
		{
			emailEmsArray[j++] = ems[i];
		}
	}

	for (var i = 0; i < emailEmsArray.length; i++)
	{
		var str = emailEmsArray[i].firstChild.nodeValue;
		str = str.replace(/\s+?\[(?:dot|period)]\s+?/g, '.');  // replaces all .
		str = str.replace(/\s+?\[(?:at)]\s+?/g, '@');          // replaces the @
		str = str.replace(/\s+?\[(?:dash|hyphen)]\s+?/g, '-'); // replaces all -
		var a = document.createElement("a");
		a.setAttribute("href", "mailto:" + str);
		a.appendChild(document.createTextNode(str));
		emailEmsArray[i].parentNode.replaceChild(a, emailEmsArray[i]);
	}
}

// See if the current page's URL contains an area names that have subareas.
function initializeNav()
{
	var currentUrl = document.location.href;

	if (currentUrl.indexOf("education") != -1)
	{
		showSubnav("education");
	}
	else if (currentUrl.indexOf("about") != -1)
	{
		showSubnav("about");
	}
	else if (currentUrl.indexOf("darfuraction") != -1)
	{
		showSubnav("darfuraction");
	}
	else if (currentUrl.indexOf("green") != -1)
	{
		showSubnav("green");
	}
	else
	{
		showSubnav("#");
	}
}

// Look at all the tr tags in the "subnav" class to see they're also in the classString class.
// If they are in the classString class, display them. If they're not, hide them.
function showSubnav(classString)
{
	var currentUrl = document.location.href;
	
	if (!document.getElementsByTagName) return false;

	var tagArray = document.getElementsByTagName("tr");
	
	for (var i = 0; i < tagArray.length; i++)
	{
		if (tagArray[i].className.indexOf("subnav") != -1)
		{
			if (tagArray[i].className.indexOf(classString) != -1)
			{
				tagArray[i].style.display = "block";
			}
			else
			{
				tagArray[i].style.display = "none";
			}
		}
	}
}

// This function highlights the current page in the navbar. First, it get all the links 
// inside the navigationTable id and looks through each. It gets the link string and removes 
// any leading "../" characters. Then it checks to see if the link string is in the current url.
// If it is, it sets the parent element's (tr) class to "navDown" which is the style for the 
// highlight.
function highlightCurrentNav()
{
	if (!document.getElementsByTagName || !document.getElementById || !document.getElementById("navigationTable")) return false;
	var nav = document.getElementById("navigationTable");
	var links = nav.getElementsByTagName("a");
	for (var i = 0; i < links.length; i++)
	{
		var urlString = links[i].getAttribute("href");
		var urlString = urlString.replace("../", "");
		var currenturl = document.location.href;
		if (currenturl.indexOf(urlString) != -1)
		{
			links[i].parentNode.className = "navDown";
			var linktext = links[i].lastChild.nodeValue.toLowerCase();
			document.body.setAttribute("class", linktext);
		}
	}
}

function addLoadListener(fn)
{
    if (typeof window.addEventListener != 'undefined')
    {
        window.addEventListener('load', fn, false);
    }
    else if (typeof document.addEventListener != 'undefined')
    {
        document.addEventListener('load', fn, false);
    }
    else if (typeof window.attachEvent != 'undefined')
    {
        window.attachEvent('onload', fn);
    }
    else
    {
        var oldfn = window.onload;
        if (typeof window.onload != 'function')
        {
            window.onload = fn;
        }
        else
        {
            window.onload = function()
            {
            	oldfn();
                fn();
            };
        }
    }
}
addLoadListener(initializeNav);
addLoadListener(highlightCurrentNav);
addLoadListener(fixEmail);

