$(document).ready
(
	function()
	{
		var minIndent = 5; //smallest allowed indenting - if menu still too wide go back to initial and allow wrapped link text
		var colorFactor = 0.9; //multiplicative factor for modifying colors of submenus
		var navTimerId;
		$('div.navSideElementHasChildren')
		.mouseenter
		(
			function()
			{
				$(this).children('span').addClass('navSideElementOver');
				if ($(this).children('div.navSideSubmenuHide').is(':visible'))
				{
					clearTimeout(navTimerId);
				}
			}
		)
		.mouseleave
		(
			function()
			{
				$(this).children('span').removeClass('navSideElementOver');
				if ($(this).children('div.navSideSubmenuHide').is(':visible'))
				{
					clearTimeout(navTimerId);
					navTimerId = setTimeout("unExpand()",2000);
				}
			}
		)
		.click
		(
			function(event)
			{
				event.stopPropagation(); //so click event of parent navSideElement, if present, which would trigger hide() function on this navSideElement, is not triggered
				clearTimeout(navTimerId);
				if ($(this).children('div.navSideSubmenuHide:hidden').length == 0) //corresponding submenu not hidden
				{
					$('div.navSideSubmenuHide').not($(this).parents()).hide(500); //hide corresponding submenu but not its parent, which was just clicked
					$(this).css('backgroundColor','transparent'); //remove darkened background color of element just clicked that corresponded to background color of corresponding submenu that just got hidden
				}
				else
				{
					$('div.navSideSubmenuHide').not($(this).parents()).hide(500); //hide submenu that has parent other than one just clicked if any
					$('div.navSideElement').not($(this).parents()).css('backgroundColor','transparent'); //remove darkened background color of parent element of submenu just hidden if any that corresponded to background color of that submenu
					$(this)
					.css
					(
						'backgroundColor',
						function()
						{
							var coloredParent;
							$(this).parents().each
							(
								function()
								{
									if ($(this).css('backgroundColor') != 'transparent')
									{
										coloredParent = this;
										return false;
									}
								}
							);
							var colorArray = $(coloredParent).css('backgroundColor').toString().replace(/ /g,'').replace('rgb(','').replace(')','').split(',');
							for (i=0; i<3; i++) colorArray[i] = parseInt(colorArray[i]*colorFactor);
							return 'rgb(' + colorArray[0] + ',' + colorArray[1] + ',' + colorArray[2] + ')';
						}
					)
					.children('div.navSideSubmenuHide')
					.show(500)
					.css
					(
						'backgroundColor',
						function()
						{
							return $(this).parent().css('backgroundColor');
						}
					)
					;
				}
			}
		)
		.children('div.navSideSubmenu')
		.addClass('navSideSubmenuHide')
		.css
		(
			"left",
			function()
			{
				return($(this).parent().children('span').width() + parseInt($(this).parent().css('paddingLeft').replace("px","")));
			}
		)
		.hide()
		;

		var minContentHeight = $('div.navSide').height();
		$('div.content').css('minHeight',minContentHeight);

		var desiredWidth = parseInt($('div.navSide').css("minWidth").replace("px",""));
		var initialIndent = 0;
		if ($('div.navSideSubmenu').not('div.navSideSubmenuHide').length > 0) initialIndent = parseInt($('div.navSideSubmenu').not('div.navSideSubmenuHide').css('paddingLeft').replace("px","")); //otherwise irrelevant - all submenus are hidden so indenting is irrelevant
		if ($('div.navSide').width() > desiredWidth) //menu is too wide
		{
			for (var i=initialIndent; i>=minIndent; i--) //no iterations if initial indent is 0, i.e., indenting is irrelevant
			{
				$('div.navSideSubmenu').not('div.navSideSubmenuHide').css("paddingLeft",i);
				if ($('div.navSide').width() == desiredWidth) break;
			}
			if ($('div.navSide').width() > desiredWidth) //indenting is at minimum or irrelevant but menu is still too wide
			{
				$('div.navSide').css('whiteSpace','normal'); //allow link text to wrap onto another line
				$('div.navSide').css('maxWidth',desiredWidth); //force menu width to be desired width
				$('div.navSideSubmenu').not('div.navSideSubmenuHide').css('paddingLeft',initialIndent); //set indenting back to initial amount
			}
		}
	}
);

function unExpand()
{
	$('div.navSideSubmenuHide').hide(500);
	$('div.navSideElement').css('backgroundColor','transparent');
}

