/* 
   menu.js written on 3/7/07 by Chris Reade
   (uses getClassList from w3C Slidy)

*/


window.onload = startup; // equivalent to onload on body element

// global variables initialised at startup
var allpages = new Array();
var currentPage;
var pictures = new Array();
var currentPicture;
var menuItems = new Array();

/* general initialization */
function startup()
{
	var divs = document.body.getElementsByTagName("div");

	// find allpages and menuItems in divs
	for (var i = 0; i < divs.length; ++i)
	   {
	      div = divs[i];

	      if (hasClass(div, "page"))
	      {  // add page to collection
	         allpages.push(div);
	         // hide each page as it is found
	         hide(div)
	      }
	      if (hasClass(div, "menuItem"))
	      {  // add item to collection
	         menuItems.push(div);
	      }
	      
	   }	
    
    // get all Images appearing in picture div
    pictures = document.getElementById("picture").getElementsByTagName("img")
    // hide these pictures initially
	for(var i=0; i < pictures.length; i++) { hide(pictures[i]); }	

//	document.body.style.visibility = "visible";
//	window.offscreenbuffering = true;
	
	//set up menu behaviour and first page and picture
	attachMenuBehaviours(menuItems);
	currentPage = allpages[0]; reveal(currentPage);
    currentPicture = pictures[0]; reveal(currentPicture);

}

/* Attaching all menu items in one loop does not work because single var page is created for all menuItems.
   Also cannot replace by allpages[i] because it picks up last value of i in loop
   i.e. a delayed evaluation/closure problem 
function attachMenuBehaviours(menuArr)
{
	for(var i=0; i<menuArr.length; i++)
	{   var page = allpages[i]
		var item = menuArr[i]
		item.onclick = function(){...reveal(page); }
	}
}
*/

function attachMenuBehaviours(menuArr)
{   // attach behaviour to items in menuArr - an array
	for(var i=0; i<menuArr.length; i++)
	   attachItemBehaviour(menuArr[i], allpages[i], pictures.item(i));
}
function attachItemBehaviour(item,page,pic)
{   // attaches behaviour to item - to switch to new page and pic
	item.onclick = function()
	   {
		 if(page != currentPage)
		 { hide(currentPage); hide(currentPicture)
		   window.scrollTo(0,0); 
		   currentPage = page; currentPicture = pic
		   reveal(currentPage); reveal(currentPicture)
		 }
	   }
		
}

function hide(item)
{
	item.style.visibility="hidden";
	item.style.display = "none";
}

function reveal(item)
{
	item.style.visibility="visible"
	item.style.display = "block";
}


function getClassList(element)
{
  if (typeof window.pageYOffset =='undefined') // true in IE 
    return element.getAttribute("className");  // IE requires className

  return element.getAttribute("class");  // other browsers require class
}

function hasClass(element, name)
{
  var regexp = new RegExp("(^| )" + name + "\W*");

  if (regexp.test(getClassList(element)))
    return true;

  return false;

}


// designed to work with both text/html and text/xhtml+xml
function getElementsByTagName(name)
{
   if (typeof document.getElementsByTagNameNS != 'undefined')
   {
      return document.getElementsByTagNameNS('http://www.w3.org/1999/xhtml', name);
   }

   if (typeof document.getElementsByTagName != 'undefined')
   {
      return document.getElementsByTagName(name);
   }

   return null;
}

