function checkAll(elm, name)
{
	$$('input[name="'+name+'"]').each(function (n){
		n.checked = elm.checked;
	});
}

function uncheckMain(elm, name)
{
	if (!elm.checked)
		$(name).checked = false;
	else if (isAllChecked(elm.name))
		$(name).checked = true;
}

function isAnyChecked(name, messageConfirm, messageNothing)
{
	if ($$('input[name="'+name+'"]').any(function (n){
		if (n.checked)
			return true;
	}))
	{
		if (confirm(messageConfirm))
			return true;
	}
	else
		alert(messageNothing);

	return false;
}

function isAllChecked(name)
{
	if ($$('input[name="'+name+'"]').all(function (n){
		if (n.checked)
			return true;
	}))
	{
		return true;
	}

	return false;
}

function redirectToLogout()
{
	var re = /\?/;
	var sym = '?';
	if (re.test(window.location.href))
		sym = '&';

	window.location.href = window.location.href + sym + 'logout=1';
}

var azMenu = Class.create(
{
	initialize: function(initCssSelector, templateFileName, options)
	{
		options = options || {};
		this.options = options;

		this.options.effectHide = options.effectHide || 'hide';
		this.options.effectShow = options.effectShow || 'show';
		var f = this.onClick;
		this.initCssSelector = initCssSelector;
		this.templateFileName = templateFileName;

		$$(this.initCssSelector).each(function(elm)
		{
			elm._tmpRef = f.bindAsEventListener(this, elm);
			elm.observe('click', elm._tmpRef);
		}, this);
	},

	onClick: function(ev)
	{
		var data = $A(arguments);
		data.shift();
		var elm = data.first();

		// не делаем переход по ссылке
		if (elm.hasClassName('noContent'))
			Event.stop(ev);

		// простое прятание всех дочерних элементов
		if (elm.hasClassName('noContent') && elm.hasClassName('inSelectedTree'))
		{
			var currentLevel = this.getLevel(elm);

			var hide;
			if (elm.hided)
			{
				hide = false;
				elm.hided = false;
			}
			else
			{
				elm.hided = true;
				var hide = true;
			}

			elm.nextSiblings().each(function(sib)
			{
				var level = this.getLevel(sib);

				if (level > currentLevel)
				{
					if (hide)
					{
						sib.hided = true;
						eval('sib.'+this.options.effectHide+'();');
					}
					else if (level == currentLevel + 1 )
					{
						eval('sib.'+this.options.effectShow+'();');
					}
				}
			}, this);
		}
		// аякс запрос на вывод дочерних
		else if (elm.hasClassName('noContent'))
		{
			var uniqName = this.getUniqName(elm);
			var obj = this;
			new AjaxRequest('core/_ajax/public.php',
			{
				parameters : {'ajax':1, 'action':'getChildren', 'uniqName':uniqName, 'templateFileName':obj.templateFileName},
				onSuccess : function(tr)
				{
					$$(obj.initCssSelector).each(function(elm)
					{
						elm.stopObserving('click', elm._tmpRef);
					}, this);

					elm.insert({after:tr.children});
					elm.addClassName('inSelectedTree');
				}
			});
		}

		return false;
	},

	getLevel : function(elm)
	{
		var level = 0;
		var matches;
		elm.classNames().each(function(cl){
			if (matches = cl.match(/^child(\d+)$/))
			{
				level = new Number(matches[1]);
			}
		});

		return level;
	},

	getUniqName : function(elm)
	{
		var matches;

		if (elm.tagName.toLowerCase() != 'a')
		{
			elm = elm.select('a').first();
		}

		if (matches = elm.href.match(/\/([^\/]+)\.htm$/))
			return matches[1];
	}
});
