var navTertiaryBehaviours = {

	// hide all divs on start
	'div.navTertiaryItem' : function(el){
			el.style.display = "none";
	},


	
	

	'#navTertiary li a' : function(el){

		el.onclick = function () {
			var selectedNode;

			//close all the divs
			//Behaviour.apply(navTertiaryBehaviours);
			var divArr = document.getElementsByClassName("navTertiaryItem");
			for(var j = 0; j < divArr.length; j++){ divArr[j].style.display = "none";}

			//finds the characters after the # in the href
			var s = String(el.href);
			if (s.indexOf("#") != -1) {
				var thisId = (s.substring(s.indexOf("#") + 1));
				var DivId = $(thisId);
				//sets the selected div to display:block
				DivId.style.display = "block";
			}
		

			// build collection of A elements within navTertiary
			var nodes = $A($("navTertiary").getElementsByTagName("a"));


			for(var i = 0; i < nodes.length; i++) {
				// if node is selected element, set index for later use
				if (nodes[i] == el) selectedNode = i;
				// set class name for node
				nodes[i].className = (i == selectedNode ? "selected" : (i == selectedNode + 1 ? "afterSelected" : ""));
				
				// if we're at end of collection, set parent LI class name
				if (i == nodes.length - 1) nodes[i].parentNode.className = (i == selectedNode ? "selected" : "last");
			}
			return false;
		},
		
		el.onfocus = function () {
			var selectedNode;

			//close all the divs
			//Behaviour.apply(navTertiaryBehaviours);
			var divArr = document.getElementsByClassName("navTertiaryItem");
			for(var j = 0; j < divArr.length; j++){ divArr[j].style.display = "none";}
			

			//finds the characters after the # in the href
			var s = String(el.href);
			if (s.indexOf("#") != -1) {
				var thisId = (s.substring(s.indexOf("#") + 1));
				var DivId = $(thisId);
				//sets the selected div to display:block
				DivId.style.display = "block";
			}
		

			// build collection of A elements within navTertiary
			var nodes = $A($("navTertiary").getElementsByTagName("a"));


			for(var i = 0; i < nodes.length; i++) {
				// if node is selected element, set index for later use
				if (nodes[i] == el) selectedNode = i;
				// set class name for node
				nodes[i].className = (i == selectedNode ? "selected" : (i == selectedNode + 1 ? "afterSelected" : ""));
				
				// if we're at end of collection, set parent LI class name
				if (i == nodes.length - 1) nodes[i].parentNode.className = (i == selectedNode ? "selected" : "last");
			}
			return false;
		}
		
	}


};	

	
Behaviour.register(navTertiaryBehaviours);


function clickFirst () {
	//find the first tertiary menu item, focus on it and then look away
	var nodes = $A($("navTertiary").getElementsByTagName("a"));

	var requestedUrl = window.location;
	var urlHashParts = String(requestedUrl).split("#");
	var navItemFound = false;

	if(urlHashParts.length > 1){
		// gets the hash value at the end of the requested url string
		var hashValue = urlHashParts[urlHashParts.length - 1];

		// regular expression to look for the hash value at the end of a string
		var reg = new RegExp("#" + hashValue + "$", "i");
		
		// loop through the array of navTertiary links and do a regular expression test to see if the href
		// of any of the links end with the requested hash value. If yes, highlight it and set navItemFound to true.
		for(var i = 0; i < nodes.length; i++){
			if(reg.test(nodes[i].href)){
				nodes[i].focus();
				nodes[i].blur();
				navItemFound = true;
				break;
			}
		}
		
		if(hashValue == "none") {
			navItemFound = true;
		}
	}

	// if no nav item have been highlight yet, highlight the default
	if(!navItemFound){
		nodes[0].focus();
		nodes[0].blur();
		scroll(0,0);
		
	}
}


addLoadEvent(clickFirst);
