/**
 * writes tree menu
 *
 */
function initTree() {	
	if (typeof masterList == "undefined") {
		return;
	}

	//set an event handler for persistant state
	if (document.all) {
		window.onbeforeunload=List.setTreeCookie;
	}

	//set some defaults
	List.setDefaults();
	//List.itemDescColumnWidth = List.masterListWidth-List.buttonColumnWidth-List.toggleColumnWidth;

	//set tree state
	List.setState();
	
	//write the menu
	masterList.write();
}//initTree()


/***************start of List class definition*****************/
/*
 * List()
 *
 * List class constructor
 *
 * @param String desc - list description string (optional)
 * @param String href - (optional) <a> href action if the list desc should be clickable
 * @param String openImage - name of image to be displayed when list is open
 * @param String closedImage - name of image to be displayed when list is closed
 * @param String tdStyle - the style to set to the containing tds
 * @param int leftIndent - the amount to indent the list from it's parent
 *
 */
function List(desc, href, openImage, closedImage, tdStyle, leftIndent) {
	this.desc = desc;
	this.items = new Array();
	
	this.isMasterList = false;
	//set later when added to another list
	this.parentList = null;
	//closed until explicitly stated
	this.listOpen = false;
	//if null, use default images
   this.openImage = openImage||List.defaultOpen;
	this.closedImage = closedImage||List.defaultClosed;
	this.href = href||null;
	//set styles to defaults for now
	this.tdStyle = tdStyle||List.defaultParentCss;
	//this.hrefStyle = hrefStyle||List.defaultParentLinkCss;
	/*
	if (List.listArray.length < 2) {
		alert("leftIndent: " + leftIndent);
		alert("default: " + List.defaultLeftIndent);
	}
	*/
	this.leftIndent = leftIndent||List.defaultLeftIndent;
	/*
	if (List.listArray.length < 2) {
		alert("leftIndent is now: " + this.leftIndent);
	}
	*/

	//id is it's index in the array, for a change
	this.id = List.listArray.length;
	List.listArray[this.id] = this;
	//preload images
	preloadImage(this.openImage);
	preloadImage(this.closedImage);
}//end List(String)

//class methods and variables
//sets default values
List.setDefaults = setListDefaults;
//sets open/closed state of tree nodes
List.setState = setState;
//sets cookie that stores current tree open/closed state
List.setTreeCookie = setTreeCookie;

//where preloaded images are stored
List.imageArray = new Array();
List.imageDir = "";//where to go hunting for the images
List.preloadImage = preloadImage;
//where all the List objects are stored
List.listArray = new Array();
//bullet image for items, if null, not displayed
List.bulletImage = null;
//default css classes
List.defaultParentCss = null;
List.defaultChildCss = null;
//default indent on left
List.defaultLeftIndent = 5;
//current page/selected css classes 
List.currrentParentTdCss = null;
List.currentChildCss = null;

//List.defaultParentLinkCss = null;
//List.defaultChildLinkCss = null;
//List.currentParentLinkCss = null;
//List.currentChildLinkCss = null;

//first column width
//List.toggleColumnWidth = 20;
//set bullet column width regardless
//List.bulletColumnWidth = 5;
//item desc column width
//List.itemDescColumnWidth = 100;

//default open/close images
List.defaultOpen = null;
List.defaultClosed = null;

//current parent open/close images
List.currentOpen = null;
List.currentClosed = null;

//css class for master list
List.masterListCss = null;

//the current page the user is on, in case we want a separate look for that link
List.currentPage = null;
//just in case
d = document;

/**
 * List.preloadImage()
 *
 * preloads indicated image so browser caches and
 * load times improve
 *
 */
function preloadImage(imageName) {
	if (imageName==null) { return; }
	var imageSrc = List.imageDir+"/"+imageName;

	//if it's not already in there, we add it
	if (List.imageArray[imageName]==null) {
		List.imageArray[imageName] = new Image();
		List.imageArray[imageName].setAttribute('src', imageSrc);
	}
}//end List.preloadImage(String)

/**
 * List.setDefaults()
 *
 * sets default values as required
 *
 */
