;(function($) {
	$.fn.menu = function(options) {
		return this.each(function() {
			var $this = $(this);
			var $originalHeight = $this.height();
			
			var $mainNavigation = $("ul.main > li", $this);
			var $mainNavigationLinks = $("ul.main > li > a", $this);
						
			// Navigationsbreite bestimmen
			var $totalWidth = $("#logo").outerWidth();
			$mainNavigation.each(function() {
				$totalWidth += ($(this).outerWidth());
				return null;
			});

			$this.css("width", $totalWidth + "px");
			
			$("ul.submenu-info", $this).css("width", $totalWidth - 220);
			$("ul.mainmenu-info", $this).css("width", $totalWidth - 220);
			//Navigationshöhe bestimmen
			var $totalHeight = 190;

			// Navigationsbackground einfügen
			var $submenuBackgroundHeight = 0;
			$submenuBackgroundHeight = 0; // IE Bug-Fix - Keine Ahnung warum ;-)
			$("ul.submenu").each(function() {
				if ($(this).outerHeight() > $submenuBackgroundHeight) {
					$submenuBackgroundHeight = $(this).outerHeight();
				}
				return null;
			});
			
			$("ul.mainmenu-info").each(function() {
				if ($(this).outerHeight() > $submenuBackgroundHeight) {
					$submenuBackgroundHeight = $(this).outerHeight();
				}
				return null;
			});
			
			$("ul.submenu-info").each(function() {
				if ($(this).outerHeight() > $submenuBackgroundHeight) {
					$submenuBackgroundHeight = $(this).outerHeight();
				}
				return null;
			});
						
			var $submenuBackground = $("<div></div>")
				.attr("id", "subnavigation-background")
				.css("width", $totalWidth + "px").css("height", $submenuBackgroundHeight + "px").appendTo($this);

			// Links mit listener versehen
			$mainNavigationLinks.each(function() {
				$(this).mouseenter(function() {
					$submenuBackground.stop(true,true).show();
					$this.css("height", $totalHeight + "px");
					$("ul.submenu", $this).stop(true,true).hide();
					$("ul.submenu-info", $this).stop(true,true).hide();
					$("ul.submenu",$(this).parent()).stop(true,true).show();
					$("ul.main > li").removeClass("active");
					$("ul.mainmenu-info", $this).stop(true,true).hide();
					var infoText = $("ul.mainmenu-info", $(this).parent());
					infoText.css("top", $submenuBackground.outerHeight() - infoText.outerHeight() + 79).stop(true,true).show();
					
					$(this).parent().addClass("active");
					return null;
				});

				$("ul.submenu > li > a").each(function() {
					$(this).mouseenter(function() {
						$("ul.submenu-info",$this).stop(true,true).hide();
						$("ul.mainmenu-info", $this).stop(true,true).hide();
						var infoText = $("ul.submenu-info", $(this).parent());
						infoText.css("top", $submenuBackground.outerHeight() - infoText.outerHeight()).stop(true,true).show();
						return null;
					});
					$(this).mouseleave(function() {
						return null;
					});
					return null;
				});
				
				$("ul.submenu").mouseleave(function() {
					$("ul.submenu-info",$this).stop(true,true).hide();
					var infoText = $("ul.mainmenu-info", $(this).parent());
					infoText.css("top", $submenuBackground.outerHeight() - infoText.outerHeight() + 79).stop(true,true).show();
					return null;
				});
				return null;
			});
			
			$this.mouseleave(function() {
				$this.css("height", $originalHeight + "px");
				$("ul.main > li").removeClass("active");
				$("ul.submenu", $this).stop(true,true).hide();
				$("ul.mainmenu-info", $this).stop(true,true).hide();
				$submenuBackground.stop(true,true).hide();
				return null;
			});
			return null;
		});
	};
	
	$.fn.menu.defaults = {
	};
})(jQuery);
