// Bold menu items that match the URL

"use strict";

// get location without hash portion
var window_location_nohash = window.location.protocol + '//' +
			     window.location.hostname +
			     window.location.pathname;

function markActiveMenuLink()
{
	/*
	 * This is tricky because we want the item to be clickable
	 * only if it will take us to a new href, and we can't test
	 * this on page load because it can have a hash that was added
	 * 'in-page' by clicking on a hash link.
	 */
	if (this.href == window.location.href)
	{
		this.blur();
		return false;
	}
	else
		return true;
}

function markActiveMenuItem(parentObj)
{
	if (!parentObj)
		return;

	var aList = parentObj.getElementsByTagName('a');
	var a_len = aList.length;

	for (var a_num = 0; a_num < a_len; a_num++)
	{
		var aObj = aList[a_num];

		// 'li' first child is 'a', is 'inactive', and matches window URL
		// do most restrictive check first
		if (aObj.href == window_location_nohash &&
		    aObj.className == 'inactive')
		{
			aObj.className = 'active';
			/* Call function this way so 'this' is passed properly. */
			aObj.onclick = markActiveMenuLink;
			// no more matches in this 'ul'
			break;
		}
	}
}

function markActiveMenuItems()
{
	// handle sidebar, which is a div
	var sidebarObj = document.getElementById('sidebar-menu');
	if (sidebarObj)
		markActiveMenuItem(sidebarObj);

	// find all years menus
	// There are fewer 'ul's than 'div's, so we put classes on those
	var ulList = document.getElementsByTagName('ul');
	var ul_len = ulList.length;
	for (var ul_num = 0; ul_num < ul_len; ul_num++)
		if (ulList[ul_num].className == 'years-menu')
			markActiveMenuItem(ulList[ul_num]);
}

markActiveMenuItems();
