// **************************************************************
// JavaScript functions to provide actions within the WRAP pages.
// **************************************************************

// Global holding an array of all menu objects.
var gMenuItemsIds = new Array;

// Adds an item to the list of menu objects. These are then selected via
// the 'MenuSwap' function using the appropriate index. Indices are applied
// in the order in which items are added.
function AddObjIdToMenuList(objId)
{
	// Add the object to the global array.
	gMenuItemsIds.push(objId);
}

// Pass in an index (one based, zero is not valid) for the object to be
// displayed. All other objects will be hidden. The object are added using
// the 'AddObjIdToMenuList' function. If all object are to be hidden, pass in
// an index of -1.
function MenuSwap(index)
{
	var objIdToShow = null;
	
	// If in range, then display the appropriate object.
	if (index > 0 && index <= gMenuItemsIds.length)
	{
		objIdToShow = gMenuItemsIds[index - 1];
	}

	// Hide all items (do this in all cases).
	for (index in gMenuItemsIds)
	{
		// Get element.
		var objId = gMenuItemsIds[index];
		var element = document.getElementById(objId);
		
		// Set the display state.
		if (objId == objIdToShow)
		{
			element.style.display = '';
		}
		else
		{
			element.style.display = 'none';
		}
	}
}

// Same as 'MenuSwap' except that if the passed in index is currently shown it
// will be hidden. This means that passing in index 1 and then index 1 again
// with this function will show object 1 and then hide it. With 'MenuSwap' it
// would be shown and then reshown, ie not hidden the second time.
function MenuToggle(index)
{
	var objIdToToggle = null;

	// If in range, then get the appropriate object.
	if (index > 0 && index <= gMenuItemsIds.length)
	{
		objIdToToggle = gMenuItemsIds[index - 1];
	}

	// Is this object currently shown?
	if (document.getElementById(objIdToToggle).style.display == '')
	{
		// Yes, currently shown, so hide by hidding ALL. Do this by
		// setting the index to -1 (hide all).
		index = -1;
	}
	
	// Call standard 'MenuSwap' with appropriate index.
	MenuSwap(index);
}

// As per 'MenuSwap' for setup but this function only works on lists with TWO
// ITEMS. An further items will be ignored. Works by detecting if the first
// item is visible. If so, it hides it and shows the second - or visa-versa.
// The global list MUST have two items in it.
function MenuFlip()
{
	var firstElement = document.getElementById(gMenuItemsIds[0]);
	var secondElement = document.getElementById(gMenuItemsIds[1]);
	
	if (firstElement.style.display == '')
	{
		firstElement.style.display = 'none';
		secondElement.style.display = '';
	}
	else
	{
		firstElement.style.display = '';
		secondElement.style.display = 'none';
	}	
}

// Toggles to elements to which the IDs are directly passed in. Toggle is
// based on the visibility of the first id (idA) only.
function ToggleElements(idA, idB)
{
	var elementA = document.getElementById(idA);
	var elementB = document.getElementById(idB);

	if (elementA.style.display == '')
	{
		elementA.style.display = 'none';
		elementB.style.display = '';
	}
	else
	{
		elementA.style.display = '';
		elementB.style.display = 'none';
	}
}

// Closes the window.
function CloseWindow()
{
	window.close();
}
