(function (){
	function init(){
		//Check to see if we can use the DOM
		if(!document.getElementById) return;
		if(!document.getElementsByTagName) return;
		if(!document.createElement) return;
		
		//Get all the UL's in the Navigation
		var navigation = document.getElementById('prodNav');
		var navSub = navigation.getElementsByTagName('ul');

		//Go through all the Sub Nav's - give them a hidden class, inject in the toogle graphic
		for (var i=0; i<navSub.length; i++){
			
			//Create the Image to inject in
			var toggleImage = document.createElement('img');
			toggleImage.setAttribute('src', '/2008/listexpander/collapsed.gif');
			toggleImage.style.cursor = "pointer";
			toggleImage.onclick = function() {
				toggleNav(this);
			};
			
			//Get the Parent of the UL, and insert the Image before the first child
			navSub[i].parentNode.insertBefore(toggleImage, navSub[i].parentNode.firstChild);
			
			//Hide the Sub Navigation using a CSS Class and assign a class to the parent for styling
			navSub[i].style.display="none";
			navSub[i].parentNode.className = "expandable";
		}
		
		//var expandLink = document.createElement('li');
		//expandLink.innerHTML = "<a href='#' onclick='toggleNav(this)' id='expandAll'>Expand All</a>"
		
		//var collapseLink = document.createElement('li');
		//collapseLink.innerHTML = "<a href='#' onclick='toggleNav(this)' id='collapseAll'>Collapse All</a>"
			
		//Add them to the Bottom of the Navigation
		//navigation.appendChild(expandLink);
		//navigation.appendChild(collapseLink);
	
		var links = navigation.getElementsByTagName('a');
		for (i=0; i<links.length; i++){
			if(links[i].href==location.href){
				var parentNode=links[i].parentNode;
				while(parentNode){
					if(parentNode.className=='expandable'){	O=parentNode.getElementsByTagName('img');
						parentNode.getElementsByTagName('img')[0].onclick(parentNode.getElementsByTagName('img')[0]);	}
					parentNode=parentNode.parentNode;
				}
				break;
			}
		}
	}
	
	function toggleNav(whichOne){
		if (whichOne.getAttribute('id') == "expandAll") {
			var navigation = document.getElementById('prodNav');
			var navigationULs = navigation.getElementsByTagName('ul');
			var allImages = navigation.getElementsByTagName('img');
			for (i = 0; i < navigationULs.length; i++) {
					navigationULs[i].style.display = "block";
					allImages[i].setAttribute('src', '/2008/listexpander/expanded.gif')
			
			}
		}
		else if (whichOne.getAttribute('id') == "collapseAll"){
			var navigation = document.getElementById('prodNav');
			var navigationULs = navigation.getElementsByTagName('ul');
			var allImages = navigation.getElementsByTagName('img');
				for (i = 0; i < navigationULs.length; i++) {
					navigationULs[i].style.display = "none";
					allImages[i].setAttribute('src', '/2008/listexpander/collapsed.gif')
				}
		}
		else {
			var theParent = whichOne.parentNode;
			var theParentULs = theParent.getElementsByTagName('ul');
			var theParentImage = theParent.getElementsByTagName('img');
			
			//Grab just the first UL and the first toggle image so that sub-sub UL navs/image don't expand too
			if (theParentULs[0].style.display == "none") {
				theParentULs[0].style.display = "block";
				theParentImage[0].setAttribute('src', '/2008/listexpander/expanded.gif');
			}
			else {
				theParentULs[0].style.display = "none";
				theParentImage[0].setAttribute('src', '/2008/listexpander/collapsed.gif');
			}
		}
	}
	
	window.onload = init;
}())