document.onmouseup = function () { clearMenus(); }
var g_timer

// call this in your onload function
function initPulldown() {
  // insert 2 divs for 2 levels of pulldowns
  document.body.insertAdjacentHTML("AfterBegin","<DIV style='position:absolute' id='divpulldown1'></DIV>")
  document.body.insertAdjacentHTML("AfterBegin","<DIV style='position:absolute' id='divpulldown2'></DIV>")
}


function getAbsoluteLeft(oNode){
	var oCurrentNode=oNode;
	var iLeft=0;
	while(oCurrentNode.offsetParent){
		iLeft+=oCurrentNode.offsetLeft;
		oCurrentNode=oCurrentNode.offsetParent;
	}
	return (iLeft + oCurrentNode.offsetLeft)
}

function getAbsoluteTop(oNode){
	var oCurrentNode=oNode;
	var iTop=0;
	while(oCurrentNode.offsetParent){
		iTop+=oCurrentNode.offsetTop;
		oCurrentNode=oCurrentNode.offsetParent;
	}
	return (iTop + oCurrentNode.offsetTop)
}

function getAbsoluteMiddle(oNode) {
	return ((oNode.offsetWidth / 2) + getAbsoluteLeft(oNode))
}

function getAbsoluteBottom(oNode) {
	return (oNode.offsetHeight + getAbsoluteTop(oNode))
}

function highlightTD (p_td) {
	p_td.className = "pulldowntdhighlight"
	stopClearMenuTimer()
}

function normalTD (p_td) {
	p_td.className = "pulldowntd"
	resetClearMenuTimer()
}

function clearMenus() {
  if (document.getElementById('divpulldown1')) {
    document.getElementById('divpulldown1').style.visibility = 'hidden'
    document.getElementById('divpulldown2').style.visibility = 'hidden'
  }
}

function stopClearMenuTimer() {
  clearTimeout(g_timer)
}

function resetClearMenuTimer() {
  clearTimeout(g_timer)
  g_timer = setTimeout("clearMenus()", 200)
}

function renderMenu(p_nMenu, p_nMenuSub, p_x, p_y) {

  if (document.getElementById('divpulldown1')) {
    stopClearMenuTimer()

    var l_arrMenu = null

    if (p_nMenuSub < 0) {
      document.getElementById('divpulldown2').style.visibility = 'hidden'
      l_arrMenu = g_arrMenu[p_nMenu][2]
    } else {
      l_arrMenu = g_arrMenu[p_nMenu][2][p_nMenuSub][2]
    }

    var l_strHTML = ""

    if (l_arrMenu.length) {
		var l_index = 0
		l_strHTML = "<table class='pulldowntable'>"
		while (l_arrMenu[l_index]) {
			l_strHTML = l_strHTML + "<tr><td class='pulldowntd'  onmouseout='normalTD(this);' onmouseup='document.location=unescape(\"" + l_arrMenu[l_index][0] + "\")' onmouseover='highlightTD(this);"
			if (p_nMenuSub < 0) {
				l_strHTML = l_strHTML + "renderMenu("+ p_nMenu +", "+ l_index +", getAbsoluteLeft(this) + this.offsetWidth, getAbsoluteTop(this));'"
			} else {
				l_strHTML = l_strHTML + "'"
			}
			l_strHTML = l_strHTML + ">"+ l_arrMenu[l_index][1] +"</td></tr>"
			l_index++
		}

	// AM: TEMP: added for COTA pulldown
	//      l_strHTML = l_strHTML + "<tr><td><img src='images/spacer.gif' width='120' height=1><BR><img src='images/pulldown_bottom.gif' width='100%' height=11></td></tr>"

		l_strHTML = l_strHTML + "</table>"
	}

    if (p_nMenuSub < 0) {
		// second level menu
		document.getElementById('divpulldown1').style.visibility = 'visible'
		document.getElementById('divpulldown1').innerHTML = l_strHTML

		// FF won't accept left or top if we don't add the f#$%#$ 'px' part...  ARGH!  Good ol Netscrap...
		document.getElementById('divpulldown1').style.left = p_x + 'px'
		document.getElementById('divpulldown1').style.top = p_y + 'px'
    } else {
		// first level menu
		document.getElementById('divpulldown2').style.visibility = 'visible'
		document.getElementById('divpulldown2').innerHTML = l_strHTML
		
		// make sure we give it all the room it needs to render the table
		// before we check to see if it won't fit on the screen
		document.getElementById('divpulldown2').style.left = '0 px'
		document.getElementById('divpulldown2').style.top = p_y + 'px'
		
		if ((p_x + document.getElementById('divpulldown2').offsetWidth > document.body.offsetWidth) && (document.getElementById('divpulldown2').offsetWidth < p_x)) {
			document.getElementById('divpulldown2').style.left = (p_x - document.getElementById('divpulldown2').offsetWidth - document.getElementById('divpulldown1').offsetWidth) + 'px'
		} else {
			document.getElementById('divpulldown2').style.left = p_x
		}
    }
  }
}


