// Floating Menu Builder
// Created by RJS, MMWeb

// 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"
	} else if (this.type=="nn") {
		this.hiddenref = "hide"
		this.visibleref = "show"
		this.styleref = ""
	}	
	return this
}

function mnuLevel(expanded,mstyle,bstyle,left,top) {
	this.expanded = expanded
	this.indent = currindent
	this.left = left
	this.top = top
	this.width = null
	this.height = null
	mstyle=="" ? this.mstyle = menustyle : this.mstyle = mstyle
	bstyle=="" ? this.bstyle = backstyle : this.bstyle = bstyle
	this.item = curritem
	this.entered = false
	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(expanded,mstyle,bstyle,left,top) {
	currindent++
	currlevel = mnuLevels.length
	mnuLevels[currlevel] = new mnuLevel(expanded,mstyle,bstyle,left,top)
	if (curritem!=null) mnuItems[curritem].child = currlevel
}

function additem(label,link,target) {
	curritem = mnuItems.length
	mnuItems[curritem] = new mnuItem(label,link,target)
}

function endlevel() {
	currindent--
	curritem = null
	for (var i=currlevel-1;i>-1;i--) {
		if (mnuLevels[i].indent==currindent) {
			currlevel=i
			break
		}
	}
}

function getLayer(l) {
	if (brow.type=="nn" && brow.ver>=5) return document.getElementById("mnu"+l)
	else if (brow.type=="nn") return document.layers["mnu"+l]
	else return document.all("mnu"+l)
}

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 thislev = null
var layerdoc = null
var str=""
var isloadingcomplete = false
var timerid = null

function buildmenu() {
	curritem = mnuItems.length - 1
	currlevel = mnuLevels.length
	buildmenulayer()
}

function buildmenulayer() {
	thislev = mnuLevels[levelct]
	var nextlev = mnuLevels[levelct+1]

	// Build layer
	str = "<table cellspacing=\"" + rowspacing + "\" cellpadding=\"" + rowpadding + "\" border=\"" + 
		bordersize + "\" bordercolor=\"" + bordercolor + "\">\n"
	for (thisitem=0;thisitem<=curritem;thisitem++) {
		thismenu = mnuItems[thisitem]
		if (thismenu.level==levelct) {
			str += "<tr><td nowrap"
			if (thislev.bstyle!="") str += " style=\"" + thislev.bstyle + "\""
			str += ">"
			idstr = "id"
			if (brow.type=="nn" && brow.ver<5) idstr = "name"
			str += "<a " + idstr + "=\"mnur" + thismenu.child + "\" href=\""
			thismenu.link!="" ? str += thismenu.link + "\" " + thismenu.target : str += "#\" onclick=\"return false\""
			str += " onmouseover=\"togglemenu("+levelct + "," + thismenu.child+")\">"
			str += "<font "
			thislev.mstyle!="" ? str += " style=\"" + thislev.mstyle + "\">" : str += ">"
			str +=  thismenu.label
			str += "</font></a></td></tr>\n"
		}
	}
	str += "</table>\n"
		
	// 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(0,0)
		layerdoc.visibility = "hide"
		layerdoc.zIndex = thislev.indent
		layerdoc.document.open()
		layerdoc.document.write(str)
		layerdoc.document.close()
	} else {
		// IE 4
		if (brow.ver<5) {
			str = "<div id=\"mnu"+levelct+"\" style=\"position:absolute; visibility:hidden; top:0; left:0; " + 
				"z-index:" + thislev.indent + "\">" + str + "</div>"
			document.body.insertAdjacentHTML("afterBegin",str)
			layerdoc = getLayer(levelct)


		// IE5+,NN5+
		} else {
			layerdoc = document.createElement("DIV")
			document.body.appendChild(layerdoc)
			with (layerdoc) {
				id = "mnu"+levelct
				innerHTML = str
				
			}
		}
	}
	continuebuild()
}

