var sd, td, sc, tc, m1, m2, u1, u2, wn, s, t;

function preloadmenus() {
	sd = MM_findObj('secpd'); td = MM_findObj('thrpd'); sc = MM_findObj('secct'); tc = MM_findObj('thrct');
	wn = MM_findObj('whatLayer'); s = MM_findObj('second'); t = MM_findObj('third');
	m1 = m2 = u1 = u2 = null;
	var menuId = getCookie('mid');
	if (menuId != null && menuId != '')
		showMenu(menuId);
	else
		showMenu('first_menu');
}

function isMenuUp(menu_id) {
	var lastMenu = getCookie('mid');
	var menus = lastMenu.split('.');
	if( menu_id.indexOf('.') >= 0 )
		return lastMenu == menu_id;
	else
		return menus[0] == menu_id;
}

// This method must not return anything, as is required for being called from a 
// "javascript:" anchor link, in order to stay on the same page.
function toggleMenu(menu_id) {
	var menus = menu_id.split('.');
	if (menus.length == 1) {
		if( isMenuUp(menus[0]) ) {
			hideMenus();
			return;
		}
		hideMenus();
	} else {
		if( isMenuUp(menu_id) ) {
			hideThirdLevelMenu(menu_id);
			return;
		}
		hideThirdLevelMenu(menu_id);
	}
	showMenu(menu_id);
	keepMenuItemFocused(menu_id);
}

function keepMenuItemFocused(menu_id) {
	// For IE, ensure that the clicked menu item remains focused
	// for those keyboard enthusiasts and for accessibility requirements.
	var menuItem = document.getElementById('u' + menu_id);
	getImmediateChildrenByTagName(menuItem, 'A')[0].focus();
}

function showMenu(menu_id) {
	menus = menu_id.split('.');
	if ((m1 = MM_findObj(menus[0])) != null) {
		// 2nd level
		s.className = 'std';
		sd.style.visibility = 'visible';
		if( sc.innerHTML != m1.innerHTML ) sc.innerHTML = m1.innerHTML;
		u1 = MM_findObj('u' + menus[0]);
		if (u1 != null) u1.className = 'ls'; 
	}
	if (menus.length == 2 && (m2 = MM_findObj(menus[1])) != null) {
		// 3rd level
		t.className = 'ttd';
		td.style.visibility = 'visible';
		tc.innerHTML = m2.innerHTML;
		u2 = findNode('u' + menu_id, sc);
		if (u2 != null) u2.className = 'ls';
	}
	setCookie(menu_id, 'mid');
	return false;
}

function hideThirdLevelMenu(menu_id) {
	menus = menu_id.split('.');
	if( menus.length < 2 ) return;
	tc.innerHTML = ''; // hides text of third level
	td.style.visibility = 'hidden'; // hides vertical bar at left of third level
	t.className = 'htd'; // allows What's New box to resize
	
	if( u2 != null ) {
		var u2c = findNode(u2.id, sc);
		if( u2c ) u2c.className = 'l2';
	}
	m2 = null;

	setCookie(menus[0], 'mid');
}

function hideMenus() {
	sc.innerHTML = ''; tc.innerHTML = '';
	sd.style.visibility = 'hidden'; td.style.visibility = 'hidden';
	m1 = null; m2 = null;
	s.className = 'htd';
	t.className = 'htd';
	if (u1 != null) 
		u1.className = 'll';
	if (u2 != null) 
		u2.className = 'l2';
	unsetCookie('mid');
}

function setCookie(cValue, cookieID) {
	var NameOfCookie = cookieID;
	document.cookie = NameOfCookie + "=" + escape(cValue);
}

function unsetCookie(cookieID) {
	setCookie (null, cookieID);
}

function getCookie(cookieID) {
	var dc = document.cookie, prefix = cookieID + "=", begin = dc.indexOf("; " + prefix);
	if (begin == -1) { 
		begin = dc.indexOf(prefix); 
		if (begin != 0) 
			return null; 
	}
	else 
		begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) 
		end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}

function findNode(id, parent) {
	if( parent.id == id ) return parent;
	for( var i = 0; i < parent.childNodes.length; i++ )
		if( parent.childNodes[i].id == id )
			return parent.childNodes[i];
		else {
			var found = findNode(id, parent.childNodes[i]);
			if( found ) return found;
		}
	return null;
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;  
	if (!d) 
		d = document; 
	if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
	    d = parent.frames[n.substring(p+1)].document; 
		n = n.substring(0,p);
	}
	if (!(x = d[n]) && d.all) 
		x = d.all[n]; 
	for (i = 0; !x && i < d.forms.length; i++) 
		x = d.forms[i][n];
	for (i = 0; !x && d.layers && i < d.layers.length; i++) 
		x = MM_findObj(n, d.layers[i].document);
	if(!x && d.getElementById) 
		x = d.getElementById(n); 
	return x;
}
