/*
links to use these menu functions should read:

<a href="whatever" 
	onMouseOver="showmenu(event, linkset['9'], this, 2); return true;" 
	onMouseOut="delayhidemenu(); " 
	id="SOMEID">
	LINK_TEXT</a>

*/

var ie4 = document.all;
var ns6 = document.getElementById && !document.all;
var ns4 = document.layers;
var imTemp = '';
var dmIgnoreShineThrough = false;
// var DM = document.getElementById('debugMsgASDF')

//________________________________________________________________________________

function showmenu(e, which, sourceObj, elementID, targetMenuObj, arbitraryTop, arbitraryLeft){

	/*
	e			= event
	which 		= selectedMenu code.  If this is an object, pass in sourceObj.innerHTML
	sourceObj 	= the object the menu will be positioned relative to
	elementID 	= an extra bit that changes the top of the menu
	*/
	

	if(!targetMenuObj) targetMenuObj = 'popmenu'
	
	if (!document.all && !document.getElementById && !document.layers){
		// if the browser doesn't support ANY of this,
		// just exit without doing anything
		return;
	}
	
	if(!which) {
		// if the selected menu doesn't exist...
		return;
	}
	
	// reset the timer object that hides the menu
	clearhidemenu()
	
	// this, just to get around a bug in MSIE 6 and earlier that allows 
	// dropdowns to "shine through"
  	toggleSelectsUnderMenus('off')

	// setup some defaults
	var wmLeft = 0
	var wmTop = 0
	var lastTop = -1
    var lastLeft = 0
	
	// figure relative position of the source object
	var sourceParent = sourceObj.parentNode
	while (sourceParent){
		if(sourceParent.offsetLeft){
        	// 9/20/07 In firefox, offsetLefts just pile up and the menu goes in too far
            // I'm fighting that by checking for a new offsetLeft value, and not adding
            // unless I get one. 
        	wmLeft += lastLeft != sourceParent.offsetLeft ? sourceParent.offsetLeft : 0
            lastLeft = sourceParent.offsetLeft
		}
		if(sourceParent.offsetTop){
			if(sourceParent.offsetTop != lastTop) {
				lastTop = sourceParent.offsetTop
				wmTop += sourceParent.offsetTop
			}
		}
		sourceParent = sourceParent.parentNode
	}

	// make some arbitrary changes for the menu bars
	if(elementID == 4 || elementID == 5){
		// left menu bars arbitrarily set the left property
		wmLeft += sourceObj.parentNode.offsetWidth;
	} else {
		// top menu bars arbitrarily set the top property
		wmTop += sourceObj.parentNode.offsetHeight
	}
	// menuObj = ie4? document.all.popmenu : ns6? document.getElementById("popmenu") : ns4? document.popmenu : ""
	menuObj = ie4? eval('document.all.' + targetMenuObj) : ns6? eval('document.getElementById("' + targetMenuObj + '")') : ns4? eval('document.' + targetMenuObj) : ""
	menuObj.theStyle = (ie4||ns6)? menuObj.style : menuObj

	if (ie4 || ns6) {
		menuObj.innerHTML = which
	} else {
		imTemp = '<layer name=gui bgColor=#E6E6E6 width=<%= 200%> onmouseover="clearhidemenu()"';
		imTemp += ' onmouseout="hidemenu()">' + which + '</layer>'
		menuObj.document.write(imTemp)
		menuObj.document.close()
	}
	
	if(ie4){
		var tbr1 = document.getElementById('mainToolbar')
		// wmTop = tbr1.offsetTop + tbr1.offsetHeight + tbr1.parentNode.offsetTop
	}

    
    
	menuObj.contentwidth = (ie4||ns6)? menuObj.offsetWidth : menuObj.document.gui.document.width
	menuObj.contentheight = (ie4||ns6)? menuObj.offsetHeight : menuObj.document.gui.document.height
	
	eventX = ie4? event.clientX : ns6? e.clientX : e.x
	eventY = ie4? event.clientY : ns6? e.clientY : e.y

	// eventY = tbr1.offsetHeight + tbr1.offsetTop //ie4? event.clientY : ns6? e.clientY : e.y
	
	//Find out how close the mouse is to the corner of the window
	var rightedge = ie4? document.body.clientWidth-eventX : window.innerWidth-eventX
	var bottomedge = ie4? document.body.clientHeight-eventY : window.innerHeight-eventY

	//if the horizontal distance isn't enough to accomodate the width of the context menu
	if (rightedge < menuObj.contentwidth){
		//move the horizontal position of the menu to the left by it's width
		menuObj.theStyle.left = ie4? document.body.scrollLeft+eventX-menuObj.contentwidth : ns6? window.pageXOffset+eventX-menuObj.contentwidth : eventX-menuObj.contentwidth
	}
	else {
		//position the horizontal position of the menu where the mouse was clicked
		// menuObj.theStyle.left = ie4? document.body.scrollLeft+eventX : ns6? window.pageXOffset + eventX : eventX
		// menuObj.theStyle.left = ie4? wmLeft : ns6? eventX : sourceObj.parentNode.offsetLeft
		menuObj.theStyle.left = ie4 || ns6 ? wmLeft : sourceObj.parentNode.offsetLeft
	}

	//same concept with the vertical position
	if (bottomedge < menuObj.contentheight){
		// menuObj.theStyle.top = ie4? document.body.scrollTop+eventY-menuObj.contentheight 
		// : ns6? window.pageYOffset+eventY-menuObj.contentheight : eventY-menuObj.contentheight
		if (ie4) {
			menuObj.theStyle.top = document.body.scrollTop + tbr1.offsetHeight + menuObj.contentheight
		} else if (ns6) {
			menuObj.theStyle.top = window.pageYOffset+eventY-menuObj.contentheight
		} else {
			menuObj.theStyle.top = eventY - menuObj.contentheight
		}
		// menuObj.theStyle.top = ie4? document.body.scrollTop + tbr1.offsetHeight + menuObj.contentheight 
		// : ns6? window.pageYOffset+eventY-menuObj.contentheight : eventY-menuObj.contentheight
	}
	else {
		menuObj.theStyle.top = (ie4) ? document.body.scrollTop + event.clientY : ns6? window.pageYOffset+eventY : eventY
		// menuObj.theStyle.top = ie4? tbr1.offsetHeight + tbr1.parentNode.offsetTop : ns6? window.pageYOffset+eventY : eventY
		// menuObj.theStyle.top = wmTop //ie4? wmTop : ns6? tbr1.offsetHeight + tbr1.parentNode.offsetTop : eventY
		menuObj.theStyle.visibility = "visible"
		// return false
	}
	
	// for the top menu bar, override the script and put the menu 
	// at the bottom edge of the element
	// where the event happened.
	
	if(arbitraryTop) wmTop = arbitraryTop;
	
	if(arbitraryLeft) menuObj.theStyle.left = arbitraryLeft;
	
	
	if(menuObj.offsetWidth < 32) menuObj.style.width = 200;
	
	if(elementID == 2 || elementID == 0) menuObj.theStyle.top = wmTop
	
	if(document.getElementById('menuTest')) document.getElementById('menuTest').innerHTML = 'left: ' + menuObj.offsetLeft;
		
}

