/**
 *
 *  menu javascript routines and setup
 *
 *
 */
// Timeout for the dropdown control
HIDE_TIMEOUT = 100;

// Preload the rollover images

image1 = new Image();
image1.src = "images/m5-rollover.jpg";

image2 = new Image();
image2.src = "images/m2-rollover.jpg";

image3 = new Image();
image3.src = "images/m3-rollover.jpg";

image4 = new Image();
image4.src = "images/m4-rollover.jpg";

image5 = new Image();
image5.src = "images/m5-rollover.jpg";

// Mouse over tab
function mouseover(ctrl) {
  // set the rollover menu
  if (ctrl.id != activeMenu) {
    imageID = "image" + ctrl.id.substr(1,1);
    imageName = "images/m" + ctrl.id.substr(1,1) + "-rollover.jpg";
    document.getElementById(imageID).src = imageName;
  }
  // show the dropdown menu if it exists
  if (document.getElementById("dropmenu" + ctrl.id.substr(1,1))) {
    var menu = document.getElementById("dropmenu" + ctrl.id.substr(1,1));
    if (menu == hideCtrl) {
      clearTimeout(hideTimeout);
    }
    var left = findPosLeft (ctrl) + 15;
    var top = findPosTop (ctrl) + 49;
    var menu = document.getElementById("dropmenu" + ctrl.id.substr(1,1));
    menu.style.left = left + "px";
    menu.style.top = top + "px";
    menu.style.display = "block";
  }
}

// reset the tab to inactive
function mouseout(ctrl) {
  // set the tab to inactive
  if (ctrl.id != activeMenu) {
    imageID = "image" + ctrl.id.substr(1,1);
    imageName = "images/m" + ctrl.id.substr(1,1) + "-inactive.jpg";
    document.getElementById(imageID).src = imageName;
  }
  // clear the dropdown menu, if the mouseover event for the dropdown is activated, the clearing will be cancelled
  if (document.getElementById("dropmenu" + ctrl.id.substr(1,1))) {
    var menu = document.getElementById("dropmenu" + ctrl.id.substr(1,1));
    hideCtrl = menu;
    hideTimeout = setTimeout("hidemenu()", HIDE_TIMEOUT);
  }
}

// find the left of the tab to position the dropdown
function findPosLeft(ctrl) {
  var curleft = 0;
	if (ctrl.offsetParent) {
	do {
			curleft += ctrl.offsetLeft;
		} while (ctrl = ctrl.offsetParent);
	}
	return curleft;
}

// find the top of the tab to position the dropdown
function findPosTop(ctrl) {
  var curtop = 0;
	if (ctrl.offsetParent) {
	do {
			curtop += ctrl.offsetTop;
		} while (ctrl = ctrl.offsetParent);
	}
	return curtop;
}

var hideCtrl;
var hideTimeout;

// Mouseover event for the dropdown menus, stops the tab from being reset from rollover
function mouseovermenu(ctrl) {
  // stop the clearing of the menu caused by the mouseout event on the tab
  clearTimeout(hideTimeout);
  // reset the tab image to rollover
  imageID = "image" + ctrl.id.substr(8,1);
  imageName = "images/m" + ctrl.id.substr(8,1) + "-rollover.jpg";
  document.getElementById(imageID).src = imageName;
}

// Mouse out event for the dropdown, starts the clearing of the dropdown
function mouseoutmenu(ctrl) {
  // hide the dropdown
  hideCtrl = ctrl;
  hideTimeout = setTimeout("hidemenu()", HIDE_TIMEOUT); 
  // reset the tab image to inactive
  if ("a" + ctrl.id.substr(8,1) != activeMenu) {
    imageID = "image" + ctrl.id.substr(8,1);
    imageName = "images/m" + ctrl.id.substr(8,1) + "-inactive.jpg";
    document.getElementById(imageID).src = imageName;
  }
}

// this is where the menu is hidden, usually after a short timeout
function hidemenu () {
  hideCtrl.style.display = "none";
}

// insure that dropdowns are cleared if the mouse is clicked
document.onclick = hidemenu;

