function initMenu()
{
	var menu = document.getElementById('catMenu');
	if (menu)
	{
		for (var i = 0; i < menu.childNodes.length; i++)
		{
			var cn = menu.childNodes[i];
			if (cn.tagName == 'LI')
			{
				parseLI(cn);
			}
		}
	}
}

function parseUL(ul)
{
	for (var i = 0; i < ul.childNodes.length; i++)
	{
		var cn = ul.childNodes[i];
		if (cn.tagName == 'LI')
		{
			parseLI(cn);
		}
	}
}
function parseLI(li)
{
	for (var i = 0; i < li.childNodes.length; i++)
	{
		var cn = li.childNodes[i];
		if (cn.tagName == 'UL')
		{
			if (cn.className != 'unfolded')
				cn.className = 'folded';
			parseUL(cn);
		}
		else if (cn.tagName == 'A' && li.getElementsByTagName('UL').length > 0)
		{
//			debugOut('a, '+li.getElementsByTagName('UL').length+', class='+cn.href, 1, 1);
			if (cn.tagName == 'A' && cn.className == 'noLink')
			{
				cn.onclick = function toggleSubmenu()
				{
					slideLI(li);
					return false;
				}
				cn.href='#';
			}
		}
		else if (cn.tagName == 'SPAN' && cn.className == 'plusMin')
		{
			var a = cn.childNodes[0];
			var imgs = cn.getElementsByTagName('IMG');
			if (imgs[0].className != 'initOpen')
			{
				imgs[0].src = imgs[0].src.replace('min.gif', 'plus.gif');
			}
			a.onclick = function toggleSubmenu()
			{
				slideLI(li);
				return false;
			}
		}
	}
}

function slideLI(li)
{
	for (var i = 0; i < li.childNodes.length; i++)
	{
		var cn = li.childNodes[i];
		if (cn.tagName == 'UL')
		{
			if (cn.className == 'folded')
			{
				var curHeight = getCSSValue(li, 'height', true);
				cn.className = 'unfolded';
				var newHeight = getCSSValue(li, 'height', true);
				li.style.height = curHeight+'px';
				togglePlusMin(li);
				slideOut(li, curHeight, curHeight, newHeight);
			}
			else
			{
				var curHeight = getCSSValue(li, 'height', true);
				var newHeight = 16; // getCSSValue(li, 'height', true);
				li.style.height = curHeight+'px';
				togglePlusMin(li);
				slideIn(li, cn, curHeight, curHeight, newHeight);
			}
		}
	}
}

function slideOut(li, orgHeight, curHeight, maxHeight)
{
	var difMax = parseInt(maxHeight - orgHeight);
	var difCur = parseInt(curHeight - orgHeight);
	var baseSpeed = 2;
	if (difMax == 0 && difCur == 0)
	{
		var addSpeed = ( (50 - baseSpeed) );
	}
	else
	{
		var addSpeed = ( 50 - (baseSpeed * (difCur / difMax) ) );
	}
//	var speed = baseSpeed + addSpeed;
	var speed = 1;
	if (parseInt(curHeight) < parseInt(maxHeight))
	{
		curHeight += 10;
		li.style.height = curHeight+'px';
		setTimeout(function() { slideOut(li, orgHeight, curHeight, maxHeight); }, speed);
	}
	else
	{
		li.style.height = 'auto';
	}
}
function slideIn(li, cn, orgHeight, curHeight, minHeight)
{
	var difMax = parseInt(orgHeight) - parseInt(minHeight);
	var difCur = parseInt(orgHeight) - parseInt(curHeight);
	var baseSpeed = 2;
	if (difMax == 0 && difCur == 0)
	{
		var addSpeed = ( (50 - 50) );
	}
	else
	{
		var addSpeed = ( 50 - (50 * (difCur / difMax) ) );
	}
//	var speed = baseSpeed + addSpeed;
	var speed = 1;
	if (curHeight > minHeight)
	{
		curHeight -= 10;
		li.style.height = curHeight+'px';
		setTimeout(function() { slideIn(li, cn, orgHeight, curHeight, minHeight); }, speed);
	}
	else
	{
		li.style.height = 'auto';
		cn.className = 'folded';
	}
}

function togglePlusMin(li)
{
	var links = li.getElementsByTagName('A');
	var imgs = links[0].getElementsByTagName('IMG');
	var plusmin = imgs[0].src.substr(-8,4);
	if (plusmin == 'plus')
		imgs[0].src = imgs[0].src.replace('plus', 'min');
	else
		imgs[0].src = imgs[0].src.replace('min', 'plus');
}

if (window.addEventListener)
{
	window.addEventListener('load', initMenu, false);
}
else
{
	window.attachEvent('onload', initMenu);
}

function ucFirst(str)
{
	return str.toUpperCase().substring(0, 1)+str.substring(1);
}
function getCSSValue(el, property, int)
{
	if (typeof(int) == 'undefined')
		var int = false;

	if (typeof(window.getComputedStyle) == 'function')
	{
		var styleValue = window.getComputedStyle(el, null)[property];
	}
	else
	{
		var styleValue = el.currentStyle[property];
		var str = 'height';
	}
	if (int)
	{
		if (styleValue == 'auto')
		{
			var offsetVar = 'offset'+ucFirst(property);
			if (typeof(el[offsetVar]) != 'undefined')
			{
				styleValue = el[offsetVar];
			}
		}
		return isNaN(parseInt(styleValue)) ? 0 : parseInt(styleValue);
	}
	else
	{
		return typeof(styleValue) != 'undefined' ? styleValue : '';
	}
}