
$(document).ready(function() {
	$("ul#hauptmenu li").css("position", "relative")
	
	$.menumerator = function(settings) {
		if($("#navi_wrap").css("position") != "relative") return;
		var config = {
			easing:		"easeOutBack",
			duration:	1000,
			throttle_timeout: 300 // how long to wait after window.resize event was fired
		};
		if(settings) $.extend(config, settings);
		var godott = null;
		var haupt = $("#hauptmenu");

		// replace elements on small screens
		var ww = $(window).width();
		var aktiv = $("#hauptmenu > li.aktiv").attr("id");
		if(ww < 955 && ww >= 874) {
			if(aktiv == "nav5") {
				$("#nav33").removeClass("last").after($("#nav1"));
				$("#nav1").removeClass("first").addClass("last");
				$("#nav2").addClass("first");
			}
		}
		if(ww < 1015 && ww >= 874) {
			if(aktiv == "nav33") {
				$("#nav33").removeClass("last").after($("#nav1"));
				$("#nav1").removeClass("first").after($("#nav2"));
				$("#nav2").addClass("last");
				$("#nav3").addClass("first");
			}
		}
		if(ww < 1150 && ww >=1015) {
			if(aktiv == "nav33") {
				$("#nav33").removeClass("last").after($("#nav1"));
				$("#nav1").removeClass("first").addClass("last");
				$("#nav2").addClass("first");
			}
		}
		if(ww < 1015) {
			if(aktiv == "nav2") {
				$("#nav5").removeClass("last");
				haupt.prepend($("#nav33").addClass("first"));
				$("#nav1").removeClass("first");
			}
		}
		if(ww < 874) {
			if(aktiv == "nav3") {
				$("#nav5").removeClass("last");
				haupt.prepend($("#nav33").addClass("first"));
				$("#nav1").removeClass("first");
			}
		}
		if(ww < 874) {
			if(aktiv == "nav5") {
				$("#nav33").removeClass("last").after($("#nav1"));
				$("#nav1").removeClass("first").after($("#nav2"));
				$("#nav2").addClass("last");
				$("#nav3").addClass("first");
			}
		}

		var nav_container = $("#navigation");
		var fromid = 1;
		var from = null;
		var to = $("#hauptmenu .aktiv");
		var submenu = $("#hauptmenu > li > .submenu"); //nav_container.find(".submenu");
		//var middle = submenu.offset().left + (submenu.outerWidth() / 2);
		//var middle = $(document).width() / 2;
		var middle = nav_container.outerWidth() / 2;
		var frompos = 0;
		var topos = 0;
		var hauptPos = 0;
		var hauptWidth = haupt.outerWidth();

		var _init = function () {

			//console.log(document.cookie);
			var cookie = document.cookie.split("; ");
			$.each(cookie, function() {
				var pair = this.split("=");
				if(pair[0] == "comes_from") {
					fromid = pair[1];
				}
			})
			if(fromid == 0) {fromid = 1;}
			from = $("#nav"+fromid);
			// Den Parent von Submenu ändern
			submenu.appendTo(nav_container);
			haupt.css({opacity: 0}).show(); //.appendTo("body");
			var hwidth = 0;
			$.each($("#hauptmenu > li"), function() {
				hwidth += $(this).outerWidth();
			});
			haupt.width(hwidth);

			frompos = middle - from.outerWidth() / 2;

			hauptPos = frompos - from.position().left ;

			topos = middle - to.outerWidth() / 2;

			$(window).resize(function() {
				if(godott != null) {
					window.clearTimeout(godott);
					godott = null;
				}
				godott = setTimeout(function() {_reanimate();},config.throttle_timeout);
			});

			haupt.css({left: hauptPos+"px", opacity: 1});
			if($.browser.msie) {
				topos -= 0;
				frompos -= 0;
				haupt.css("filter","none");
			}
			_animate(topos - to.position().left);
		}

		var _animate = function(val) {
			haupt.animate(
				{left: val},
				config.duration,
				config.easing
			);
		}

		var _reanimate = function() {
			//middle = $(document).width() / 2;
			//middle = submenu.offset().left + (submenu.outerWidth() / 2);
			middle = nav_container.outerWidth() / 2;
			topos = middle - to.outerWidth() / 2;
			if($.browser.msie) {topos -= 0;}
			_animate(topos - to.position().left);
		}

		//return this;
		_init();
	};

	$("#hauptmenu li a, #submenu li a").bind("mouseover", function(e) {
		e.stopPropagation();
		var me = $(this);
		var parent = me.parent();
		if(!parent.hasClass("aktiv")) {
			var elem = me.clone();
			elem.addClass("hover").css({opacity: 0}).bind("mouseout", function(e) {
				e.stopPropagation();
				$(this).animate(
					{opacity: 0},
					{
						duration: 700,
						easing: "easeInOutQuart",
						complete: function(){
							$(this).remove();
						}
					}
				);
			});
			me.after(elem);
			elem.animate(
				{opacity: 1},
				{
					duration: 700,
					easing: "easeInOutQuart",
					complete: function(){
					}
				}
			)
		}
	});

	$.easing.def = $.easing.easeOutQuad;

	$.menumerator({
		duration: 1500,
		easing: "easeInOutQuart"
	});
})
