// Dodać efekt błysku aktywnego elementu

$(document).ready(function()
{

	$("ul.subsubmenu").hide();
	if( $("ul.subsubmenu li a.active").length > 0 )
	{
		var a = $( $("ul.subsubmenu li a.active").parent() ).parent().children("li").children("a");
		var b = $("ul.subsubmenu li a.active");

		if( a.attr("href") == b.attr("href") && window.location.hash == "#.")
		{
			$(b).parent().parent().slideDown(300, function() {
				$("#submenu ul.subsubmenu li a.active").animate({marginRight:5}, 300);
				window.location.hash = '';
			});
		}
		else
		{
			$(b).parent().parent().show();
			$("#submenu ul.subsubmenu li a.active").animate({marginRight:5}, 300);
		}
	}


	$("#submenu ul.subsubmenu li a.active").click( function(event) {
		event.stopPropagation();
		event.preventDefault();
		$(this).fadeTo( 50, 0.10, function(){ 	$(this).fadeTo( 500, 1) } )
		
	});
	
	$("#submenu ul li a").click( function(event) {

		if( $(this).hasClass("active") )
		{
			event.stopPropagation();
			event.preventDefault();
			$(this).parent().children("ul").children("li").children("a.active").fadeTo( 50, 0.10, function(){ 	$(this).fadeTo( 500, 1) } );
			
			return;
		}

		var subsubmenu = $(this).parent().children("ul");
		if( subsubmenu.length > 0 )
		{
			event.stopPropagation();
			event.preventDefault();
			
			var x = this;
			var submenu_visible = $("ul.subsubmenu:visible");
			
			if( submenu_visible.length > 0 )
			{
				$("ul.subsubmenu:visible").animate({height:0}, 100, function() {
					$("ul.subsubmenu:visible").hide();
					window.location.href = $(x).parent().children("ul").children("li:first").children("a").attr("href")+"#.";
				});
			}
			else
			{
				window.location.href = $(x).parent().children("ul").children("li:first").children("a").attr("href")+"#.";
			}
		}
		else
		{
			// To check paths
			event.stopPropagation();
			event.preventDefault();
			
			if( $(this).parent().parent().parent().parent().parent().attr("id") != 'submenu' )
			{
				var submenu_visible = $("ul.subsubmenu:visible");
				if( submenu_visible.length > 0 )
				{
					var x = this;
					$("ul.subsubmenu:visible").animate({height:0}, 100, function() {
						$("ul.subsubmenu:visible").hide();
						window.location.href = $(x).attr("href");
					});
				}
				else
				{
					window.location.href = $(this).attr("href");
				}
			}
			else
			{
				window.location.href = $(this).attr("href");
			}
		}

	});
});