// JavaScript Document 
$(document).ready(function(){

		$('ul#top_menu ul').each(function(index)
					{	
						$(this).parent().hover(
									  function () {
										  $(this).children().next().fadeIn(400);
										  },
									  function () {
										  $(this).children().next().hide();
										  }
									  )
					});
		
		$('ul#top_menu a').click(function(){
					closeMenu();
				});
		
		$('.ico a').click(function(){
					closeMenu();
				});
				
				
		$("#left_menu a").each(function(){
			var nind=$("#left_menu a").index(this);
			if ($.cookie('submenuMark-' + nind)) { 			
				if($(this).parent("li").parent("ul").length>0){
						recurse_open($(this).parent("li").parent("ul"));
				}
			}
		});
				
		$("#left_menu a").click(function(){
			if($(this).parent("li").children("ul").length>0){
				if($(this).parent("li").children("ul").is(":hidden")){
					$(this).parent("li").parent("ul").find("ul").slideUp("slow");
					$(this).parent("li").children("ul").slideDown("slow");
				}else{
					recurse_close($(this).parent("li").children("ul"));
				}
				return false;
			}else{
				var ind=$("#left_menu a").index(this);
				for(i=0;i<$("#left_menu a").length;i++) cookieDel(i);
				cookieSet(ind);
			}
		});	
			
		function recurse_close(obj){
			obj.slideUp("slow");
			if(obj.find("ul").length>0) recurse_close(obj.find("ul")); 
		}
		
		function recurse_open(obj){
			obj.show();
			if(obj.parent("li").parent("ul").length>0) recurse_open(obj.parent("li").parent("ul")); 
		}
				
		/*		
		$('ul#left_menu .mli').click(function(){
				closeMenu();
			});
		$("ul#left_menu").find("a.mli").click(function(){
					closeMenu();
				});
		$('ul#left_menu ul').each(function(i) { // Check each submenu:
				if ($.cookie('submenuMark-' + i)) {  // If index of submenu is marked in cookies:
					$(this).show(); // Show it (add apropriate classes)
				}else {
					$(this).hide(); // Hide it
				}
				
				$(this).prev().click(function() { // Attach an event listener
				var this_i = $('ul#left_menu ul').index($(this).next()); // The index of the submenu of the clicked link
				if ($(this).next().css('display') == 'none') {
					// When opening one submenu, we hide all same level submenus:
					$(this).parent('li').parent('ul').find('ul').each(function(j) {
						if (j != this_i) {
							$(this).slideUp(400, function () {
								cookieDel($('ul#left_menu ul').index($(this)));
							});
						}
					});
					// :end
				
				$(this).next().slideDown(400, function () { // Show submenu:
					cookieSet(this_i);
				});
			}else {
				$(this).next().slideUp(400, function () { // Hide submenu:
					$(this).prev();
						cookieDel(this_i);
					$(this).find('ul').each(function() {
						$(this).slideDown(400, cookieDel($('ul#left_menu ul').index($(this))));
					});
				});
			}
		return false; // Prohibit the browser to follow the link address
		});
	});
	*/
});

function closeMenu(){
		$('ul#left_menu ul').each(function(i){
		cookieDel(i);
		})	
}

function cookieSet(index) {
	$.cookie('submenuMark-' + index, 'opened', {expires: null, path: '/'}); // Set mark to cookie (submenu is shown):
}
function cookieDel(index) {
	$.cookie('submenuMark-' + index, null, {expires: null, path: '/'}); // Delete mark from cookie (submenu is hidden):
}

