// ---------------------------------
//
// Outline-style Menu Builder
//
// Created by RJS, MMWeb
//
// ---------------------------------
// Version history:
//  3/28: RJS: Added style tags to prevent menus from showing
//    when printing in IE,NN6
//
// ---------------------------------

// Constructors
function browser() {
	this.type = ""
	var app = navigator.appName
	var strver = navigator.appVersion
	if (app.indexOf("Netscape")>-1) {
		this.type = "nn"
		this.ver = parseInt(strver.substring(0,1))
	} else if (app.indexOf("Internet Explorer")>-1) {
		this.type = "ie"
		this.ver = parseInt(strver.charAt(strver.indexOf("MSIE")+5))
	}
	this.os = ""
	var agt = navigator.userAgent
	if (agt.indexOf("Mac")>-1) this.os = "mac"
	else if (agt.indexOf("Win")>-1) this.os = "win"
	
	// Use settings
	if (this.type=="ie" || (this.type=="nn" && this.ver>=5)) {
		this.hiddenref = "hidden"
		this.visibleref = "visible"
		this.styleref = ".style"

		// Add style to hide when printing
		document.writeln("<style media=\"print\">")
		document.writeln("div.menus { display:none; }")
		document.writeln(".shrinktable { width:600 }")
		document.writeln("</style>")
		
	} else if (this.type=="nn") {
		this.hiddenref = "hide"
		this.visibleref = "show"
		this.styleref = ""
	}
	return this
}

function mnuLevel(visible,mstyle,bstyle) {
	this.item = curritem
	this.visible = visible
	this.indent = currindent
	this.left = menuleft + (currindent * menuindent)
	this.top = 0
	this.height = 0
	mstyle=="" ? this.mstyle = menustyle : this.mstyle = mstyle
	bstyle=="" ? this.bstyle = backstyle : this.bstyle = bstyle
	return this
}

function mnuItem(label,link,target) {
	this.level = currlevel
	label=="" ? this.label = "" : this.label = label
	link==""  ? this.link = "" : this.link = link
	target==""  ? this.target="" : this.target = " target=\"" + target + "\""
	this.child = null
	return this
}

// Functions
function beginlevel(visible,mstyle,bstyle) {
	currindent++
	currlevel = mnuLevels.length
	mnuLevels[currlevel] = new mnuLevel(visible,mstyle,bstyle)

	// Advise last item it has children
	if (levelstarted && curritem!=null) mnuItems[curritem].child = currlevel

	// If previous level has ended, find sibling and copy info to this level
	if (!levelstarted) {
		levelstarted = true
		for (i=currlevel-1;i>-1;i--) {
			if (mnuLevels[i].indent==currindent) {
				siblev = mnuLevels[i]
				with (mnuLevels[currlevel]) {
					item = siblev.item
					visible = siblev.visible
					left = siblev.left
					mstyle = siblev.mstyle
					bstyle = siblev.bstyle
				}
				break
			}
		}		
		
	}

}

function additem(label,link,target) {
	if (!levelstarted) {
		currindent--
		beginlevel(null,null,null)
	}
	curritem = mnuItems.length
	mnuItems[curritem] = new mnuItem(label,link,target)
}

function endlevel() {
	currindent--
	levelstarted = false
}

function getLayer(l) {
	if (brow.type=="nn" && brow.ver>=5) return document.getElementById(l)
	else if (brow.type=="nn") return document.layers[l]
	else return document.all(l)
}

function getImageLayer(l) {
	if (brow.type=="nn" && brow.ver<5) return document.layers["mnu"+l].document
	else return document
}

function getVisibility(v) {
	if (v) return brow.visibleref
	else return brow.hiddenref
}

// Global variables for menu drawing only
var levelct = 0
var thisitem = 0
var menuheight = 0
var totalheight = 0
var thislev = null
var layerdoc = null

function buildmenu() {
	// // Determine coordinates of base if specified
	getbasecoords()

	// Set up menu
	clearTimeout(imgtimer)
	currlevel = mnuLevels.length
	curritem = mnuItems.length
	menuheight = basetop + menutop
	totalheight = menuheight
	buildmenulayer()
}

