//////////////////////////////////////////////////////////
// name:	browser	
// purpose:	To determine browser type
// inputs:	none
// outputs:	none
//////////////////////////////////////////////////////////

function browser() {
	// Set up all conditions as false
	this.isIE = false;  
	this.isNS4 = false;  
	this.isNS6 = false; 
	
	// Test for NS4
	if (document.layers) { 
	// If is true set isNS4 
		this.isNS4 = true;
	// return to stop any other settings becoming true				
		return;
	}
	// Test for IE
	if (document.all) { 
		this.isIE = true;
		return;
	}

	// Test for NS6
	if (document.getElementById && !document.all) { 
		this.isNS6 = true;
		return;
	}
}
// Set up browserType as global object decribing browser type
browserType  = new browser();

//////////////////////////////////////////////////////////
// name:	getLeft
// purpose:	To determine left position of element for placing of new elements.
// inputs:	element - the element who's position is to be determined
// outputs:	x - the left position
//////////////////////////////////////////////////////////
function getLeft(element) {
	// Retrieve the offset from container element
	x = element.parentNode.offsetLeft;
	
	// if the element is contained add the offset of that element
	// by sending through the function again.  Repeat as required 
	if (element.offsetParent != null) {
		x = x + getLeft(element.offsetParent);
	}
	// IE and NS return differnt values - need to compensate
	if (browserType.isIE) {
		x = parseInt(x) + parseInt(0);
	}
	else {	
		x = parseInt(x) + parseInt(0);
	}


	// return left offset
	return x;
}

//////////////////////////////////////////////////////////
// name:	getTop
// purpose:	To determine top position of element for placing of new elements.
// inputs:	element - the element who's position is to be determined
// outputs:	y - the top position
//////////////////////////////////////////////////////////

function getTop(element) {

	// Retrieve the offset from container element
	y = element.offsetTop;
	
	// if the element is contained add the offset of that element
	// by sending through the function again.  Repeat as required 
	if (element.offsetParent != null) {
		y = y + getTop(element.offsetParent);
	}
	// IE and NS return differnt values - need to compensate
	if (browserType.isIE) {
		y = parseInt(y) + parseInt(1);
	}
	else {
		y = parseInt(y) + parseInt(1);
	}

	// return left offset
	return y;
}
//////////////////////////////////////////////////////////
// Menu System
//////////////////////////////////////////////////////////

// Global varibles to register current menus
activeBut = null;
activeMenu = null;

timelimit = 500;
shutTimer = null;

/*
// Set up mouse event capture
if (browserType.isNS6 ) {
	document.addEventListener("MOUSEDOWN", checkMenu, true);
//	the following might work as well
//	document.captureEvent(Event.MOUSEDOWN);	
	
}
else if (browserType.isNS4) {
	document.captureEvent(Event.MOUSEDOWN);
	
}
// is mouse is down goto checkMenu function
document.onmousedown = checkMenu;
*/
//////////////////////////////////////////////////////////
// name:	checkMenu
// purpose:	Responds to mouse click and checks if a menu needs shutting
// inputs:	event - mouse event
// outputs:	none
// links:	removeClassName
//////////////////////////////////////////////////////////
function checkMenu(event) {

	// if there is no active button no need to carry on return
	if (!activeBut) {
		return;
	}
	
	// determine browser type and retrieve target element
	if (browserType.isIE) { 
		
		button = window.event.srcElement;
	} 
	else {	// NS method
		if (event.target.tagName) {
			button = event.target;
		}
		else {
			button = event.target.parentNode;
		}
	}
	
	// If they clicked on the active button ignore it
	if (button == activeBut) {
		return;
	}
	
	// If the element does not have a class name i.e. is outside the menu system
	// or if it does have a class name but it is not a menu then close menu
	if (!button.className || button.className != "menuItem") {
		// the button will no longer be active so remove the formatting tag
    		removeClassTag(activeBut);
		// make the menu layer invisible
		activeMenu.style.visibility = "hidden";
		// null the active items
		activeBut = null;
		activeMenu = null;
	}

	return;

}
//////////////////////////////////////////////////////////
// name:	openMenu
// purpose:	open the menu
// inputs:	menuId - id of the layer calling the function
//		event - the trigger event to determine target - not needed for IE
// outputs:	activeBut - set to the active button
//		activeMenu - set to the active menu
// links:	removeClassName
//////////////////////////////////////////////////////////
function openMenu(event, menuId) {

	//	determine event target
	if (browserType.isIE) { 
		button = window.event.srcElement;
	} 
	else {
		button = event.currentTarget;
	}

	if (activeBut == button) {
		return;
	}

	// if there is already a menu open close it
	if (activeBut !=null) {
		removeClassTag(activeBut.parentNode);
		activeMenu.style.visibility = "hidden";
	}	
	
	//	select required layer
	if (browserType.isIE) { 
		button.menu = document.all[menuId];	
	}

	if (browserType.isNS4) {
		button.menu = document.layers[menuId];
	}

	if (browserType.isNS6 ) {
		button.menu = document.getElementById(menuId);
	}
	
	// remove highlight
	button.blur();

	// set layer visibility
	button.menu.style.visibility = "visible";

	button.parentNode.className = button.parentNode.className + "__menuButtonActive";

	// get the position of the element
	x = getLeft(button);
  	y = getTop(button) + button.offsetHeight;
	
 	button.menu.style.left = x + "px";
  	button.menu.style.top  = y + "px";

	// set active menus
	activeBut= button;
	activeMenu = button.menu;

}

function shutMenu() {
	shutTimer = setTimeout("closeMenu()", timelimit);

}

function keepOpen() {
	if (shutTimer) {
		clearTimeout(shutTimer);
	}
}

function closeMenu() {
	if (activeBut !=null) {
		removeClassTag(activeBut.parentNode);
		activeMenu.style.visibility = "hidden";
	}	
	activeBut = null;
	activeMenu = null;
}

//////////////////////////////////////////////////////////
// name:	removeClassTag(element, classTag)
// purpose:	reformat class tags
// inputs:	element - to be changed
// outputs:	none
//////////////////////////////////////////////////////////
function removeClassTag(element) {
	// set Arrays to hold class tags
	newClassTag = new Array();
	// split old tag
	oldClassTag = element.className.split("__");
	
	// cycle through tags copying to new array unless it is menuButtonActive
	for (i = 0; i < oldClassTag.length; i++) {
		if (oldClassTag[i] != "menuButtonActive") {
			newClassTag[i] = oldClassTag[i];
		}
	}
	// copy new array to className
	element.className = newClassTag.join("");
	
}

