// JavaScript Document
// TOP NAV SCRIPT 
/* This script parses the top Nav XMl doc */

//This function gets the menu item name and path from the xml
function parseTopNav(selected) {
	//alert("parseTopNav was called");
	
	var nodeLocation = null;
	var linkNum = null;
	
	// find the location of the root element and set the total number of child nodes
	for (j = 0; j <= topNavXML.childNodes.length; j++) {
		if (topNavXML.childNodes[j].nodeName == "root") {
			nodeLocation = j;
			linkNum = topNavXML.childNodes[j].childNodes.length;
			//alert("linkNum = " + linkNum);
			break;
		}
		
		else {
			//do nothing	
		}
	}
	
	for (i = 0; i < linkNum; i++) {
		var myItem = topNavXML.childNodes[nodeLocation].childNodes[i];
		
		if (myItem.nodeName == "item") {
			//alert(myItem.getAttribute("name"));
			//alert(myItem.getAttribute("path"));
			buildMenuItem(myItem, i, selected);	

		} else {
			//do nothing	
		}
	
	}
	
}


//This function builds a menu LI and appends it to the list
function buildMenuItem(myItem, num, selected) {
	//alert("buildMenu called");
	
	var myMenu = document.getElementById("topNavUl");
	var myLi = null;
	
	if (myItem.getAttribute("name") == selected) {
		myLi = document.createElement("li");
		myLi.setAttribute("className", "selected");
		myLi.setAttribute("class", "selected");
		
		myText = document.createTextNode(myItem.getAttribute("name"));
		myLi.appendChild(myText);
		
		
	}
	
	else {
		myLi = document.createElement("li");
		myAnchor = document.createElement("a");
		//myAnchor.setAttribute("href", myItem.getAttribute("path"));
		myAnchor.setAttribute("href", "javascript:void(0);");
		var path = myItem.getAttribute("path");
		myAnchor.onmousedown = function(){createLink(path);};
		//myAnchor.setAttribute("onmousedown", "javascript:createLink('" + path + "');");
		myText = document.createTextNode(myItem.getAttribute("name"));
		
		myAnchor.appendChild(myText);
		myLi.appendChild(myAnchor);
	}
	
	myMenu.appendChild(myLi);
	
}


//This function adds the player variables to the link & goes to the new page
function createLink(path) {
	//alert("createLink was called");
	document.getElementsByName("PLAYER")[0].navigatePage(path);
	PLAYER.navigatePage(path);
}

//This funcition called back from mp3player sets the vars for the url & calls window.open
function setUrlStrings(path, pstate, ptrack, ptime)
{
	//alert("setUrlStrings was called");
	var myUrl = path + "?pstate=" + pstate + "&ptrack=" + ptrack + "&ptime=" + ptime;
	window.open(myUrl, "_self");
}

function traceAction(myString)
{
	alert(myString);	
}