
var indentPixels = 5

// art files and sizes for three widget styles
// (all three widgets must have same height/width)
var collapsedWidget = "<img src='../images/sidenav/arrow-right.gif' width=7 height=8 border=0>"
var expandedWidget = "<img src='../images/sidenav/arrow-down.gif' width=7 height=8 border=0>"
var endpointWidget = "<img src='../images/sidenav/goldbullet.gif' width=5 height=5 border=0>"

// just for preloading
var widgetWidth = 10
var widgetHeight = 10

// fonts for parent and child items
var parentstylestart = "<font face='Arial,Verdana,Helvetica' size=2 color=#660033><b>";
var parentstyleend = "</b></font>";
var childstylestart = "<font face='Arial,Verdana,Helvetica' size=1>";
var childstyleend = "</font>";

// Target for documents loaded when user clicks on a link.
// Specify your target frame name here.
var displayTarget = "_top"

// Create array object containing outline content and attributes.
// To adapt outline for your use, modify this table.
// Start the array with [1], and continue without gaps to your last item.
// The order of the five parameters:
//    1. Boolean (true or false) whether _next_ item is indented.
//    2. String to display in outline entry (including <FONT> or style tags).
//    3. URL of link for outline entry; Use empty string ("") for no link
//    4. Integer of indentation level (0 is leftmost margin level)
//    5. String for status line during onMouseOver (apostrophes require \\')


var db = new Array()

// add more records to complete your outline
// ** END AUTHOR-ADJUSTABLE SPECIFICATIONS **//

function addparent(title,link) {
	db[db.length] = new dbRecord(true, title, link, 0, title);
}

function addchild(title,link) {
	db[db.length] = new dbRecord(false, title, link, 1, title);
}

// object constructor for each outline entry
function dbRecord(mother,display,URL,indent,statusMsg){
	this.mother = mother   // is this item a parent?
	this.display = display // text to display
	this.URL = URL         // link tied to text; if empty string, item appears as straight text
	this.indent = indent   // how many levels nested?
	this.statusMsg = statusMsg  // descriptive text for status bar 
	return this
}

// pre-load all images into cache
var fillerImg = new Image(1,1)
fillerImg.src = "filler.gif"
var collapsedImg = new Image(widgetWidth,widgetHeight)
collapsedImg.src = collapsedWidget
var expandedImg = new Image(widgetWidth,widgetHeight)
expandedImg.src = expandedWidget
var endpointImg = new Image(widgetWidth,widgetHeight)
endpointImg.src = endpointWidget

// ** functions that get and set persistent cookie data **
// set cookie data
var mycookie = document.cookie
function setCurrState(setting) {
        mycookie = document.cookie = "currState=" + escape(setting)
}
// retrieve cookie data
function getCurrState() {
        var label = "currState="
        var labelLen = label.length
        var cLen = mycookie.length
        var i = 0
        while (i < cLen) {
                var j = i + labelLen
                if (mycookie.substring(i,j) == label) {
                        var cEnd = mycookie.indexOf(";",j)
                        if (cEnd ==     -1) {
                                cEnd = mycookie.length
                        }
                        return unescape(mycookie.substring(j,cEnd))
                }
                i++
        }
        return ""
}

// **function that updates persistent storage of state**
// toggles an outline mother entry, storing new value in the cookie
function toggle(n) {
	var newString = ""
	var currState = getCurrState()
	var expanded = currState.charAt(n) // of clicked item
	newString += currState.substring(0,n)
	newString += expanded ^ 1 // Bitwise XOR clicked item
	newString += currState.substring(n+1,currState.length)
	setCurrState(newString) // write new state back to cookie
}

function alloffbutone(n) {
	var newString = ""
	for (var i=0; i<db.length; i++) {
		if (i == n) { newString += "1"; }
		else { newString += "0"; }
	}
	setCurrState(newString) // write new state back to cookie
}

function toggleparent(displaytext) {
	for (var i=0; i<db.length; i++) {
		if (db[i].mother && db[i].display == displaytext) {
			alloffbutone(i);
			return;
		}
	}
}

