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('nav');
	var navSub = navigation.getElementsByTagName('ul');
	
	//Go through all the Sub Nav's - give them a hidden class, inject in the toogle graphic
	for (i=0; i<navSub.length; i++){
		
		//Create the Image to inject in
		var toggleImage = document.createElement('img');
		toggleImage.setAttribute('src', 'images/contract.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";
					
	}
	
	
}

function toggleNav(whichOne){
	if (whichOne.getAttribute('id') == "expandAll") {
		var navigation = document.getElementById('nav');
		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', 'images/contract.gif')
		
		}
	}
	else if (whichOne.getAttribute('id') == "collapseAll"){
		var navigation = document.getElementById('nav');
		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', 'img/expand.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', 'images/contract.gif');
		}
		else {
			theParentULs[0].style.display = "none";
			theParentImage[0].setAttribute('src', 'images/expand.gif');
		}
	}
}

window.onload = init;
