(function(e)
{
	$.fn.menu = function ()
	{
		var self = $(this);
		$(this).each(function()
		{	
			$(this).css({ height: 'auto' });
			var memory_height = $(this).height()+12;
			$(this).css({height: 0, 'overflow': 'hidden'});
			$(this).parent().css({"cursor": "pointer"}); //$(this).parent().attr("rel", "dicht").css({"cursor": "pointer"});
			
			// when this map should be open at init, open it
			if($(this).parent().attr('rel') == 'open')
			{
				$(this).parent().attr("rel", "open").find("ul").animate({ height: memory_height }, "fast");
				$(this).parent().children('span').html("-");
			}
			
			$(this).parent().click(function (event)
			{
				if(event.originalTarget && event.originalTarget.tagName == 'A') return; // do not collapse when the clicked target was the link
				if(event.srcElement && event.srcElement.tagName == 'A') return;
					
				if ($(this).attr("rel") != "open")
				{
					close_others (self);
				};
				
				if($(this).attr("rel") == "dicht" || $(this).attr("rel").length == 0)
				{
					 $(this).attr("rel", "open").find("ul").animate({ height: memory_height }, "fast");
					 $(this).children('span').html("-");
				}
				else
				{
					$(this).attr("rel", "dicht").find("ul").animate({ height: 0 }, "fast");
					$(this).children('span').html("+");
				}
			});
		});
		
		function close_others (obj)
		{
			obj.parent().each(function ()
			{
				if ($(this).attr("rel") == "open")
				{
					$(this).attr("rel", "dicht").find("ul").animate({ height: 0 }, "fast");
					$(this).find("span").html("+");
				};
			});
		}
	};
})($);



$(document).ready(function ()
{
	$("div.leftmenu ul li ul").menu();
});
