
var menuID='navExpMenu';
registerInitFunc(initExpandingMenu);

function initExpandingMenu()
{
	var nav=document.getElementById(menuID);	//denotes the navigation element
	
	if(nav != null)
	{
	    var lis = nav.getElementsByTagName('li');
	    var uls = nav.getElementsByTagName('ul');
	    var aTags = nav.getElementsByTagName('a');
	    // if DOM is not available stop right here.
	    if(!document.getElementById && !document.createTextNode){return;}
	    //toggle(lis[0]);
	    //lis[1].className = "hiddenChild";
    	
	    nameATags(aTags);
	    closeAllChildren(uls);
	    setParentClasses(lis);
		showCurrent(lis);
	    setOnClickEvents(lis);
	}
}

//---------------------------------------------------------------------
// Closes all sub branches of the parsed item
function setOnClickEvents(lis)
{
	// Set onclick for all parent li elements
	for(var i=0; i<lis.length; i++)
	{
		var uls = lis[i].getElementsByTagName('ul');
		var aTag = lis[i].getElementsByTagName('a');
		
		if(uls.length > 0){
			var func = "javascript:toggle("+i+")";
			//lis[i].setAttribute("href",func);
			aTag[0].setAttribute("onClick",func);
		}
		else{
			lis[i].onclick = function(){
				this.id = "current";
				resetChildren(this);
				this.className = "isActive";
				//showCurrent(lis); 
			}
		}
		
	}
}

//---------------------------------------------------------------------
// When a child element is clicked thi swill reset all child element id's and classes
function resetChildren(li)
{
	var nav=document.getElementById('navExpMenu');
	var lis=nav.getElementsByTagName('li');
	
	for(var i=0; i<lis.length; i++)
	{
		var subNav = lis[i].getElementsByTagName('ul');
		if(subNav.length == 0){
			lis[i].className = "isChild";
			lis[i].id = "";
		}
	}
}

//---------------------------------------------------------------------
// Closes all sub branches of the parsed item
function setParentClasses(lis)
{
	for(var i=0; i<lis.length; i++)
	{
		var subNav = lis[i].getElementsByTagName('ul');
		if(subNav.length>0){
			lis[i].className = "isParent";
		}else{
			lis[i].className = "isChild";
		}
	}
}
//---------------------------------------------------------------------
// Give a unique name to all a Tags in the menu
function nameATags(aTags)
{
	for(var i=0; i<aTags.length; i++)
	{
		aTags[i].setAttribute("name","aTag_"+i);
	}
}

//---------------------------------------------------------------------
// Checks if the parsed element has a sub menu and returns boolean
function isParent(li)
{
	var uls = li.getElementsByTagName('ul');
	return(uls.length>0);
}

//---------------------------------------------------------------------
// Searches the menu for an element of a certain type
// with the parsed 'name' attribute and returns it
function getElement(eType,eName)
{
	var nav=document.getElementById(menuID);
	var els=nav.getElementsByTagName(eType);
	for(var i=0; i<els.length; i++)
	{
		var el = els[i].getAttribute("name");
		//alert(el+" , "+eName);
		if(el == eName)
		{
			return(els[i].parentNode);
		}
	}
	return(null);
}
//---------------------------------------------------------------------
// Toggles the visibility of the parsed element
function toggle(id)
{
	var branch = getElement('a',"aTag_"+id);
	var uls = branch.getElementsByTagName('ul');
	branch = uls[0];
	
	if(branch.className == "hiddenChild")
	{
		branch.className = "shownChild";
		branch.parentNode.id = "current";
		branch.parentNode.className = "isActive";
	}else
	{
		branch.className = "hiddenChild";
		branch.parentNode.id = "";
		branch.parentNode.className = "isParent";
	}
}

//---------------------------------------------------------------------
// Closes all sub branches of the parsed item
function closeAllChildren(uls)
{
	for(var i=0; i<uls.length; i++)
	{
		uls[i].className = "hiddenChild";
	}
}

//---------------------------------------------------------------------
// Shows the current element and all elements above it
function showCurrent(lis)
{
	for(var i=0; i<lis.length; i++)
	{
		if(lis[i].id == "current"){
			if(isParent(lis[i])==true){
				lis[i].className = "isActive";
				showElement(lis[i].getElementsByTagName('ul')[0]);
			}else{
				showElement(lis[i]);
			}
			
		}
	}
}

//---------------------------------------------------------------------
// Shows the entire hierarchy from the top level down to the parsed element
function showElement(li)
{
	li.className = li.parentNode.className = "isActive";
	//alert(li.parentNode.parentNode.className);
	if(li.parentNode.id != menuID){
		//alert(li.parentNode.parentNode.firstChild.getAttribute('temp'));
		li.parentNode.parentNode.className = "isActive";
		showElement(li.parentNode);
	}
}