function setListDefaults() {
	List.currentParentCss = List.currentParentCss||List.defaultParentCss;
	List.currentChildCss = List.currentChildCss||List.defaultChildCss;
	//List.currentParentLinkCss = List.currentParentLinkCss||List.defaultParentLinkCss;
	//List.currentChildLinkCss = List.currentChildLinkCss||List.defaultChildLinkCss;
	List.currentOpen = List.currentOpen||List.defaultOpen;
	List.currentClosed = List.currentClosed||List.defaultClosed;

	//preload the images while we're here
	preloadImage(List.defaultOpen);
	preloadImage(List.defaultClosed);
	preloadImage(List.currentOpen);
	preloadImage(List.currentClosed);
}//List.setDefaults()

/**
 * List.setState()
 *
 * sets open/closed state of tree nodes
 *
 * @param state - delimited list of open nodes
 *
 */
function setState() {
	//get cookies here and set open states
	//alert(document.cookie);
	var state = null;
	if (document.cookie != null && document.cookie != "") {
		var cookies = document.cookie.split(";");
		var theCookie;
		var cookieValue = "";
		for (var i=0; i<cookies.length; i++) {
			theCookie = cookies[i].split("=");
			//if (theCookie[0] == "treeState") {
			if (theCookie[0].indexOf("treeState") != -1) {
				state = theCookie[1];
			//} else if (theCookie[0] == "currentPage") {
			}
			/*
			//NEED TO DO: uncomment this if go back to setting it by cookie
			else if (theCookie[0].indexOf("currentPage") != -1) {
				//b/c there might be query strings, reconstruct the url from the rest of theCookie array
				for (var j=1; j<theCookie.length; j++) {
					cookieValue += theCookie[j];
					if (j != theCookie.length - 1) {
						cookieValue += "=";
					}
				}
				
				List.currentPage = cookieValue;
			}
			*/
		}
	}

	//set list nodes to open as cookie indicates
	if (state != null && state != "") { 
		var openList = state.split("`");
		if (openList==null) { return; }
	
		for (var i=0; i<openList.length; i++) {
			if (List.listArray[openList[i]]) {
				List.listArray[openList[i]].listOpen = true;
			}
		}//end for
	}//if state != null

	//now set styles for current page and it's parent
	if (List.currentPage == null) { return; }
	
	//for the most part, go completely thru the array b/c of the way we are doing links.  if there are duplicates, we want the last one
	var listItem = null;
	var currentPageItem = null;
	var index = List.currentPage.indexOf("?");
	var currentPageItemBase = List.currentPage;

	var currentListItemBase = "";
	//trim off query string
	if (index != -1) {
		currentPageItemBase = List.currentPage.substring(0, index);
	}

	for (var i=0; i<List.listArray.length; i++) 
	{
		listItem = List.listArray[i];
		index = (listItem.href != null)?listItem.href.indexOf("?"):-1;
		currentListItemBase = (index != -1)?listItem.href.substring(0, index):listItem.href;
		
		var u_i;
		var len;
		var li_sub;

		if(listItem.href != null && listItem.href.substring(0, 6) == '/login')
		{
			u_i = listItem.href.indexOf("origURL=") + 8;
			len = listItem.href.length;
		 	li_sub = listItem.href.substring(u_i, len);

			if(li_sub == List.currentPage)
			{
				// alert(listItem.href + ' \n\n ' + List.currentPage);
				// alert(listItem.href.substring(u_i, len));			
		
				currentPageItem = List.currentPage;
			}
		}

		//search is from most exact to least exact
		if (listItem.href == List.currentPage || li_sub == List.currentPage) 
		{
			currentPageItem = listItem;
			
			//only break if is an item and match is exact
			if (typeof currentPageItem.items == "undefined") 
			{
				break;
			}
		} 
		else 
		if (currentListItemBase == currentPageItemBase) 
		{
			currentPageItem = listItem;
		} 
		else 
		{
         		currentPageItem = null;
		}
	}//end for

	//if we found a currentPageItem, set styles accordingly
	if (currentPageItem != null) {
		currentPageItem.tdStyle = (typeof currentPageItem.items != "undefined")?List.currentParentCss:List.currentChildCss;

		if (currentPageItem.parentList) {
			currentPageItem = currentPageItem.parentList;
			currentPageItem.listOpen = true;
			currentPageItem.tdStyle = List.currentParentCss;
			//currentPageItem.hrefStyle = List.currentParentLinkCss;
			currentPageItem.openImage = List.currentOpen;
			currentPageItem.closedImage = List.currentClosed;
		}
	}
}//setState(String)

/**
 * writes tree state to cookie
 *
 */
