
// function to mark the currently menu item as "on"
// added by Mike Voss for XCommunications 2007
function markMenuItem(){
    // declare boolean to hold testing results
    var blnOn = false;
    
    // loop through all the links in the main nav's <ul>
    for (var i = 0; i < document.getElementById("nav").getElementsByTagName("a").length; i++){
        // flag as active if current page is current main menu item
        blnOn = (window.location.href == document.getElementById("nav").getElementsByTagName("a")[i].href);
        
        // if this menu has a submenu...
        if(document.getElementById("ul0" + (i+1)) != null){
            // ...check in relevant submenu items (identified by the numbered <ul> tags)
            for (var j = 0;j<document.getElementById("ul0" + (i+1)).getElementsByTagName("a").length;j++){
                // If current page is in submenu of this main menu item...
                if(window.location.href == document.getElementById("ul0" + (i+1)).getElementsByTagName("a")[j].href){
                    // ... flag as active menu section
                    blnOn = true;
                    // ... and mark this submenu item as active
                    document.getElementById("ul0" + (i+1)).getElementsByTagName("li")[j].className = "on";
                }
            }     
        }
        // If the current link was found to be active...
        if(blnOn){
            // ...mark the corresponding main menu <li> element with the css class "on"
            document.getElementById("nav").getElementsByTagName("li")[i].className = "on";
        }
    }
}

// process menu css on page load
function defaultDisplay(){
    // apply css class "on" where appropriate
    markMenuItem();
    // display the correct submenu item, based on which page is displayed
    if(window.location.href.indexOf('/exhibitions/') != -1){
        swapLayers('nav02','lyr02'); 
        return false;
    }
    if(window.location.href.indexOf('/news') != -1){
        swapLayers('nav03','lyr03'); 
        return false;
    }
}

// process mouseout effect
function mouseOut(lnk,id){
    // if we are not dealing with exhibitions or news pages...
    if((window.location.href.indexOf("/exhibitions/") == -1)&&(window.location.href.indexOf("/news") == -1)){
        // ... run the swap script as normal
        swapLayers(lnk,id);
    }
    // otherwise...
    else{
        // if this is an exhibitions page...
        if(window.location.href.indexOf("/exhibitions/") != -1){
            // ... keep the exhibitions submenu open at all times...
            showLayer("lyr02");
            // ... and the news menu closed for now
            hideLayer("lyr03");
        }
        // if this is a news page...
        if(window.location.href.indexOf("/news") != -1){
            // ... keep the news submenu open at all times...
            showLayer("lyr03");
            // ... and hide the exhibitions submenu for now
            hideLayer("lyr02");
        }
    }
}

// process mouseover effect
function mouseOver(lnk,id){
    // if we are not dealing with exhibitions or news pages...
    if((window.location.href.indexOf("/exhibitions/") == -1)&&(window.location.href.indexOf("/news") == -1)){
        // ... run swapscript as normal
        swapLayers(lnk,id);
    }
    else{
        // ... otherwise just show the new submenu without hiding the old one
        showLayer(id);
    }
}

/*************************************************************************
  This code is from Dynamic Web Coding at http://www.dyn-web.com/
  Copyright 2001-3 by Sharon Paine 
  See Terms of Use at http://www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
*************************************************************************/

// resize fix for ns4
var origWidth, origHeight;
if (document.layers) {
	origWidth = window.innerWidth; origHeight = window.innerHeight;
	window.onresize = function() { if (window.innerWidth != origWidth || window.innerHeight != origHeight) history.go(0); }
}

// link style change
var cur_link;
function doLinkClass(lnk) {
  if (lnk && lnk.blur) lnk.blur();	// remove marquee
  if (!lnk || cur_link == lnk) return;
  if (cur_link) cur_link.className = "done";
  lnk.className = "on";
  cur_link = lnk;
}

var cur_lyr;	// holds id of currently visible layer
function swapLayers(lnk,id) {
  doLinkClass(lnk);
  if (cur_lyr) hideLayer(cur_lyr);
  showLayer(id);
  cur_lyr = id;
}

function showLayer(id) {
  var lyr = getElemRefs(id);
  if (lyr && lyr.css) lyr.css.visibility = "visible";
}

function hideLayer(id) {
  var lyr = getElemRefs(id);
  if (lyr && lyr.css) lyr.css.visibility = "hidden";
}

function getElemRefs(id) {
	var el = (document.getElementById)? document.getElementById(id): (document.all)? document.all[id]: (document.layers)? getLyrRef(id,document): null;
	if (el) el.css = (el.style)? el.style: el;
	return el;
}

// get reference to nested layer for ns4
// from old dhtmllib.js by Mike Hall of www.brainjar.com
function getLyrRef(lyr,doc) {
	if (document.layers) {
		var theLyr;
		for (var i=0; i<doc.layers.length; i++) {
	  	theLyr = doc.layers[i];
			if (theLyr.name == lyr) return theLyr;
			else if (theLyr.document.layers.length > 0) 
	    	if ((theLyr = getLyrRef(lyr,theLyr.document)) != null)
					return theLyr;
	  }
		return null;
  }
}

function init(id,lyr) {
  var lnk = getElemRefs(id);
  swapLayers(lnk,lyr);
}