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

function submitFormToEmail(f)
{
	$$('div.correct,div.error').invoke('remove');

	var valid = new Validation(f, {onSubmit:false,useTitles:true});
	if (!valid.validate())
		return false;

	var p = {};
	p.action = "sendFormToEmail";
	p.ajax = 1;
	p.form = f;

	new AjaxRequest('core/_ajax/public.php',
	{
		parameters : p,

		onSuccess : function(tr)
		{
			if (tr.error)
			{
				var message = new Element('div').update(tr.error);
				message.addClassName('error');
				message.hide();
				f.insert({before:message});

				if(typeof Effect == 'undefined') {
					message.show();
				} else {
					new Effect.Appear(message, {duration : 1 });
				}
			}
			else
			{
				var message = new Element('div').update(tr.message);
				message.addClassName('correct');
				message.hide();
				f.insert({before:message});

				if(typeof Effect == 'undefined') {
					message.show();
				} else {
					new Effect.Appear(message, {duration : 1 });
				}

				f.reset();
			}
		},

		onFailure : function(tr)
		{
			pr(tr.responseText);
		}
	});
}