function getCookie(nameOfCookie) {
	if (document.cookie.length > 0) {
		begin = document.cookie.indexOf(nameOfCookie+"=");
		if (begin != -1) {
			begin += nameOfCookie.length+1;
			end = document.cookie.indexOf(";", begin);
			if (end == -1) {
				end = document.cookie.length;
			}
			return unescape(document.cookie.substring(begin, end));
		}
	}
	return null;
}

function setCookie(nameOfCookie, value, expiredays) {
	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
	document.cookie = nameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString() + "; path=/;");
}

function toggleMenu(currentMenu) {
	if (document.getElementById) {
		thisMenu = document.getElementById(currentMenu).style;
		if (thisMenu.display == 'block') {
			thisMenu.display = 'none';
			setCookie(currentMenu, 'none', 1);
		} else {
			thisMenu.display = 'block';
			setCookie(currentMenu, 'block', 1);
		}
		return false;
	}
}

function initializeMenu(currentMenu) {
	if (document.getElementById) {
		thisMenu = document.getElementById(currentMenu).style;
		cookieState = getCookie(currentMenu);
		if (cookieState != null) {
			thisMenu.display = cookieState;
		} else {
			setCookie(currentMenu, 'none', 1);
			thisMenu.display = 'none';
		}
	}
}

//document.onclick = blurAllLinks;
if (document.captureEvents) document.captureEvents(Event.CLICK);
function blurAllLinks() {
	var allLinks = document.getElementsByTagName('a');
	for(i=0 ; i<allLinks.length ; i++){
		allLinks[i].blur();
	}
}

//This prototype is provided by the Mozilla foundation and
//is distributed under the MIT license.
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license

if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}

var theMenus = ['artists', 'exhibitions'];
var lastMenu = null;
function initializeAllMenus ()
{
	theMenus.forEach (initializeMenu);
	lastMenu = getCookie('lastMenu');
}

function menuToggle (current)
{
	theMenus.forEach (function (e) { if (lastMenu != null && lastMenu != current && e == lastMenu) toggleMenu (e) });
	
	lastMenu = lastMenu == current ? null : current;
	setCookie('lastMenu', current, 1);
	return (toggleMenu (current));
}

function resetCookies ()
{
	theMenus.forEach (function (e) { setCookie(e, null, 0); });
	setCookie('lastMenu', null, 0);
}


