
var undefined;
var menuDisplay;



function navItemBuilder(desc, depth) {
	this.desc = desc;
	this.depth = depth;
}


var menuStructure = new Array(
	new navItemBuilder("home",			0),
	new navItemBuilder("portfolio",		0),
	new navItemBuilder("collections",		1),
	new navItemBuilder("landscapes",			2),
	new navItemBuilder("bodies",				2),
	new navItemBuilder("drawings",			1),
	new navItemBuilder("archive",			1),
	new navItemBuilder("spaces",		0),
	new navItemBuilder("exhibitions",		1),
	new navItemBuilder("studio",			1),
	new navItemBuilder("biography",		0),
	new navItemBuilder("statement",			1),
	new navItemBuilder("cv",				1),
	new navItemBuilder("contact",		0),
	new navItemBuilder("links",			0)
);


function onNavClick(itemRef) {
	//alert("onNavClick("+itemRef+")\n\nmenuStructure[itemRef] = "+menuStructure[itemRef]);
	var imgState = "on";
	var i = itemRef-1;
	var currentLevel = menuStructure[i].depth;
	// loop through menuStructure looking for children of the clicked
	for (j=i+1; j<menuStructure.length; j++) {
		//alert("j="+j+"\n\nmenuStructure[j].depth = "+menuStructure[j].depth);
		// if the next item is at the next level then it must be a child
		if (menuStructure[j].depth > currentLevel) {
			var itemNum = j+1;
			// if it's currently showing then we need to close it
			if (menuDisplay[j] == 1) {
				document.getElementById("navItem_"+itemNum).style.display = "none";
				menuDisplay[j] = 0;
				imgState = "off";
			}
			// else open it, but only if it is a direct child (i.e. one level down)
			else {
				if (menuStructure[j].depth == currentLevel+1) {
					document.getElementById("navItem_"+itemNum).style.display = "";
					menuDisplay[j] = 1;
				}
			}
		}
		// else if it's at the same or lower level then we're done
		else if (menuStructure[j].depth <= currentLevel) {
			break;
		}
	}
	// close all other open menu branches
	closeOtherBranches(itemRef);
	// make clicked item bold
	document.getElementById("navImg_"+itemRef).src = "images/nav/"+ menuStructure[i].desc +"_"+ imgState +".gif";
	window.focus();
}


function closeOtherBranches(itemRef) {
	var i = itemRef-1;
	// find the root item of the branch
	for (j=i; j>=0; j--) {
		// if we're at the lowest depth then we have found the root item, so break
		if (menuStructure[j].depth == 0) {
			break;
		}
	}
	//alert("root item is "+j);
	// loop through the menu
	for (k=0; k<menuStructure.length; k++) {
		// if it's a level zero item and it's not the branch we want to keep open, then close all it's children
		if (menuStructure[k].depth == 0 && k != j) {
			//alert("closing all children for item "+k);
			for (m=k+1; m<menuStructure.length; m++) {
				if (menuStructure[m].depth == 0) { break; }
				var itemNum = m+1;
				document.getElementById("navItem_"+itemNum).style.display = "none";
				menuDisplay[m] = 0;
			}
		}
	}
}


// stores menu state in cookie
function onLeavePage() {
	// NO LONGER NEED TO SET THIS SO RETURN
	return;
	// alert("onLeavePage()");
	if (navigator.cookieEnabled) {
		var expDate = getExpDate(1, 0, 0);
		var arrayAsString = "";
		if (menuDisplay.length) { arrayAsString = menuDisplay.join(""); }
		//alert("setting cookie = "+arrayAsString);
		setCookie("displayValues", arrayAsString, expDate);
	}
}


// expands menu to previously viewed state, called after menu is drawn out (NAME CHANGED AS NO LONGER USED - FUNCTIONAL REQUIREMENTS HAVE CHANGED)
function expandMenuX() {
	// extract menu display details from cookie
	if (navigator.cookieEnabled) {
		var displayValues = getCookie("displayValues");
		//alert("displayValues (from cookie) = "+displayValues);
	}
	// if there is nothing in the cookie then set display array to defaults and return
	if (displayValues === undefined || displayValues == null) {
		//alert("no data - setting to default...");
		menuDisplay = new Array(1,1,0,0,0,0,0,1,0,0,1,0,0,1,1);
		return;
	}
	// else populate the menuDisplay array
	else {
		menuDisplay = displayValues.split("");
	}
	//alert("menuDisplay = "+menuDisplay);
	// loop through display array and set display properties
	for (i=0; i<menuDisplay.length; i++) {
		var itemNum = i+1;
		if (menuDisplay[i] == 1) {
			document.getElementById("navItem_"+itemNum).style.display = "";
		}
	}
}


// expands the menu just enough to show only the current page
function expandMenu() {
	// if there is no menuDisplay set on the page then set display array to defaults and return
	if (menuDisplay === undefined) {
		//alert("no data - setting to default...");
		menuDisplay = new Array(1,1,0,0,0,0,0,1,0,0,1,0,0,1,1);
		return;
	}
	//alert("menuDisplay = "+menuDisplay);
	// loop through display array and set display properties
	for (i=0; i<menuDisplay.length; i++) {
		var itemNum = i+1;
		if (menuDisplay[i] == 1) {
			document.getElementById("navItem_"+itemNum).style.display = "";
		}
	}
}



// test function
function expandAll() {
	menuDisplay = new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
	// loop through display array and set display properties
	for (i=0; i<menuDisplay.length; i++) {
		if (menuDisplay[i] == 1) {
			var itemNum = i+1;
			//if (itemNum < 10) { itemNum = "0"+itemNum; }
			document.getElementById("navItem_"+itemNum).style.display = "";
		}
		else {
			document.getElementById("navItem_"+itemNum).style.display = "none";
		}
	}
}