function continuebuild() {
	// Test to see if layer is loaded yet
	var thisheight=0
	var thiswidth=0
	var tryagain=false
	if (brow.type=="nn" && brow.ver<5) {
		thislev.height = thisheight = layerdoc.clip.height
		thislev.width = thiswidth = layerdoc.clip.width
	} else {
		// IE 4
		if (brow.type=="ie" && brow.ver<5) {
			// This is really ugly, but it catches delays
			for (var z=0;z<document.images.length;z++) {
				var tmpImg = document.images[z]
				if (tmpImg.parentElement.id.indexOf("mnur")==0 && !tmpImg.complete) {
					tryagain=true
					break
				}
			}

			// Retrieve table size
			tryagain = true
			if (layerdoc.children) {
				var tab = layerdoc.children[0]
				thislev.height = thisheight = tab.offsetHeight
				thislev.width = thiswidth = tab.offsetWidth
				tryagain = false
			}

		} else {
			// IE5+
			if (brow.type=="ie" && layerdoc.readyState=="loading") tryagain = true

			// NN6
			else with (layerdoc) {
				style.zIndex = thislev.indent
				style.position = "absolute"
				style.visibility = "hidden"
			}
			thislev.height = thisheight = layerdoc.offsetHeight
			thislev.width = thiswidth = layerdoc.offsetWidth
		}
	}
	if (thisheight==0 || thiswidth==0 || tryagain) {
		timerid = setTimeout("continuebuild()",10)
		return
	}
	clearTimeout(timerid)

	// Determine top and left
	var thisdoc
	if (!thislev.left) {
		if (levelct==0) thislev.left = menuleft
		else if (thislev.indent==1) thislev.left = getleft(mnuItems[thislev.item].label)    // CUSTOMIZATION
		else {
			thisdoc = mnuLevels[mnuItems[thislev.item].level]
			thislev.left = thisdoc.left + thisdoc.width + offsetwidth
		}
	}
	var thistop=0
	if (!thislev.top) {
		if (levelct==0) thislev.top = menutop
		else if (thislev.indent==1) thislev.top = menutop		   // CUSTOMIZATION
		else {
			var thisanc
			if (brow.type=="nn" && brow.ver>=5) {
				thisanc = document.getElementById("mnur"+levelct)
				thistop = thisanc.offsetTop
			} else if (brow.type=="nn") {
				thisdoc = document.layers["mnu"+mnuItems[thislev.item].level]
				thisanc = thisdoc.document.anchors["mnur"+levelct]
				thistop = thisdoc.y + thisanc.y
			} else {
				thisanc = document.all("mnur"+levelct)
				while (thisanc) {
					thistop += thisanc.offsetTop
					thisanc = thisanc.offsetParent
				}
			}
			thislev.top = thistop + offsettop
		}
	}

	// Increase pointer and move on
	
		// Increase pointer and move on
	eval("layerdoc" + brow.styleref + ".zIndex=\"" +
(100+thislev.indent) + "\"")
	eval("layerdoc" + brow.styleref + ".top=\"" + thislev.top + "\"")
	eval("layerdoc" + brow.styleref + ".left=\"" + thislev.left + "\"")	
	if (levelct>0 || (levelct==0 && !hidebase)) eval("layerdoc" + brow.styleref + ".visibility=\"" + getVisibility(thislev.expanded) + "\"")
	levelct++
	if (levelct<currlevel) return buildmenulayer()	

	// For Netscape, correct reloading and capture mice
	if (brow.type=="nn" && brow.ver<5) {
		window.captureEvents (Event.RESIZE)
		document.captureEvents (Event.MOUSEMOVE)
	}
	window.onresize = reloadonresize
	document.onmousemove = testposition

	// Enabled
	isloadingcomplete = true
}

function reloadonresize() {
	window.location.reload(true)
}

function togglemenu(l,i) {
	if (!isloadingcomplete) return
	if (i==null) i=l
	var thislev = mnuLevels[i]
	thislev.expanded = true
	for (var j=0;j<currlevel;j++) {
		var lev = mnuLevels[j]
		if (lev.indent>=thislev.indent && j!=i) {
			lev.expanded = false
			lev.entered = false
		}
		var layerdoc = getLayer(j)
		if (j>0 || (j==0 && (!hidebase))) 
			eval("layerdoc" + brow.styleref + ".visibility=\"" + getVisibility(lev.expanded) + "\"")
	}

}

function togglespecial(n) {
	for (var i=0;i<=curritem;i++) {
		if (mnuItems[i].label==n) return togglemenu(0,mnuItems[i].child)
	}
	return togglemenu(0,null)
}

function testposition(e) {
	var cx = 0
	var cy = 0
	if (brow.type=="nn") {
		cx = e.pageX ; cy = e.pageY
	} else {
		cx = event.clientX + document.body.scrollLeft
		cy = event.clientY + document.body.scrollTop
	}
	var lev
	var itm
	var bottom=-1
	var layerdoc
	var oktoclose
	var onlayer
	if (hidebase) bottom=0
	// Walk from children to parents 	
	for (var i=currlevel-1;i>bottom;i--) {
		lev = mnuLevels[i]
		if (lev.expanded) {
			(cx>=lev.left && cx<=lev.left+lev.width && cy>=lev.top && cy<=lev.top+lev.height) ? onlayer=true : onlayer=false
			if (onlayer) lev.entered=true
			if (lev.entered && !onlayer) {
				oktoclose = true
				for (var j=0;j<=curritem;j++) {
					itm = mnuItems[j]
					if (itm.level==i && itm.child!=null) if (mnuLevels[itm.child].expanded) {
						oktoclose = false
						break
					}
				}
				if (oktoclose) {
					lev.entered = false
					lev.expanded = false
					layerdoc = getLayer(i)
					eval("layerdoc" + brow.styleref + ".visibility=\"" + getVisibility(false) + "\"")
				}
			}
		}
		
	}
}


// Declarations
var brow = new browser
var mnuLevels = new Array()
var mnuItems = new Array()
var currlevel = null
var curritem = null
var currindent = -1
var offsettop = -10
var offsetwidth = -10
var menuleft = 0
var menutop = 112
var menustyle = "font-family:Verdana,sans-serif;font-size:xx-small;color:#ffffff"
if (brow.type=="nn") menustyle = "font-family:Verdana,sans-serif;font-size:x-small;color:#ffffff"  // Adjust for Netscape
var backstyle = ""
var rowspacing = 0
var rowpadding = 3
var bordersize = 1
var bordercolor = "#999999"
var hidebase = true


// -----------------------------------------------
//  START CUSTOMIZATIONS

// Setup to run on window load
//window.onload = buildmenu

function getleft(img) {
	// Get reference to images document
	if (brow.type=="nn" && brow.ver<5) var doc = document.navmenu.document
	else var doc = document
	var imgleft = 0
	var obj = doc.images[img]
	if (obj) {
		if (brow.type=="nn" && brow.ver<5) imgleft = obj.x
		else {
			while (obj) {
				imgleft += obj.offsetLeft
				obj = obj.offsetParent
			}
		}
	}
	return imgleft
}

function repositionmenus() {
	for (var i=0;i<currlevel;i++) {
		var layerdoc = getLayer(i)
		var newtop = navmenutop + mnuLevels[i].top
		eval("layerdoc" + brow.styleref + ".top=\"" + newtop + "\"")
	}
}
//  END CUSTOMIZATIONS
// -----------------------------------------------
