// ************************************
// 	Cho's JavaScript files
//	version: 1.0
//	last updated: 22/07/2009
//	
//	Contains general JavaScript functions in use across all pages
// ************************************

// ************************************
//	Global functions
// ************************************

function addOnClick(addToEl, fToAdd)
{
	// Adds an onClick JavaScript event to a selected element
	
	// Parameters
	// addToEl - The element to add the function to
	// fToAdd - The name of the function to add
	
	var bEl = document.getElementById(addToEl);
	var functEvent = new Function (fToAdd);

	if (bEl)
	{
		bEl.onclick = functEvent;
	}
}

function hideElement(elToHide, hideType)
{
	// Hides an element either completely or visually
	
	// Parameters
	// elToHide - The id of the element to hide
	// hideType - Accepts two values -visual- or -full-
	
	var dEl = document.getElementById(elToHide);
	
	if (dEl)
	{
		if (hideType == "full")
		{
			dEl.className = "hideFull";
		}else{
			dEl.className = "hideVisually";
		}
	}
}

function unhideElement(elToShow)
{
	// Removes any hide classes applied to an element
	// Note that this function will remove all classes from an element
	
	// Parameters
	// elToShow - The id of the element to show
	
	var dEl = document.getElementById(elToShow);
	
	if (dEl)
	{
		dEl.className = "";
		dEl.removeAttribute('class');
	}
}

// ************************************
//	Navigation functions
// ************************************

function removeCurrentLink()
{
	// Runs through the navigation section and removes the link for the current page
	
	var i, j, procEl, tstUrl, lastSlash, urlSearch, linkText, parEl;
	var navSect = document.getElementById('site-navigation');
	var navList = navSect.getElementsByTagName('li');
	var curUrl = document.location.href;
	
	// Pull just the page name from the url
	lastSlash = curUrl.lastIndexOf('/');
	urlSearch = curUrl.substring(lastSlash + 1, curUrl.length);
	
	// Loop through the navigation elements, and remove the href value for the current page
	if (navList)
	{
		for (i=0; i<navList.length; i++)
		{
			procEl = navList[i].getElementsByTagName('a');
			if (procEl)
			{
				for (j=0; j<procEl.length; j++)
				{
					tstUrl = procEl[j].getAttribute('href');
					if (tstUrl.toLowerCase() == urlSearch.toLowerCase())
					{
						// Found the current page - create the new next element instead of a link
						linkText = document.createTextNode(procEl[j].innerHTML);
						parEl = procEl[j].parentNode;
						parEl.className = "currentPage";
					
						// Replace the link child of the list item with the text
						parEl.replaceChild(linkText, procEl[j]);
					}
				}
			}
		}
	}
}


// ************************************
//	Initialise function
// ************************************

window.onload = function init()
{
	// Runs through the navigation section and removes the link for the current page
	
	
	// Do a browser check to make sure this stuff will work.
	if (!document.getElementById && !document.createElement && !document.createTextNode)
	{
		return;
	}
	
	// Call the function to remove the link to the current page from the navigation
	removeCurrentLink();
}
