
/*
  ExpandMenu
  
  Clicking on the list item will show any nested list, pushing other elements down the page.
  Initially hidden with javascript so if javascripti isn't on the list will appear as normal.
  
*/
ExpandMenu = {

    init: function(id){
        
        var ul = document.getElementById(id);
        if(null == ul) return;
        var lis = ul.getElementsByTagName('LI');
        
        for(var i=0, l=lis.length;i<l; i++){        
            var li = lis[i];
            var sub = li.getElementsByTagName('UL');
            if(null == sub[0]) continue;
            
            // Wrap the list elements text in a link (A) if it
            // isn't already, or is wrapped in some other element
            var fchild = child = li.firstChild;
            if(fchild.nodeType == 1 && fchild.nodeName != 'A'){
                while(null != child && child.nodeType == 1){
                    child = child.firstChild;
                }
                var a = document.createElement('A');
                a.setAttribute('id',removeSpaces(child.nodeValue) + '_id');
                a.setAttribute('href','#');
                a.setAttribute('title','Expand '+child.nodeValue);
                a.appendChild(document.createTextNode(child.nodeValue));
                li.replaceChild(a,fchild);
            }
            sub[0].style.display = 'none'; // Hide submenu to start with
            a.sub = sub[0]; // Give link a reference to the submenu
            a.expanded = false;// Tell it the menu isn't currently expanded
            
            // Add onclick handler to show/hide the menu
            a.onclick = function(){
                this.sub.style.display = this.expanded ? 'none' : 'block';
                this.expanded = !this.expanded;
                this.expanded ? 
                    addClassName(this.parentNode,'expanded') :
                    removeClassName(this.parentNode,'expanded'); // Hook for styling expanded state  ;
                this.blur();
                return false;
            }
        }

        // if there is a openMenuItem defined on this page, attempt to pre-open the specified menu item                
        var openMenuItemVar = document.getElementById('openMenuItem'); 
        if(openMenuItemVar != null && openMenuItemVar.value != null)
        {
            var toOpenObjectObject = document.getElementById(openMenuItemVar.value); 
            if(toOpenObjectObject != null)
            {
                toOpenObjectObject.onclick();
            }
        }
    }
}

function removeSpaces(aString) 
{
    // make a variable to return
	var toReturn = "";
	
	// put a space in the string so it won't error upon splitting
	aString = '' + aString;
	splitstring = aString.split(" ");
	
	// loop over all the splitted values and add them to the return string
	for(i = 0; i < splitstring.length; i++)
	{
	    toReturn += splitstring[i];
	}
	
	// return the string with no spaces
	return toReturn;
}

//addEvent(window,'load',function(){ ExpandMenu.init('menu'); });

domFunction(function(){ ExpandMenu.init('menu'); },{'menu':'id'})

