function getElementsByStyleClass(className,insideElement) {
	var nothing;
	if(insideElement==nothing){
		var all = document.all ? document.all : document.getElementsByTagName('*');
	} else {
		var all = insideElement.getElementsByTagName('*');
	}
	var elements = new Array();
	for (var e = 0; e < all.length; e++)
	if (all[e].className == className){
		elements[elements.length] = all[e];
	}
	return elements;
}

var verticle; //Set to decide if this is a horozontal or verticle menu, beyond rendering it will also stop items from closing while you hover over them.
var keepopen; //Keeps the route to an current_page menu item open
var snap; //True if you want menus to snap closed when you go back to the document
var disable; //Sometimes you may want to disabled altogher, such as very long verticle menus.

function buildsubmenus(ve,ke,sn,di){
	verticle = ve;
	keepopen = ke;
	snap = sn;
        disable = di;

        var ultags=document.getElementById("nav").getElementsByTagName("ul")

	for (var t=0; t<ultags.length; t++){
		ultags[t].parentNode.getElementsByTagName("a")[0].className+=" subfolderstyle"	
		if (ultags[t].parentNode.parentNode.id=="nav"){ //if this is a first level submenu
			if(verticle){//it always sits out to the right
				ultags[t].style.left="0px" //dynamically position first level submenus to be width of main menu item
			} else {
                            if(ultags[t].parentNode.parentNode.parentNode.parentNode.tagName=="UL"){ //Third drops out to the right, this might be doable is css and the whole parent block could go
                                ultags[t].style.left=ultags[t].parentNode.offsetWidth+"px" //dynamically position first level submenus to be width of main menu item
                            } else {
                                ultags[t].style.left="0px" //dynamically position first level submenus to be width of main menu item
                            }
			}
		} else {//else if this is a sub level submenu (ul)

			ultags[t].style.left=ultags[t-1].getElementsByTagName("a")[0].offsetWidth+"px" //position menu to the right of menu item that activated it
		}
                if(!disable){
                    ultags[t].parentNode.onmouseover=function(){
                            this.getElementsByTagName("ul")[0].style.display="block";
                    }
                }
		
		if(!verticle){
			ultags[t].parentNode.onmouseout=function(){
				this.getElementsByTagName("ul")[0].style.display="none";
			}
		} 
	}
	
	if(snap){ //This bit controls if menus close when they hover over the content
		document.getElementById("main").onmouseover=function(){
			var parents = document.getElementById("nav").getElementsByTagName("ul");
			for(var i=0 ; i<parents.length ; i++){
				parents[i].style.display="none";
			}
                        if(verticle){
                            lightThePath();
                        }
		}
	} 
	if(keepopen){
		lightThePath();
	}

}

function lightThePath(){ //This opens the path to the current page and it's children if they exist
    	//alert('initialising path light');

	var path = new Array();

	path = getElementsByStyleClass('path subfolderstyle');


	if(path!=null){ //Checks if "path" classed files exist
		for(var i = 0; i<path.length; i++){
			path[i].nextSibling.style.display="block";	//Making paths visible
			if(verticle){
				path[i].parentNode.onmouseout=null;			//Disabling hiding on a path menu
			}
		}
	}

	try {
		var current_page = new Array();
		current_page = getElementsByStyleClass('current_page subfolderstyle');

		try {
			current_page[0].nextSibling.style.display="block";	//Making lower menu stay open
			if(verticle){
				current_page[0].parentNode.onmouseout=null;
			}
		} catch(all) { }
	} catch(all) {
		alert('fix php');	
	}	 
}