function buildmenulayer() {
	thislev = mnuLevels[levelct]
	var childlev

	// Build layer
	var str = "<table border=" + bordersize + " bordercolor=" + bordercolor + 
		" cellspacing=" + rowspacing + " cellpadding=" + rowpadding + ">\n"
	while (thisitem<curritem && mnuItems[thisitem].level==levelct) {
		thismenu = mnuItems[thisitem]
		str += "<tr><td align=center valign=top width=" + menuindent + ">"
		if (thismenu.child!=null) {
			str += "<a href=# onclick=\"return togglemenu("+thismenu.child+")\">"
			str += "<img name=\"mnui" + thisitem + "\" border=0" 
			str += " width=" + imgwidth + " height=" + imgheight + " src=\""
			childlev = mnuLevels[thismenu.child]
			childlev.visible ? str += menuminus : str += menuplus
			str += "\"></a>"
		}
		str += "</td><td valign=top"
		if (maxwidth>0) str += " width=" + (maxwidth-thislev.left-menuindent-(3*rowspacing))
		if (thislev.bstyle!="") str += " style=\"" + thislev.bstyle + "\""
		str += "><font"
		thislev.mstyle!="" ? str += " style=\"" + thislev.mstyle + "\">" : str += ">"
		if (thismenu.link!="") str += "<a href=\"" + thismenu.link + "\" " + thismenu.target + ">"
		str += thismenu.label
		if (thismenu.link!="") str += "</a>"
		str += "</font></td></tr>\n"
		thisitem++
	}
	str += "</table>\n"
	
	//
	thislev.left += baseleft
		
	// Draw layer
	// Netscape 4
	if (brow.type=="nn" && brow.ver<5) {
		document.layers["mnu"+levelct] = new Layer(window.innerWidth)
		layerdoc = document.layers["mnu"+levelct]
		layerdoc.moveTo(thislev.left,menuheight)
		layerdoc.visibility = "hide"
		layerdoc.document.open()
		layerdoc.document.write(str)
		layerdoc.document.close()

	// IE 4
	} else if (brow.ver<5){
		str = "<div id=\"mnu"+levelct+"\" style=\"position:absolute;top:"+
			menuheight+";left:"+thislev.left+";visibility:hidden\">" + str + "</div>"
		document.body.insertAdjacentHTML("BeforeEnd",str)
		layerdoc = getLayer("mnu"+levelct)

	// IE5+,NN5+
	} else {
		layerdoc = document.createElement("DIV")	
		document.body.appendChild(layerdoc)
		with (layerdoc) {
			id = "mnu"+levelct
			className = "menus"
			style.position = "absolute"
			style.left = thislev.left
			style.visibility = "hidden"
			innerHTML = str
		}
	}
	continuebuild()
}

function continuebuild() {
	// Test to see if layer is loaded yet
	var thisheight=0
	if (brow.type=="nn" && brow.ver<5) thisheight = layerdoc.clip.height
	else thisheight = layerdoc.offsetHeight
	if (thisheight!=0) thislev.height = thisheight
	else return setTimeout("continuebuild()",25)

	// Increase pointer and move on
	thislev.top = menuheight
	eval("layerdoc" + brow.styleref + ".top=\"" + menuheight + "\"")
	eval("layerdoc" + brow.styleref + ".visibility=\"" + getVisibility(thislev.visible) + "\"")
	if (thislev.visible) menuheight += thisheight
	totalheight += thisheight
	levelct++
	if (levelct<currlevel) return buildmenulayer()

	// If footer exists
	if (hasfooter) positionfooter()

	// If synching
	if (autosync) syncitem()

	// For Netscape, correct page height and reloading
	if (brow.type=="nn" && brow.ver<5) {
		if (document.height<totalheight) document.height=totalheight
		window.captureEvents (Event.RESIZE)
	}
	window.onresize = reloadonresize
}

function reloadonresize() {
	window.location.reload(true)
}

function togglemenu(i) {
	thislev = mnuLevels[i]
	thislev.visible = !thislev.visible
	drawmenu(i)
	return false
}

function drawmenu(i) {
	var thislev = mnuLevels[i]
	menuheight = thislev.top
	var inmysection = true
	var layerdoc
	var imglayer
	var lev
	for (j=i;j<currlevel;j++) {
		lev = mnuLevels[j]
		lev.top = menuheight
		if (lev.indent<thislev.indent) inmysection=false
		if (inmysection) {
			if (thislev.visible && lev.indent==thislev.indent) lev.visible = true
			else if (!thislev.visible && lev.indent>=thislev.indent) lev.visible = false
		} 
		if (lev.item!=null) {
			lev.visible ? ex = menuminus : ex = menuplus
			imglayer = getImageLayer(mnuItems[lev.item].level)
			imglayer.images["mnui"+lev.item].src=ex
		}
		layerdoc = getLayer("mnu"+j)
		eval("layerdoc" + brow.styleref + ".top=\"" + menuheight + "\"")
		eval("layerdoc" + brow.styleref + ".visibility=\"" + getVisibility(lev.visible) + "\"")
		if (lev.visible) menuheight += lev.height
	}

	// Reposition footer
	if (hasfooter) positionfooter()
}

