/**
 * @author sovnarkom, Enykey
 */



function toggle(item, behavior)
{
	if (behavior == 0) {
		$(item).find('ul:first').toggle(500)
		$(item).toggleClass('opened');
		
	}
	else {
		if (!$(this).hasClass('act')) {
			$(item).find('ul:first').toggle(0);
			$(item).toggleClass('opened');
		}
	}
	
};

function subtoggle(item, behavior)
{
	var list = $(item).find('ul:first');
	var items = $(item).find('ul:first > li');
	
	if (items.length != 0) {
		if (behavior == 0) list.toggle(500)
		else list.toggle(0);
		$(item).toggleClass('opened');
		return false;
	}
	else {
		return true;
	}
};

$(document).ready(function()
{
	
	
	$('.menu h3 a.link').click(function()
	{
		toggle(this.parentNode.parentNode, 0);
		return false;
	});
	
	$('.menu>li li a').click(function()
	{
		return subtoggle(this.parentNode, 0);
	});
	
	$('.menu li[class*=opened]').each(function()
	{
		toggle(this, 1);
	})
	
		
	 	$('.menu > li').mouseover(function(){
	 		$(this).find('.icon').show(0);
	 	})
	 	
	 	$('.menu>li').mouseout(function(){
	 		if (!$(this).hasClass('act')) {
	 			$(this).find('.icon').hide(0);
	 		}
	 	})
	 

	$('.menu>li').each(function()
		{
			var act = $(this).find('.active');
			
			

			if (act.length != 0)
			{
					if ($(this).find('a').hasClass("act"))
					{
						$(this).find('a').removeClass("act");	
					}
					act.parents('li').each(function(){
						$('.opened').find('span:first').addClass("act");
						$(this).addClass("act");		
						toggle(this,1);
					})
			}
		
			
			;
			
		})

});