function setTreeCookie() {
	var cookieStr = "";
   
	for (var i=1; i<List.listArray.length; i++) {
		if (List.listArray[i].listOpen) {
			cookieStr += i+"`";
		}
	}
	if (cookieStr.length > 0) {
		cookieStr = cookieStr.substring(0, cookieStr.length-1);
	}

	document.cookie = "treeState="+cookieStr;
}//setTreeCookie()

//instance methods and variables
List.prototype.addItem = addItem;
List.prototype.toString = listToString;
List.prototype.write = listWrite;

count = 0;
/*
 * List.addItem(newItem)
 *
 * @param Object newItem can be list or item
 *
 * adds new items to the list.  sets item's parentList to itself
 *
 */
function addItem(newItem) {
	this.items[this.items.length] = newItem;
	newItem.parentList = this;
}//end List.addItem()

/**
 * recursively calculates item's left indent
 *
 * @param item (list or item obj)
 *
 * @return indent
 */
function calculateLeftIndent(item) {
	var indent = 0;
	if (item.parentList == null) {
		indent = (typeof item.leftIndent=="undefined")?0:item.leftIndent;
	} else {
		indent = ((typeof item.leftIndent=="undefined")?0:item.leftIndent)+calculateLeftIndent(item.parentList);
	}

	if (count < 5) {
		//alert("returning: " + indent);
		count++;
	}
	return indent;
}//end calculateLeftIndent(Object)


/**
 * List.toggleList(listId)
 *
 * @param int listId
 *
 * toggles list display between open/closed (display = 'block'/'none')
 *
 */
function toggleList(listId) {
	//var list = List.listArray[listId];
	var list = List.listArray[listId];
	var displayType = (list.listOpen)?"none":"block";
	var listDiv = null;
	var toggleImage = d.images["toggleImage"+listId];

	for (var i=0; i<list.items.length; i++) {
		listDiv = d.getElementById("listItem"+list.items[i].id)
		listDiv.style.display = displayType;
	}

	list.listOpen = !list.listOpen;
	var imageName = (list.listOpen)?list.openImage:list.closedImage;
	toggleImage.setAttribute("src", List.imageArray[imageName].getAttribute("src"));

	/*
	if (isNS) {
		List.setTreeCookie();
	}
	*/
}//end List.toggleList()

/**
 * Item.toString()
 * 
 * returns string representation of the list
 */
function listToString() {
	var str = "";
	str += "list " + this.id + "\n";
	str += " - desc: " + this.desc + "\n";
	str += " - href: " + this.href + "\n";
	str += " - parentList: " + (this.parentList!=null)?this.parentList.id:"null" + "\n";
	str += " - listOpen: " + this.listOpen + "\n";
	str += " - openImage: " + this.openImage + "\n";
	str += " - closedImage: " + this.closedImage + "\n";
	str += " - tdStyle: " + this.tdStyle + "\n";
	//str += " - hrefStyle: " + this.hrefStyle + "\n";
	str += " ****** items ********\n";

	for (var i=0; i<this.items.length; i++) {
		str += this.items[i].toString();
	}

	return str;
}//listToString()

/**
 * List.write()
 *
 * generates html for list	
 * - this is custom made for a strictly parent-child (2-deep) list.
 *   anything else would probably require substantial rewriting of the write methods
 *   because of the styles used currently
 *
 * @return html
 *
 */
