function fnToggle(number)
{
	var result;
	if (fnIsClosed(number))
	{
		fnCloseAll();
		result = true;
		fnOpen(number);
	}
	else
	{
		result = false;
		fnClose(number);
	}
	return false; // result;
}

function fnIsClosed(number)
{
	return byId("sub" + number).style.display == "none";
}

function fnCloseAll()
{
	var index = 0;
	var control = byId("sub" + index);
	while (control != null)
	{
		if (!fnHasCurrentLink(index) && ! fnIsClosed(index))
		{
			fnClose(index);
		}
		index ++;
		control = byId("sub" + index);
	}		
}

function fnOpen(number)
{
	var control = byId("sub" + number);
	var image = byId("img" + number);
	control.style.display = "block";
	image.src = gimgMinus.src;
}

function fnClose(number)
{
	var control = byId("sub" + number);
	var image = byId("img" + number);
	control.style.display = "none";
	image.src = gimgPlus.src;
}

function fnHasCurrentLink(number)
{
	var path = window.location.pathname;
	if (path.length < 3)
	{
		// default page
		return number == 0; // first group
	}
	else
	{	
		// div[sub].ul.li
		var control = fnFindClosestElementInDeep(document.getElementById("sub" + number), "LI");
		var result = false;
		
		while (control != null)
		{
			var a = fnFindClosestElementInDeep(control, "A");
			if (a != null)
			{
				var value = a.attributes["href"].value;
				if (value && value.length > 0 && value.charAt(value.length - 1) != "#" && (/* ff */path.indexOf(value) >= 0 || /* ie */value.indexOf(path) >= 0))
				{
					result = true;
					break;
				}
			}
			else
			{
				throw "Sub menu link cannot be found.";
			}
			
			control = fnFindNextElement(control);
		}
		return result;
	}
}

function fnFindClosestElementInDeep(control, name)
{
	var result = null;
	control = control.firstChild
	while (control != null)
	{
		while (control != null && control.nodeType != 1) control = control.nextSibling;
		if (control.nodeName == name)
		{
			result = control;
			break;
		}
		else
		{
			control = control.firstChild;
		}
	}
	
	return result;
}

function fnFindNextElement(control)
{
	var result = null;
	control = control.nextSibling;
	while (control != null)
	{
		if (control.nodeType == 1)
		{
			result = control;
			break;
		}
		else
		{
			control = control.nextSibling;
		}
	}
	return result;
}