/*
Rollovers for navigation
Done by changing the classnames of elements.
Next sibling element needs to be changed too as we need to change the background image of that
Marky Wong - 25th Feb 2006
*/

addLoadEvent(setUpNav);

//
//
function setUpNav(){

	// get out browser doesn't support these DOM methods
	if(!document.getElementById || !document.getElementsByTagName){
		return;
	}

	// get all the navigation links 
	var nav = document.getElementById('nav-primary');
	var links = nav.getElementsByTagName('a');
	
	var num_links = links.length;
	
	// add onmouseovers and onmouseouts
	for (i=0; i<num_links; i++) {
		link = links[i];
		//link.onmouseover = doMouseOver;
		//link.onmouseout = doMouseOut;
		links[i].onmouseover = function() {doNavRollovers(this, 'active', 'active-neighbour')};
		links[i].onmouseout = function() {doNavRollovers(this, '', '')};
	
	}

}



//
//
function doNavRollovers(el, activeClass, neighbourClass) {

	// set the parent element <li> class to activeClass
	var li = el.parentNode;
	changeClassName(li, activeClass);		
		
	
	// have to change the next sibling element classname
	// Extra IE handler code as IE doesn't recognise whitespace as a valid next sibling element
	// Whilst other browsers do
	if(li.nextSibling){
	
		var nextLi = getNextElement(li.nextSibling);		
		
		if(nextLi == null){
			// no sibling 
			// we've got last li element as we've returned null for nextLi
			// get the first <span> we encounter and change it's class to activeClass
			changeClassName(li.getElementsByTagName('span')[0], activeClass)
		}
		else {
			// we've got last li element
			// get the first <span> we encounter and change it's class to activeClass
			changeClassName(getNextElement(li.nextSibling), neighbourClass)
		}		
				
	} else {
		
		// no sibling in IE
		// we've got last li element as we've returned null for nextLi
		// get the first <span> we encounter and change it's class to activeClass
		changeClassName(li.getElementsByTagName('span')[0], activeClass);
	}
}



function changeClassName(el, name) {
	el.className = name;
}
