// Does the current XML element contain, or is it itself 
// the current menu?
var containsOrIsSelected = function(xml, currentMenu) {
    if( xml.getAttribute("name") == currentMenu ) {
	return true;
    }
    for( var i=0;i<xml.childNodes.length;i++ ) {
	if( xml.childNodes[i].nodeType == 1 ) {
	    if( containsOrIsSelected(xml.childNodes[i], currentMenu) ) {
		return true;
	    }
	}
    }
    return false;
}

// Menu type
function Menu(xml, currentMenu) {
    // name : String
    this.name = xml.getAttribute("name");
    // link : String
    this.link = xml.getAttribute("link");
    // active : boolean
    if( this.name == currentMenu ) {
	this.active = true;
    }
    else {
	this.active = false;
    }

    // children: Menu[]
    this.children = new Array();

    // Initialize children: We only want to create a menu that reflects
    // the items that should be drawn.
    if( containsOrIsSelected(xml, currentMenu) ) {
	var num_children = 0;
	for( var i=0; i<xml.childNodes.length; i++ ) {
	    if( xml.childNodes[i].nodeType == 1 ) {
		this.children[num_children] = 
		    new Menu(xml.childNodes[i], currentMenu);
		num_children++;
	    }
	}
    }

    // draw : int -> String, given your current level of indentation...
    this.draw = function(level) {
	// remove this after testing (NEB Don't know what this means)
        var domain = document.domain;
	var on_click = "onClick=\"loadAndDrawMenusFromFile('http://" + domain + "/menus.xml','" + this.name + "','navbarContents')\"";
      
	var result = "<a style=\"padding-left: " + level +"em; \"";

	if( this.active ) {
	    result = result + " class=\"selected\" ";
	}

	result = result + " href=\""+this.link+"\" " + on_click + ">" + 
	this.name +"</a>";
	// Also draw children
	for( i in this.children ) {
	    result = result + this.children[i].draw(level+1);
	}
	return result;
    }
}

// Top Level Menu Type
function TopLevelMenu(xml, currentMenu) {
	// children : Menu[]
   this.children = new Array();

	var xml_children = xml.childNodes;
	var child_count = 0;
	for( var i = 0; i<xml_children.length; i++ ) {
	  if( xml_children[i].nodeType == 1 ) {
		 this.children[child_count] = 
		   new Menu(xml_children[i], currentMenu);
		 child_count++;
	  }
	}

	// draw : () -> String, calls draw on each child
	this.draw = function() {
		var result = "";
		for( i = 0; i<this.children.length; i++ ) {
			result = result + this.children[i].draw(1);
		}
		return result;
	}
}

// Uses AJACSSS to load an xml file with the menu info.
// Takes an URL and the name of the current location in 
// the menu tree, and prints it all out to the given div
function loadAndDrawMenusFromFile(fileURL,currentMenu,divName) {
		loadXMLDocument(fileURL,currentMenu,divName);
}

function drawMenuFromXML(xml,currentMenu,divName) {
		document.getElementById(divName).innerHTML = 
			(new TopLevelMenu(xml, currentMenu)).draw();
}

// Most of this code was blatantly stolen from the W3c
// schools AJAX tutorial
function loadXMLDocument(fileURL,currentMenu,divName) {
		var xmlHttp;

		try 
	{
			// Firefox, oprera, safari
			xmlHttp = new XMLHttpRequest();
	}		 
		catch(e)
	{
			try 
		{
				// IE
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
			catch(e)
		{
				try 
			{
					xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
				catch(e)
			{
					drawMenuFromXML(returnDefaultMenu(), 
							currentMenu, divName);
			}
		}
	}
		
		xmlHttp.onreadystatechange = function() 
	{
			if( xmlHttp.readyState == 4 )
		{
				drawMenuFromXML(xmlHttp.responseXML.documentElement,
						currentMenu, divName);
		}
	};
		xmlHttp.open("GET",fileURL,true);
		xmlHttp.send(null);
}

// Return a basic menu, for the non-ajax set.
function returnDefaultMenu() {
		return "CRAPPY DEFAULT! IT NO WORKING."
}

// Initialize
function initializeMenu() {

}

// Given the currently selected menu item
// draw the correct menu.
function drawMenu(currentSelection) {

}
