// JavaScript Document


function openMenu(event) {
	var tempid = this.getAttribute("id");
	var anc = this;
	var menuid_array = tempid.split("A");
	var menuid = menuid_array[0];
    var menu = document.getElementById(menuid);

	var x = findPosX(anc);
	var y = findPosY(anc);

	menu.style.left = (menu.ShiftX + x) + "px";
  	menu.style.top  = ((menu.TopPad + y) - GlobalOverLap) + "px";
	if(menu.SetWidth) menu.style.width = (menu.SetWidth) + "px";
	//alert(y);
	menu.style.visibility = "visible";
}	

function activeMenu(event){
	var menu = this;
	if(!menu.Active) { 
		menu.Active = true; //set flag to stop anchor from closing window
		//alert(menu.Active);
	}
	
}



function closeMenu(event) {
  //TAKE CARE OF EVENT BUSINESS
  if (!event) var event = window.event; //IE ASSIGNMENT
  event.cancelBubble = true;
	if (event.stopPropagation) event.stopPropagation(); //IE BUBBLESTOP

  if (window.event) {
    var current = this;
    var related = window.event.toElement;
  }
  else {
    current = event.currentTarget;
    related = event.relatedTarget;
  }
  if (current != related && !contains(current, related)) {
	  current.style.visibility = "hidden";
	  current.Active = false;
	 // alert(current.Active);
  }
}

function closeMenuAnchor(event) {
	var tempid = this.getAttribute("id");
	var menuid_array = tempid.split("A");
	var menuid = menuid_array[0];
    var menu = document.getElementById(menuid);
	if(!menu.Active) menu.style.visibility = "hidden";
}
								 
					



function contains(a, b) {
  // Return true if node a contains node b.
  while (b.parentNode)
    if ((b = b.parentNode) == a)
      return true;
  return false;
}


function initMenu() {

//assign all events to menus and their triggers
	var divSet = document.getElementsByTagName("div");
	var Nid,Nid_array;
	for(var i=0;i < divSet.length; i++){ 
		Nid = divSet[i].getAttribute("id");
		if(Nid){ //only for divs with an id and not the null at end of array
			if(Nid.indexOf("Anchor") > 0) { //only if id has "Anchor" string in it
				divSet[i].onmouseover = openMenu;
				divSet[i].onmouseout = closeMenuAnchor; //close if never mouseover popupmenu
				Nid_array = Nid.split("A"); //find menu
				var menu = document.getElementById(Nid_array[0]);
				
				if(eval("window." + Nid_array[0] + "TopPad")) menu.TopPad = eval(Nid_array[0] + "TopPad");
				else menu.TopPad = divSet[i].offsetHeight;

				if(eval("window." + Nid_array[0] + "SetWidth")) menu.SetWidth = eval(Nid_array[0] + "SetWidth");
				else menu.SetWidth = null; 

				if(eval("window." + Nid_array[0] + "ShiftX")) menu.ShiftX = eval(Nid_array[0] + "ShiftX");
				else menu.ShiftX = 0; 

				menu.onmouseover = activeMenu; //once msover,then active(fn closeMenuAnchor won't close it)
				menu.onmouseout = closeMenu; //close and deactivate menu
			}
		}
	}
}




//help with this code came from:
//http://www.quirksmode.org/dom/w3c_core.html
//http://www.javascriptkit.com/domref/elementmethods.shtml
