// ==================================================================================================
// MENU PARSER (2008-10)
// ==================================================================================================
// name: string. the menu label
// url: string. the url for this menu
// css_class: optional string. any class you would like applied to this menu
// containing_div: optional string referencing the id of the div the menu code is to be placed into
function menu(name, url, css_class, containing_div){
	this.name = name;
	this.url = url;
	this.css_class = css_class;	
	this.containing_div = containing_div;
	this.sub_menu_names = new Array();
	this.sub_menu_urls = new Array();
	this.sub_menu_classes = new Array();
	this.addMenuItem = addMenuItem;
}

// -----------------------------------------------------------------------
function addMenuItem(name, url, css_class){
	this.sub_menu_names[this.sub_menu_names.length] = name;
	this.sub_menu_urls[this.sub_menu_urls.length] = url;
	this.sub_menu_classes[this.sub_menu_classes.length] = css_class;
}

// -----------------------------------------------------------------------
// accepts root menu item to generate unordered list
// this method fills a div with an id="nav" with the menu code
// contents of this div tell the function:
// dir switches: applies any folder/dir switches you like to the beginning of non-absolute urls
// current page: case sensitive search that highlights the current page in the menu
function parseMenu(menu){
	content = "";
	var url;
	var css_class;
	
	// get the current page title and any dir switches
	if(document.getElementById(menu.containing_div)){
		var page_args = document.getElementById(menu.containing_div).innerHTML.split(",");
		dir = page_args[0];
		current_section = page_args[1];
	}
	
	// get the current page filename
	var current_page = document.URL.substr(document.URL.lastIndexOf('/')+1);
	
	// root menu has no name - do not display
	if ( menu.name != "" ){		
		// check if the url is absolute - if so, do not append dir switch
		( menu.url.match("http://") ? url = menu.url : url = dir + menu.url );
		
		// has a css class been specified?
		( menu.css_class ? css_class =  'parent ' + menu.css_class : css_class = 'parent');
		
		// search and highlight current page title
		if( current_section == menu.name ){
			css_class += ' current';
		}
		
		// if there are css classes, create the class attribute to be added to the link
		if( css_class.length != 0 ){
			css_class = '" class="' + css_class;
		}
		
		content += '<li><a href="' + url + css_class + '">' + menu.name + '</a><ul>';
	}
	
	// hooray for recursion! print out submenus and their links
	for ( var n=0; n<menu.sub_menu_names.length; n++ ) {
		if ( typeof(menu.sub_menu_names[n] ) != 'object') {
			url = css_class = '';
			
			// check if the url is absolute - if so, do not append dir switch
			( menu.sub_menu_urls[n].match("http://") ? url = menu.sub_menu_urls[n] : url = dir + menu.sub_menu_urls[n] );
			
			// has a css class been specified?
			( menu.sub_menu_classes[n] ? css_class = menu.sub_menu_classes[n]: css_class = '');
			
			// search and highlight current page title
			if( current_section == menu.sub_menu_names[n] ){
				css_class += ' current'
			}
			
			// if there are css classes, create the class attribute to be added to the link
			if( css_class.length != 0 ){
				css_class = '" class="' + css_class;
			}
			
			content +=  '<li><a href="' + url + css_class + '">' + menu.sub_menu_names[n] + '</a></li>';
		} else {
			content += parseMenu(menu.sub_menu_names[n], dir, current_section);
		}
	}
	
	content += '</ul></li>';
	return content;
}