/*  
==================================================================================================
	random page function
==================================================================================================
*/

function showRandomPage(idOfContainer) {
  var randomPageNum = Math.floor(Math.random() * 5); // generate random number between 0 and 4
  var nodeList = document.getElementById(idOfContainer).getElementsByTagName("li"); // get all navigation list items
  switchPage(nodeList[randomPageNum]); // "activate" the random page
  return false;
} 
/*  
==================================================================================================
	CASE STUDIES Pagination 
	(Change between pages on the CASE STUDIES on the Homepage.)
==================================================================================================
*/
function switchPage(newPageNumberElement) {
// For use with the pagination controls in the centre column
	var newPageNumber = newPageNumberElement.innerHTML.replace(/^\s+|\s+$/g, '');
	var container = newPageNumberElement.parentNode;
	var nodeList = container.getElementsByTagName("li");
	var pageList = container.parentNode.getElementsByTagName("div");

	// Make *all* menu nodes inactive
	for(var i = 0; i < nodeList.length; i++) {
		if(nodeList[i] && nodeList[i].parentNode.id == container.id) {
			changeClass(nodeList[i], "inactive");
			changeClass(pageList[i], "inactive");
		}
	}
	
	// make *selected* node active
	changeClass(newPageNumberElement, "active");
	changeClass(document.getElementById("page_" + newPageNumber), "active");

	return false;
}
/*  
==================================================================================================
	Generic function to change the class on any HTML element/object.
==================================================================================================
*/
function changeClass(elementToChange, newClassName) {
	elementToChange.className = newClassName;
}
/*  
==================================================================================================
	Close all menu items and open the selected one.
==================================================================================================
*/
function switchMenuItem(menuNodeDiv) {
	// get the parent container of the selected menu node
	var menuContainer = menuNodeDiv.parentNode;
	
	// get all divs within the parent container
	var navNodes = menuContainer.getElementsByTagName("div");
	
	// CLOSE *ALL* MENU NODES
	// loop through all divs
	for(i = 0; i < navNodes.length; i++){
		// only close the DIV, if it's a direct child of the menu container (i.e. a "menu node")
		if(navNodes[i].parentNode.id == menuContainer.id){
			changeClass(navNodes[i], "inactive");
		}
	}
	
	// OPEN THE *SELECTED* MENU NODE
	changeClass(menuNodeDiv, "active");	
}
/*	
==================================================================================================
	This function is almost the same as "switchMenuItem()" but
	if the selected menu was open, close it (i.e. this would close ALL menu items)
==================================================================================================
*/
function openCloseMenuItem(menuNodeDiv) {
	// Check the current state of the selected item
	var originalClass = menuNodeDiv.className;

 	// Close all menu items and open the selected item
	switchMenuItem(menuNodeDiv);
	
	// Now close the selected item if it WAS ORIGINALLY OPEN
	if(originalClass == "active"){
		changeClass(menuNodeDiv, "inactive");
	}
}