function listWrite() {
	var html = "";

	if (this.isMasterList) {
		var treeDiv = d.getElementById(this.parentList);
		if (treeDiv == null) {
			return html;
		}

		html += "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\"";
		if (List.masterListWidth > 0) {
			html += " width=\""+List.masterListWidth+"\"";
		}
		if (List.masterListCss != null) {
			html += " class=\""+List.masterListCss+"\"";
		}
		html += ">\n";
		for (var i=0; i<this.items.length; i++) {
			html += this.items[i].write();
		}

		html += "</table>\n";

		//var win = window.open("", "");
		//win.document.write(html);
		treeDiv.innerHTML = "";
		treeDiv.innerHTML = html;
		var listItem;
		for (var i=0; i<List.listArray.length; i++) {
			listItem = d.getElementById("listItem"+List.listArray[i].id);
			if (listItem!=null) {
				//if list open or it's parent list is open (last part b/c of changes for this tree), then set to block
				listItem.style.display = (List.listArray[i].listOpen)?"block":(List.listArray[i].parentList.listOpen)?"block":"none";
			}
		}
	} else {
		//first write out list description w/ open/closed image
		var imageSrc = List.imageDir+"/"+(this.listOpen?this.openImage:this.closedImage);
		html += "	<tr>\n";
		html += "		<td class=\""+this.tdStyle+"\" style=\"padding-left: ";
		html += calculateLeftIndent(this)+"px; padding-right: 2px;\" nowrap><span style=\"vertical-align: middle;\">";
		html += "<a href=\"javascript:void toggleList("+this.id+")\" style=\"padding-right: 2px;\"><img name=\"toggleImage"+this.id+"\" src=\"";
		html += imageSrc + "\"></a>";
		//html += "		<td valign=\"top\" class=\""+this.tdStyle+"\" width=\""+List.bulletColumnWidth+"\">&nbsp;</td>\n"; 
		//html += "		<td colspan=\"2\" class=\""+this.tdStyle+"\" style=\"vertical-align: top; width: "+(List.masterListWidth-List.buttonColumnWidth)+"px;\">";
		html += "</span><span style=\"vertical-align: middle;\">";
		if (this.href != null) {
			html += "<a href=\""+this.href+"\" class=\""+this.tdStyle+"\">"+this.desc+"</a>";
		} else {
			html += this.desc;
		}
		html += "</span></td>\n";
		html += "	</tr>\n";

		//now the items
		for (var i=0; i<this.items.length; i++) {
			html += this.items[i].write();
		}		
		return html;
	}
}//end List.write()



/***************end of List class definition*******************/

/***************start of Item class definition*******************/
/**
 * Item()
 *
 * @param String desc - desc text that is displayed in the html
 * @param String href - (optional) <a> href action if the item is clickable
 * @param String tdStyle - the style to set to the containing tds
 * @param String seperator - whether it's a separator or not
 * @param int leftIndent - the amount to indent the item from it's parent
 *
 * Item class constructor
 *
 */
function Item(desc, href, tdStyle, separator, leftIndent) {
	this.desc = desc||"&nbsp;";
	this.href = href;
	this.parentList = null;
	//set styles to default for now
	this.tdStyle = tdStyle||List.defaultChildCss;
	this.separator = separator||false;
	/*
	if (List.listArray.length < 5) {
		alert("item leftIndent: " + leftIndent);
		alert("item default: " + Item.defaultLeftIndent);
	}
	*/
	this.leftIndent = leftIndent||Item.defaultLeftIndent;
	/*
	if (List.listArray.length < 5) {
		alert("item leftIndent is now: " + this.leftIndent);
	}
	*/
	//this.hrefStyle = hrefStyle||List.defaultChildLinkCss;

	//id is it's index in the array, for a change
	this.id = List.listArray.length;
	List.listArray[this.id] = this;
}//end Item()

Item.prototype.toString = itemToString;
Item.prototype.write = itemWrite;
//default left indent from parent
Item.defaultLeftIndent = 5;

/**
 * Item.toString()
 * 
 * returns string representation of the item
 */
function itemToString() {
	var str = "";
	str += "item " + this.id + "\n";
	str += " - desc: " + this.desc + "\n";
	str += " - href: " + this.href + "\n";
	str += " - tdStyle: " + this.tdStyle + "\n";
	//str += " - hrefStyle: " + this.hrefStyle + "\n";
	str += " - parentList: " + this.parentList.id + "\n";

	return str;
}//itemToString()

/**
 * Item.write()
 *
 * generates html for item
 *
 * returns html
 *
 */
function itemWrite() {
	var html = "";

	html += "	<tr id=\"listItem"+this.id+"\">\n";
	html += "		<td class=\""+this.tdStyle+"\" style=\"padding-left: ";
	html += calculateLeftIndent(this) + "px;\" nowrap><span style=\"vertical-align: middle;\">";
	if (!this.separator && List.bulletImage != null) {
		var imageSrc = List.imageDir+"/"+List.bulletImage;
		html += "<img src=\"" + imageSrc + "\">";
	} else {
		html += "&nbsp;";
	}
	html += "</span><span style=\"vertical-align: middle;\">";
	if (this.href != null) {
		html += "<a href=\""+this.href+"\" class=\""+this.tdStyle+"\">";
	}
	html += this.desc;

	if (this.href != null) {
		html += "</a>";
	}

	html += "</span></td>\n";
	html += "	</tr>\n";

	return html;
}//end Item.write()
/***************end of Item class definition*******************/