function positionfooter() {
	var thisheight = 0
	var pageend = getLayer("mnuend")
	var footer = getLayer("mnufoot")
	eval("footer" + brow.styleref + ".visibility=\"" + getVisibility(true) + "\"")
	if (brow.type=="nn" && brow.ver<5) {
		if (menuheight>pageend.pageY) {
			footer.pageY = menuheight
			totalheight += footer.clip.height
		} else {
			footer.pageY=pageend.pageY
			totalheight = footer.pageY + footer.clip.height
		}			
	} else {
		if (brow.type=="ie") {
			pe = pageend
			while (pe) {
				thisheight += pe.offsetTop
				pe = pe.offsetParent
			}
		} else thisheight = pageend.offsetTop
		if (menuheight>thisheight) {
			footer.style.top = menuheight
			totalheight += footer.offsetHeight
		} else {
			footer.style.top = thisheight
			totalheight = footer.style.top + thisheight
		}
	}
}

function syncitem() {
	// Locate current page in menu
	var l = window.location.href
	var thisitem
	var lev
	var levindent
	for (i=0;i<curritem;i++) {
		thisitem = mnuItems[i]
		if (l.indexOf(thisitem.link)==l.length-thisitem.link.length) {
			// Found matching item -- see if it has children
			lev = thisitem.level
			if (lev<currlevel-1) if (mnuLevels[lev+1].item==i) lev = lev + 1
			levindent = mnuLevels[lev].indent
			// Move backward and update ancestors
			for (j=lev;j>-1;j--) {
				if (mnuLevels[j].indent<=levindent) {
					mnuLevels[j].visible = true
					levindent = mnuLevels[j].indent
				}
			}
			// Move forward and update peers and children
			levindent = mnuLevels[lev].indent
			for (j=lev+1;j<currlevel;j++) {
				if (mnuLevels[j].indent<=levindent) {
					mnuLevels[j].visible = true
					levindent = mnuLevels[j].indent
				}
			}					
			drawmenu(0)
			return
		}
	}
}

function waitforimages() {
	// Change images if they have been changed in configuration
	if (menuminus!=oldminus || menuplus!=oldplus) {	
		imgminus = null; imgplus = null
		imgminus = new Image() ; imgplus = new Image()
		imgminus.src = menuminus; imgplus.src = menuplus
	} else {
		imgtimer = setTimeout("waitforimages()",50)
	}
}

function getbasecoords() {
	// Get reference to base document
	var doc = getLayer(baselayer)
	if (doc) {
		if (brow.type=="nn" && brow.ver<5) {
			baseleft = doc.pageX
			basetop = doc.pageY
		} else while (doc) {
			baseleft += doc.offsetLeft
			basetop += doc.offsetTop
			doc = doc.offsetParent
		}
	}
}


// Declarations
var brow = new browser
var mnuLevels = new Array()
var mnuItems = new Array()
var levelstarted = false
var currlevel = null
var curritem = null
var currindent = -1
var maxwidth = 0
var menuindent = 12
var menuleft = 10
var menutop = 10
var menustyle = "font-family:Arial,Helvetica; font-size:12pt"
var backstyle = ""
var rowspacing = 1
var rowpadding = 1
var bordersize = 0
var bordercolor = ""
var imgminus = new Image()
var imgplus = new Image()
var menuminus = "_menu/minus.gif" ; var oldminus = menuminus
var menuplus = "_menu/plus.gif"; var oldplus = menuplus
imgminus.src = menuminus ; imgplus.src = menuplus
var imgwidth = 7 ; var imgheight = 7
var imgtimer = null
var hasfooter = false
var autosync = true
var baselayer = ""
var baseleft = 0
var basetop = 0

// -----------------------------------------------------
// Defaults for Self-Help
menuminus = "/_menu/tminus.gif"				// Override standard icons
menuplus = "/_menu/tplus.gif"
var imgwidth = 11 ; var imgheight = 11			// Override standard icon dimensions
maxwidth = 425						// set maximum width of menu
menuindent = 20						// amount to indent each level
menustyle = ""
hasfooter = true
baselayer = "mnuanch"
menuleft = 0
menutop = 0
// -----------------------------------------------------

// Get new images as soon as specified
waitforimages()



