// JavaScript Document
/*
	Code to provide pop-up menus for the PI site
*/

// define getElementByID() in browsers that don't have it already
if(!document.getElementById)
{
	if(document.all)
	{
		document.getElementById = function(id) { return document.all[id]; }
	}
	else if(document.layers)
	{
		document.getElementById = function(id) { return document.layers[id]; }
	}
	else
	{
		document.getElementById = function(id) { return null; }
	}
}

// ========================================================================	

// global variables
var PICurrentSubnavMenu = null; // currently showing menu
var PIMenuToHide = null; // menu we've scheduled to hide
var PIMenuHideTime = 333; // number of milliseconds menus take to disappear
var PIMenuHideTimeout = null;

// called when the mouse moves into a main nav button
function PIMouseOverNav(menuName)
{
	if(menuName != null)
	{
		// hide the currently visible subnav if there is one
		PIHideMenu();
				
		// show the new menu
		PIToggleSubnav(menuName);
		PICurrentSubnavMenu = menuName;
	}
	
	// set the button's arrow to be highlighted
	//var arrowImage = document.getElementById(menuName + "arrow");
	//swapImage(arrowImage, 'navon');
	//PISwapImage(menuName, true);
}

// called when the mouse moves out of the main nav button
function PIMouseOutNav(menuName)
{
	if(menuName != null)
	{
		// schedule the subnav menu to be hidden
		PIHideMenuWithDelay();
	}
	
	// set the button's arrow to be normal
	//var arrowImage = document.getElementById(menuName + "arrow");
	//revertImage(arrowImage);
	//PISwapImage(menuName, false);	
}

// deal with the mouse pointer moving into a subnav menu
function PIMouseOverSubnav(menuName)
{
	// if this menu was scheduled to be hidden then cancel that
	if(PIMenuToHide == menuName)
	{
		PIHideMenuCancel();			
	}
}

// called when the mouse moves out of a subnav menu
function PIMouseOutSubnav(menuName)
{
	// schedule this menu to be hidden
	PIHideMenuWithDelay();
}

// ========================================================================

// swap the image contained in the given paragraph tag
// para needs to be <p><a ...><img ...> ... so img is first child
// of first child of paragraph
// if isSubnav = true then subnav arrow image is used
function PISwapImage(paraElem, isSubnav)
{
	if(paraElem == null) { return; }
	var anchorElem = paraElem.firstChild;
	if(anchorElem == null) { return; }
	// check if the anchor is actually an image
	image = null;
	if(anchorElem.nodeName == "IMG")
		image = anchorElem;
	else
		image = anchorElem.firstChild;

	// if we have the image then swap it
	if(image != null)
	{
		if(isSubnav)
		{
			swapImage(image, 'subnavon');
		}
		else
		{
			swapImage(image, 'navon');
		}
	}
}

// same as PISwapImage but reverts the image back to its original form
function PIRevertImage(paraElem)
{
	if(paraElem == null) { return; }
	var anchorElem = paraElem.firstChild;
	if(anchorElem == null) { return; }
	// check if the anchor is actually an image
	image = null;
	if(anchorElem.nodeName == "IMG")
		image = anchorElem;
	else
		image = anchorElem.firstChild;

	// if we have the image then revert it
	if(image != null)
	{
		revertImage(image);
	}
}

// ========================================================================	

// hide the currently visible subnav menu immediately
// used when closing one subnav and opening another straight away
function PIHideMenu()
{
	// if the menu we want to hide now is already on a timed hide then cancel the times hide
	if(PICurrentSubnavMenu == PIMenuToHide)
	{
		PIHideMenuCancel();		
	}
	
	if(PICurrentSubnavMenu != null)
	{
		PIToggleSubnav(PICurrentSubnavMenu);
		PICurrentSubnavMenu = null;
	}
}

// hide the currently visible subnav menu after a few seconds
// used when user has not gone into another subnav to replace the current one
function PIHideMenuWithDelay()
{
	// not sure what to do if we already have a timeout going
	
	PIMenuToHide = PICurrentSubnavMenu;
	PIMenuHideTimeout = setTimeout("PIToggleSubnav(PIMenuToHide)", PIMenuHideTime); 
}

// cancel a delayed hide of a subnav menu
function PIHideMenuCancel()
{
	if(PIMenuHideTimeout != null)
	{
		PIMenuToHide = null;
		clearTimeout(PIMenuHideTimeout);
	}
}

// make a menu visible if it's invisible and vice-versa
function PIToggleSubnav(menuName)
{		
	if(menuName == null)
	{
		return;
	}
	
	var menuObj = document.getElementById(menuName);
	if(menuObj == null)
	{
		return;
	}
	
	if(menuObj.style.visibility == 'visible')
	{
		menuObj.style.visibility = 'hidden';
		
		// if this was the currently visible menu then set the currently visible menu to null
		if(menuName == PICurrentSubnavMenu)
		{
			PICurrentSubnavMenu = null;
		}
	}
	else
	{
		menuObj.style.visibility = 'visible';
		menuObj.style.zIndex = 100;
	}
}

// ========================================================================	

/*
	Supply a list of pictures to load in the format:
	<picture name>, <location of picture>
*/
function preloadImages()
{
	if(document.images)
	{
		var i, a = preloadImages.arguments;
		
		if(!document.rolloverPicNames)
			document.rolloverPicNames = new Array();
		if(!document.rolloverPics)
			document.rolloverPics = new Array();
	
		for(i = 0; i < a.length; i += 2)
		{
			document.rolloverPicNames[i] = a[i];
			document.rolloverPics[i] = new Image;
			document.rolloverPics[i].src = a[i + 1];
		}
	}
}

/*
	change the image displayed in the given picture object
	to the one with the supplied name
*/
function swapImage(imageToSwap, newImageName)
{
	// find the index of the picture whose name is newImageName
	var i, picIndex = -1;
	if(document.rolloverPics == null) { return; }
	for(i = 0; i < document.rolloverPics.length; i++)
		if(document.rolloverPicNames[i] == newImageName)
			picIndex = i;
	
	// swap the src of imageToSwap to become the relevant
	// image in document.rolloverPics[]
	if(picIndex >= 0)
	{
		// store the image's original src if we haven't already
		if(!imageToSwap.oSrc)
			imageToSwap.oSrc = imageToSwap.src;
			
		imageToSwap.src = document.rolloverPics[picIndex].src;
	}
}

/*
	Revert image(s) to original state
*/
function revertImage()
{
	var i, theImage, a = revertImage.arguments;
	for(i = 0; a&& (i < a.length)&& (theImage = a[i])&& theImage.oSrc; i++)
		theImage.src = theImage.oSrc;
}


