﻿// LeftNav JavaScript setup, Copyright 2007-2012 Mark Licata (iweb@washington.edu)

//Global variable assigned to the element containing sublinks
var selectedNav0;

//Initial function to close the navigation setup on page load
function collapseNavInitial() {
	collapseNavFunc(document.getElementById("LeftNav"), true);
}

//function called onclick to close the
function collapseNav(navRoot) {
	collapseNavFunc(navRoot, false);
}

function collapseNavFunc(navRoot,initial) {
    //loop which iterates over each child element
	for(var n = 0; n < navRoot.childNodes.length; n++) {
	    var cur = navRoot.childNodes[n];
	    if(cur.nodeName == "LI") {
		    var childList = cur.getElementsByTagName("ul")[0];
		    if(!childList) continue;
		    var text = cur.getElementsByTagName("span")[0];
		    if(!text) continue;
			
		    if(cur.className == "navSel") {	
			    childList.open = true;
			    childList.realHeight = childList.offsetHeight;
			    childList.curHeight = childList.offsetHeight;
			    childList.style.height = childList.offsetHeight + "px";
			    childList.style.overflow = "hidden";
			    childList.style.display = "block";
				
			    clickNav(childList,initial); 
		    } else {
			    childList.open = false;
			    childList.realHeight = childList.offsetHeight;
			    childList.curHeight = 0;
			    childList.style.height = "0px";
			    childList.style.overflow = "hidden";
			    childList.style.display = "none";
		    }
			
		    text.onclick = clickNavClosure(childList);
		    //collapseNavFunc(childList,initial);
	    }
	}
}

function clickNavClosure(e) {
    return function() { clickNav(e,false); }
}

function clickNav(childList,initial) {
	if(!childList.open) {
		childList.style.display = "block";
		childList.open = true;
		resizeList(childList,initial);
	}
	if(selectedNav0) {
		selectedNav0.open = false;
		resizeList(selectedNav0,initial);
	}
	selectedNav0 = ( (childList == selectedNav0) ? null : childList);
}

//
function resizeList(ul,initial) {
	var direction  = (ul.open ? 1 : -1);
	if (initial) {
		ul.curHeight += direction * 500; // pixels per resize step
	} else {
		ul.curHeight += direction * 30; // pixels per resize step
	}
	if(ul.curHeight > ul.realHeight){
	    ul.curHeight = ul.realHeight;
	}
	if(ul.curHeight < 1){
	    ul.curHeight = 1;
	}
	
	//set list height in pixels
	ul.style.height = ul.curHeight + "px";

	if( ((ul.curHeight > 1) && !ul.open) || ((ul.curHeight < ul.realHeight) && ul.open) ) {
		if (!initial) {
			setTimeout(function() { resizeList(ul); }, 50); // delay between resize cycles
		}
	}
		
	if(!ul.open) {
		ul.style.display = "none";
	} else {
		ul.style.display = "block";
	}
}