var sg_isAdmin = false;

// main menu
var loadedNodes = new Object();
var hideUniqNodes = new Array();
var nodesInfo = new Object();

function setChildrenUniqNames(uniqName)
{
	for (var i in loadedNodes)
	{
		if (i == uniqName)
		{
			$A(loadedNodes[i]).each(function(n)
			{
				hideUniqNodes.push(n);
				setChildrenUniqNames(n);
			});
		}
	}
}

function switchNodes(event)
{
	Event.stop(event);
	var elm = Event.element(event);

	while (elm.id.empty() && !elm.id.match(/menuLeft_/))
	{
		elm = elm.up();
	}

	var uniqName = elm.id.substr(9);

	if (nodesInfo[uniqName]['open'])
	{
		hideNodes(uniqName);
	}
	else
	{
		showNodes(uniqName);
	}
}

function hideNodes(uniqName)
{
	setChildrenUniqNames(uniqName);
	nodesInfo[uniqName]['closed'] = true;
	nodesInfo[uniqName]['open'] = false;
	hideUniqNodes.each(function(n)
	{
		if ($('menuLeft_' + n))
		{
			$('menuLeft_' + n).hide();

			if (!nodesInfo[n])
				nodesInfo[n] = new Object();
		}
	});

	hideUniqNodes.clear();
}

function showNodes(uniqName)
{
	var ids = loadedNodes[uniqName];
	nodesInfo[uniqName]['closed'] = false;
	nodesInfo[uniqName]['open'] = true;
	ids.each(function(n)
	{
		if ($('menuLeft_' + n))
		{
			$('menuLeft_' + n).show();

			nodesInfo[n]['closed'] = false;
			nodesInfo[n]['open'] = true;
		}
	})
}

function getChildren(event)
{
	Event.stop(event);
	var elm = Event.element(event);
	elm.stopObserving('click', getChildren);

	elm.observe('click', switchNodes);

	while (elm.id.empty() && !elm.id.match(/menuLeft_/))
	{
		elm = elm.up();
	}

	var uniqName = elm.id.substr(9);
	var req = new JsHttpRequest();
	req.onreadystatechange = function()
	{
		if (req.readyState == 4)
		{
			if (req.responseJS)
			{
				new Insertion.After("menuLeft_" + uniqName, req.responseJS.children);
				var mas = $A(req.responseJS.childrenArray);
				loadedNodes[uniqName] = new Array();
				nodesInfo[uniqName] = new Object();
				nodesInfo[uniqName]['open'] = true;
				nodesInfo[uniqName]['closed'] = false;
				mas.each(function(n)
				{
					loadedNodes[uniqName].push(n.uniqName);
				});
			}

		/*if (req.responseText)
		{
			$("debug").innerHTML = req.responseText;
		}*/
		}
	}

	req.open(null, 'form.POST ajax/menu.php', true);
	req.send(
	{
		action: "getChildren",
		ajax: "1",
		uniqName: uniqName
	});
}
// main menu