// ______________________________________________________________________________

function contains_ns6(a, b) {
//Determines if 1 element in contained in another- by Brainjar.com
while (b.parentNode)
	if ((b = b.parentNode) == a)
		return true;
return false;
}

//________________________________________________________________________________

function hidemenu(){
	if (window.menuObj){
		menuObj.theStyle.visibility = (ie4||ns6)? "hidden" : "hide"
	    toggleSelectsUnderMenus('on')
	}
}

//________________________________________________________________________________

function dynamichide(e){
	if (ie4&&!menuObj.contains(e.toElement))
		hidemenu()
	else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
		hidemenu()
}

//________________________________________________________________________________

function delayhidemenu(){
	if (ie4||ns6||ns4) {
		delayhide = setTimeout("hidemenu()",500)
	}
}

//________________________________________________________________________________

function clearhidemenu(){
	if (window.delayhide)
		clearTimeout(delayhide)
}


//________________________________________________________________________________

function highlightmenu(e, state, targetMenuObj){

	if(!targetMenuObj) targetMenuObj = 'popmenu'

	if (document.all)
		source_el = event.srcElement
	else if (document.getElementById)
		source_el = e.target
		
	if (source_el.className == "menuitems"){
		source_el.id = (state == "on")? "mouseoverstyle" : ""
	}
	else{
		while(source_el.id!= targetMenuObj){
			source_el = document.getElementById? source_el.parentNode : source_el.parentElement
			if (source_el.className == "menuitems"){
				source_el.id = (state == "on")? "mouseoverstyle" : ""
			}
		}
	}
}

if (ie4||ns6){
	document.onclick = hidemenu
}


//------------------------------------------------------------

	function toggleSelectsUnderMenus(st){
	
		if(dmIgnoreShineThrough) return;
	
    	var ctl = document.getElementsByTagName('SELECT')
        for(var i = 0; i < ctl.length; i++){
        	if(ctl[i].getAttribute('hideOnMenu') == '1'){
            	if(st == 'on' || st == 'show') {
	            	ctl[i].style.visibility = 'visible';
                } else {
	            	ctl[i].style.visibility = 'hidden';
                }
            }
        }
    }

//------------------------------------------------------------

var dropmenus = function(){
	return {
		item_onClick: function(S, A, B) {
			try {
				window.event.cancelBubble = true;
				
			} 
			catch(err){
				return
			}
			clearhidemenu();
			hidemenu();
			switch(A){
				case 'popup-sm':
					window.open(S, '', 'width=360,height=240,resizeable=1');
					break;
				case 'popup-med':
					window.open(S, '', 'width=480,height=360,resizeable=1');
					break;
				case 'popup-lg':
					window.open(S, '', 'width=640,height=480,resizeable=1');
					break;
				case 'none':
					document.location = S;
					break;
				default:
					window.open(S);
					break;
			}
			
		}
	}
}();
