// Close all inactive submenus on start, and allow submenu display to toggle

"use strict";

// Must pass 'this' as first parameter because on-click event masks original
// 'this';  http://www.quirksmode.org/js/this.html
function toggleSubmenu(SubMenuName) {
	var DivObj = document.getElementById('submenu-' + SubMenuName).style;
	// find 'a' object for this submenu
	var menuText = document.getElementById('submenu-' + SubMenuName + '-a').firstChild;

	// toggle menu marker display
	if (DivObj.display == 'none')
	{
		// '' resets to defaut
		DivObj.display = '';
		// change arrow direction, replace() does not work, perhaps because entities are already replaced
		menuText.nodeValue = menuText.nodeValue.substring(0, menuText.nodeValue.length-1) + ' ';
	}
	else
	{
		DivObj.display = 'none';
		menuText.nodeValue = menuText.nodeValue.substring(0, menuText.nodeValue.length-1) + '\xBB';
	}

	// needed to remove 'outline' after click
	document.getElementById('submenu-' + SubMenuName + '-a').blur();
}

function closeInactiveSubmenus()
{
	var submenus = new Array();

	// Get list of submenus
	var ulMenu =  document.getElementById('sidebar-menu').getElementsByTagName('ul');
	var len = ulMenu.length;
	for (var i = 0; i < len; i++)
		if (/submenu$/.test(ulMenu[i].className))
			submenus.push(ulMenu[i].id.replace(/^submenu-/, ''));

	// Close inactive submenus based on the window href
	var window_location_href = window.location.href;
	len = submenus.length;

	for (var i = 0; i < len; i++)
	{
		var reg = RegExp('/main/' + submenus[i].replace(/-/g, '/') + '/');
		if (!reg.test(window_location_href))
			toggleSubmenu(submenus[i]);
	}

	/* there is a delay in the update of the submenus when loading blogs */
}

// close inactive menus on start, but only when Javascript is enabled
closeInactiveSubmenus();
