	var nav_id = 'nav';	
	
/*
	init()
	Called on DOM Ready, opens Tree to current node
*/	
	function init() {
		
		// Hide UL
		YAHOO.util.Dom.getElementsBy(recieve, 'ul', nav_id, hideUL);

		// Check if we need to open Tree to the Current page
		// Current page is determined by a class of "current" on the link
		var currentLink = YAHOO.util.Dom.getElementsByClassName('current', 'a', nav_id, openToCurrent);

	}
	
	YAHOO.util.Event.onDOMReady(init);
	
	
	YAHOO.util.Event.onAvailable(nav_id, function(){
			
		YAHOO.util.Event.addListener(this, 'click', toggle);
		
		function toggle(e) {
		
			var target = YAHOO.util.Event.getTarget(e);

			var n = target.parentNode.getElementsByTagName('ul');
			
			if (n[0] && target.nodeName.toLowerCase() === 'a') {
				
				var url = target.href;
				// If we want the link text to expand node
				if (url === 'javascript:void(0);') {
					n[0].style.display = n[0].style.display == 'block' ? 'none' : 'block';
					target.parentNode.className = n[0].style.display == 'block' ? 'open' : 'parent';
					YAHOO.util.Event.preventDefault(e);
				}
				
			}
								
		}
		
	})
	

	function recieve(e) {
		return true;
	}


/*
	hideUL()
	Hides/collapses all UL elements
	PARAMS: e <HTMLElement>
*/	
	function hideUL(e) {
		YAHOO.util.Dom.setStyle(e, 'display', 'none'); 
	}


/*
	openToCurrent()
	Handles looping through parent nodes to open them. The <a> that has the class="current" is handed in
	PARAMS: e <HTMLElement>
*/	
	function openToCurrent(e) {
		
		var parentLI = YAHOO.util.Dom.getAncestorBy(e);
		
		setSelfOpen(e);

		var newParentLI = setParentOpen(parentLI);

		// Loop through all parent LI and UL and set styles to open
		do {
			newParentLI = setParentOpen(newParentLI);
			// Once we have reached the root we exit loop
			if (YAHOO.util.Dom.getAncestorBy(newParentLI).id == nav_id) {
				newParentLI = false;
			}
			//alert(newParent);
		} while (newParentLI)	
		
	}



/*
	setSelfOpen()
	Opens self node if it has childen.
	PARAMS: e <HTMLElement>
*/	
	function setSelfOpen(e) {
		var nextSibling = YAHOO.util.Dom.getNextSibling(e);

		if (nextSibling) {
			if (nextSibling.nodeName.toLowerCase() === 'ul' ) {
				YAHOO.util.Dom.setStyle(nextSibling, 'display', 'block'); 
			}
		}
	};
	
	
/*
	setParentOpen()
	Opens parent node. Sets parentLI class to "open" and sets parentLI's parent UL to display:block.
	PARAMS: parentLI <HTMLElement>
	RETURNS: Next parent LI of parentLI <HTMLElement>
*/	
	function setParentOpen(parentLI) {
		if (parentLI) {
			YAHOO.util.Dom.replaceClass(parentLI, ' ', 'open'); 
			var parentUL = YAHOO.util.Dom.getAncestorBy(parentLI);
			YAHOO.util.Dom.setStyle(parentUL, 'display', 'block'); 
			return YAHOO.util.Dom.getAncestorBy(parentUL);	
		}else{
			return false;
		}
	}
	
	

