jQuery(document).ready(function(){	
	$(".fancybox").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'fade',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false
	});
	$.cookie('openedmenu', 0);
	/* Ciasteczkowa obsługa zapamiętywania otwartych menu */
	var c = $.cookie('openedmenu');
	$('.sublevel').each(function(){
		if(c === null || c.indexOf($(this).attr('id')) == -1)
		{
			$(this).hide();
		}
	});
	$('.submenucontainer').delegate('li.toplevel a', 'click', function(ev){
		ev.preventDefault();
		var $anchor = $(this);
		var $li = $anchor.closest('li');
		//Pobierz wszystkie elementy dla menu głównych
		var all_links = new Array();
		var i = 0;
		$('li.toplevel').each(function(el){
			all_links[i] = $(this).attr('id');	
			i++;
		});
		for(var i=0; i<all_links.length; i++)
		{
			if(all_links[i] != $li.attr('id'))
			{
				var $k = $('#'+all_links[i]).closest('li');
				if($k.nextAll().is(':visible'))
		 		{
					openMenu($('#'+all_links[i]));
				}
			}
		}
		openMenu($(this));	
	});
	
	function openMenu(el)
	{		
		 var $anchor = el;
		 var $li = $anchor.closest('li');
		 if($li.nextAll().is(':visible'))
		 {
			 //$anchor.width($anchor.width()-25);
			 
			 var id = $li.nextAll().eq(-1).attr('id');
			 var c = $.cookie('openedmenu');
			 
			 if(c !== null && c.indexOf("|") != -1)
			 {
				 if(c.indexOf("|"+id) != -1)
				 {
					 $.cookie('openedmenu', c.replace("\|"+id, ""));
				 }
				 else
				 {
					$.cookie('openedmenu', c.replace(id+"\|", "")); 
				 }
			 }
			 else if(c !== null && c.indexOf("|") == -1)
			 {
				 $.cookie('openedmenu', null)
			 }
		 }
		 else
		 {
			 var id = $li.nextAll().eq(-1).attr('id');
			 var c = $.cookie('openedmenu');
			 (c === null) ? $.cookie('openedmenu',id) : $.cookie('openedmenu',c+'|'+id);	
		 
		 }
		 $li.nextAll().slideToggle(600);
	}
	
	//Submenu
	$.cookie('openedsubmenu', 0);
	/* Ciasteczkowa obsługa zapamiętywania otwartych menu */
	var c = $.cookie('openedsubmenu');
	$('.bottomlevel').each(function(){
		if(c === null || c.indexOf($(this).attr('id')) == -1)
		{
			$(this).hide();
		}
	});
	$('.bottommenucontainer').delegate('li.midlevel a', 'click', function(ev){
		ev.preventDefault();
		var $anchor = $(this);
		var $li = $anchor.closest('li');
		//Pobierz wszystkie elementy dla menu głównych
		var all_links = new Array();
		var i = 0;
		$('li.midlevel').each(function(el){
			all_links[i] = $(this).attr('id');	
			i++;
		});
		for(var i=0; i<all_links.length; i++)
		{
			if(all_links[i] != $li.attr('id'))
			{
				var $k = $('#'+all_links[i]).closest('li');
				if($k.nextAll().is(':visible'))
		 		{
					openSubMenu($('#'+all_links[i]));
				}
			}
		}
		openSubMenu($(this));	
	});
	
	function openSubMenu(el)
	{		
		 var $anchor = el;
		 var $li = $anchor.closest('li');
		 if($li.nextAll().is(':visible'))
		 {
			 //$anchor.width($anchor.width()-25);
			 
			 var id = $li.nextAll().eq(-1).attr('id');
			 var c = $.cookie('openedsubmenu');
			 
			 if(c !== null && c.indexOf("|") != -1)
			 {
				 if(c.indexOf("|"+id) != -1)
				 {
					 $.cookie('openedsubmenu', c.replace("\|"+id, ""));
				 }
				 else
				 {
					$.cookie('openedsubmenu', c.replace(id+"\|", "")); 
				 }
			 }
			 else if(c !== null && c.indexOf("|") == -1)
			 {
				 $.cookie('openedsubmenu', null)
			 }
		 }
		 else
		 {
			 var id = $li.nextAll().eq(-1).attr('id');
			 var c = $.cookie('openedsubmenu');
			 (c === null) ? $.cookie('openedsubmenu',id) : $.cookie('openedsubmenu',c+'|'+id);	
		 
		 }
		 $li.nextAll().slideToggle(600);
	}
});