// **functions used in assembling updated outline**
// returns the proper GIF file name for each entry's control
function getGIF(n, currState) {
	var mom = db[n].mother  // is entry a parent?
	var expanded = currState.charAt(n) // of clicked item
	if (!mom) {
		return endpointWidget
	} else {
		if (expanded == 1) {
			return expandedWidget
		}
	}
	return collapsedWidget
}

// returns the proper status line text based on the icon style
function getGIFStatus(n, currState) {
	var mom = db[n].mother  // is entry a parent
	var expanded = currState.charAt(n) // of rolled item
	if (!mom) {
		return "No further items"
	} else {
		if (expanded == 1) {
			return "Click to collapse nested items"
		}
	}
	return "Click to expand nested items"
}

function drawmenu() {
	// initialize 'current state' storage field
	if (getCurrState() == "" || getCurrState().length != (db.length)) {
		initState = ""
		for (i = 0; i > db.length; i++) {
			initState += "0"
		}
		setCurrState(initState)
	}
	
	// build new outline based on the values of the cookie
	// and data points in the outline data array.
	// This fires each time the user clicks on a control,
	// because the HREF for each one reloads the current document.
	var newOutline = ""
	var prevIndentDisplayed = 0
	var showMyDaughter = 0
	var currState = getCurrState() // get whole state string
	// cycle through each entry in the outline array
	for (var i = 0; i < db.length; i++) {
		var theGIF = getGIF(i, currState)		// get the image
		var theGIFStatus = getGIFStatus(i, currState)  	// get the status message
		var currIndent = db[i].indent	// get the indent level
		var expanded = currState.charAt(i) 	// current state
	
		// display entry only if it meets one of three criteria
		if (currIndent == 0 || currIndent <= prevIndentDisplayed ||	(showMyDaughter == 1 && (currIndent - prevIndentDisplayed == 1))) {
			newOutline += "<TR>\n";
			if (currIndent > 0) {
				newOutline += "<TD VALIGN=TOP>\n";
				newOutline += "<IMG SRC=../\"images/template/spacer.gif\" HEIGHT = 1 WIDTH =" + (indentPixels * currIndent) + ">"
				newOutline += "<A HREF=\"javascript:history.go(0)\" " + "onMouseOver=\"window.status=\'" + theGIFStatus + 
				"\';return true;\" onClick=\"alloffbutone(" + i + ");return " + (theGIF != endpointWidget) + "\">"
				newOutline += theGIF + "</A>"
				newOutline += "</TD><TD VALIGN=TOP>" + childstylestart;
			}
			else { 
				newOutline += "<TD VALIGN=TOP COLSPAN=2>" + parentstylestart;
				newOutline += "<A HREF=\"javascript:history.go(0)\" " + "onMouseOver=\"window.status=\'" + theGIFStatus + 
				"\';return true;\" onClick=\"alloffbutone(" + i + ");return " + (theGIF != endpointWidget) + "\">"
				newOutline += theGIF + "</A>";
			}		
			if (db[i].URL == "" || db[i].URL == null || document.location.href.indexOf(db[i].URL) != -1) {
				newOutline += " " + db[i].display + "<BR>"	// no link	
			} else {
				linkcolor = ( currIndent != 0 || theGIF == expandedWidget ? "#996633" : "#cc9966" );
				newOutline += " <A STYLE=\"color:" + linkcolor + "; text-decoration:none\" HREF=\"" + db[i].URL + "\" TARGET=\"" + 
					displayTarget + "\" onMouseOver=\"window.status=\'" + 
					db[i].statusMsg + "\';return true;\""
				if (currIndent == 0) {
					newOutline += " onClick=\"alloffbutone(" + i + "); return true;\""
				}
				newOutline += ">" + db[i].display + 
					"</A><BR>"
			}
			if (currIndent == 0) { newOutline += parentstyleend; }
			else { newOutline += childstyleend; }
			newOutline += "</TD></TR>\n";
			prevIndentDisplayed = currIndent
			showMyDaughter = expanded
			if (db.length > 25) {
				document.write(newOutline)
				newOutline = ""
			}
		}
	}
	document.write("<table width=100%>\n")
	document.write(newOutline)
	document.write("</table>\n")
}
