function leftMenu_init(){
	// Search for items tagged with LeftMenu then search for sub-menus and hide them.
	// For each item with a sub-menu change it's url to an open menu action and duplicate it's url to the first item of it's submenu
	var uls = document.getElementsByTagName('ul');
	for(var u = 0; u < uls.length; u++){

		if(uls[u].className.search(/\bLeftMenu\b/) == -1)
			continue;

		var lis = uls[u].getElementsByTagName('li');
		for(var i = 0; i < lis.length; i++){

			// If this LI contains a UL, add the main adetails to the ul list
			var subULs = lis[i].getElementsByTagName('ul');
			if(subULs.length > 0){
			
				//// lis[i].style.border = '1px solid #ff0000;';
				// Get the details
				var aList = lis[i].getElementsByTagName('a');
//-//			var href = aList[0].href;
//-//			var linkText = aList[0].innerHTML;
			
				// Get the first li for insertbefore
//-//			var subLi = subULs[0].getElementsByTagName('li');
	
				// Add to the first UL as the first item
//-//			var myLi = document.createElement('li');
//-//			var myLink = document.createElement('a');
//-//			myLink.setAttribute('href',href);
				//myLink.appendChild(document.createTextNode(linkText));
//-//			myLink.innerHTML = linkText;
//-//			myLi.appendChild(myLink);

				//subULs[0].appendChild(myLi);
//-//			subULs[0].insertBefore(myLi, subLi[0]);

				// Remove the HREF on the link and add an event handler
//-//			aList[0].setAttribute('href','javascript:void(0);');
//-//			addEvent(aList[0], 'click', leftMenu_toggleSubmenu, false);
				
				addClass(aList[0], 'subheader');
				
				subULs[0].style.display = "none";
				
			}

		}
	}	

	// Do the highlighting

////// We don't need this block but it may be usefull later. This removes the protocol/domain off the url
////// Get current page
///	var curPage = document.location.href;
///	myParts = new Array();
///	myParts = curPage.split("/");
///	var myNewParts = new Array();
///	// Not going to use pop as not fully supported.
///	for(i = 0;i < myParts.length;i++){
///		if(i>2)	{
///		myNewParts[myNewParts.length] = myParts[i];
///		}
///	}
///	var curPath = "/" + myNewParts.join("/");
/// END bit we may need later.


	// Loop through the items and see if any of them are == curPath
	// Also, any selected items open their submenus (if they have them)
	// We are doing this here after the first loop as the first loop may have made changes to the structure.
	
	for(var u = 0; u < uls.length; u++){
		if(uls[u].className.search(/\bLeftMenu\b/) == -1)
			continue;

		var lis = uls[u].getElementsByTagName('li');
		for(var i = 0; i < lis.length; i++){

			var aList = lis[i].getElementsByTagName('a');
			if(aList[0].href == document.location.href){
				addClass(aList[0], 'Selected');
				aList[0].style.color = "#FF6702";
	
				// Open the parents by looping back to root (well back to the LeftMenu ul
				keepLooping = true;
				myElement = lis[i];
				while(keepLooping){
					myElement = myElement.parentNode;
	
					if(myElement.firstChild.className.search(/\bsubheader\b/) != -1){
						tog_uls = myElement.getElementsByTagName('ul');
						tog_uls[0].style.display = "block";
						addClass(myElement.firstChild, 'active');
					}
	
					if(myElement.nodeName.toLowerCase() == "ul"){
						if(myElement.className.search(/\bLeftMenu\b/) != -1){
							keepLooping = false;
							break;
						}
					}
				} // end loop back to open menus

				// Open the FIRST child
				var subUls = lis[i].getElementsByTagName('ul');
				if(subUls.length >  0){
					subUls[0].style.display = "block";
					addClass(lis[i].firstChild, 'active');
					//addClass(subUls[0])
				}

			}

		}
	}	
	
} // end leftMenu_init

addEvent(window, 'load', leftMenu_init, false);
addEvent(window, 'unload', EventCache.flush, false);

function leftMenu_toggleSubmenu(e){
	if(!e) var e = window.event;
	if(e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;

	//alert(targ.href);
	uls = targ.parentNode.getElementsByTagName('ul');
	if(uls.length > 0){

		if(uls[0].style.display == "block"){
			uls[0].style.display = "none";
			removeClass(targ, 'active');
		}else{

			// Close all then open this one.
			leftMenu_closeAll();

			uls[0].style.display = "block";
			addClass(targ, 'active');
		}
	}
} // end leftMenu_toggleSubmenu

function leftMenu_closeAll(){

	var uls = document.getElementsByTagName('ul');
	for(var u = 0; u < uls.length; u++){
		if(uls[u].className.search(/\bLeftMenu\b/) == -1)
		continue;

		var lis = uls[u].getElementsByTagName('li');
		for(var i = 0; i < lis.length; i++){
		
			var subULs = lis[i].getElementsByTagName('ul');
			if(subULs.length > 0){
				removeClass(lis[i].firstChild, 'active');
				subULs[0].style.display = "none";
				
			}
		}
	}
} // end leftMenu_closeAll

function addClass(target, classValue){
	var pattern = new RegExp("(^| )" + classValue + "( |$)");
	if(!pattern.test(target.className)){
		if(target.className == ""){
		target.className = classValue;
		}else{
		target.className += " " + classValue;
		}
	}
	return true;
} // end addClass

function removeClass(target, classValue){
	var removedClass = target.className;
	var pattern = new RegExp("(^| )" + classValue + "( |$)");
	removedClass = removedClass.replace(pattern, "$1");
	removedClass = removedClass.replace(/ $/, "");
	target.className = removedClass;
	return true;
} // end removeClass