document.onmousemove = DelayHideMenus;
document.onmouseup = HideMenus;

var Timeout;
var TimeOutRunning = false;
var MenuShown = false;
var CurrentMenu = '';
var MenuCountOut = 0;
var loaded = false;

var WaitTime = 250;
var MnuCountDelay = 40;

var mnuSelCol = '#FFFFFF';
var mnuSelBkCol = '#000000';
var mnuUnSelCol = '#000000';
var mnuUnSelBkCol = '#FFFFFF';

function SetMenuColours (SelCol, SelBkCol, UnSelCol, UnSelBkCol) {
  mnuSelCol = SelCol;
  mnuSelBkCol = SelBkCol;
  mnuUnSelCol = UnSelCol;
  mnuUnSelBkCol = UnSelBkCol;
  loaded = true;
}

function DelayHideMenus(eventObj) {
  if (!TimeOutRunning && MenuShown) {
    Timeout = setTimeout("CheckMenu()", WaitTime);
    TimeOutRunning = true;
  }
}

function ShortDelayMenu() {
  if (MenuCountOut > 2) {
    MenuCountOut = 2;
  }
}

function CheckMenu() {
  if (MenuCountOut-- <= 0) {
    TimeOutRunning = false;
    HideMenus();
  }
  else {
    Timeout = setTimeout("CheckMenu()", WaitTime);
    TimeOutRunning = true;
  }
}


function HideMenus() {
 changeObjectVisibility(CurrentMenu, 'hidden');
 changeObjectColour('mnu' + CurrentMenu, false);
 if (TimeOutRunning) {
    clearTimeout(Timeout);
  }
 TimeOutRunning = false;
}

function ShowMenu(menu) {
    if (!loaded) {return false;}
    if ((CurrentMenu != '') && (CurrentMenu != menu))  {
	  HideMenus();
	}
    if (TimeOutRunning) {
      clearTimeout(Timeout);
      TimeOutRunning = false;
    }
    CurrentMenu = menu;
    if(changeObjectVisibility(CurrentMenu, 'visible')) {
      changeObjectColour('mnu' + CurrentMenu, true);
      MenuShown = true;
      MenuCountOut = MnuCountDelay;
      return true;
    } else {
	return false;
    }
}

function ActiveSlideShowMenu(menu, linkid, newurl){
    if (!loaded) {return false;}
    result = ShowMenu(menu);
    alink = document.getElementById(linkid);
    if ((alink != null) && (alink.href != newurl)) {
      alink.href = newurl;
    }
    return result;
}

function KeepMenuVisible() {
  if (MenuCountOut > 0) {
    MenuCountOut = MnuCountDelay;
  }
}


function getStyleObject(objectId) {
	if (CurrentMenu == '') {
	  return false;
	}
	// cross-browser function to get an object's style object given its id
	if(document.getElementById && document.getElementById(objectId)) {
	  // W3C DOM
	  return document.getElementById(objectId).style;
	}
	else if (document.all && document.all(objectId)) {
	  // MSIE 4 DOM
	  return document.all(objectId).style;
	}
	else if (document.layers && document.layers[objectId]) {
	  // NN 4 DOM.. note: this won't find nested layers
	  return document.layers[objectId];
	}
	else {
	  return false;
	}
} // getStyleObject

function changeObjectVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	  styleObject.visibility = newVisibility;
	  styleObject.zIndex=20;
	  return true;
    }
	else {
   	  return false;
    }
}


function changeObjectColour(objectId, selected) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	  if (selected) {
		  styleObject.color = mnuSelCol;
		  styleObject.backgroundColor = mnuSelBkCol;
	  }
	  else {
		  styleObject.color = mnuUnSelCol;
		  styleObject.backgroundColor = mnuUnSelBkCol;
		  styleObject.backgroundImage = '';
	  }
	  return true;
    }
	else {
	  //we couldn't find the object, so we can't change its visibility
      return false;
    }
}

