$(document).ready(function () {

	var $ls = $("#language-select");
	var $mls = $("#mobile-language-select")
	var $mainMenu = $("#main-menu");
	var $mobileMenu = $("#mobile-menu");
	var $mobileMenuButton = $("#mobile-menu-button");
	
	// float main menu items
	$mainMenu.find(".right").parent().addClass("right");
	
	// set all top level containers
	$mainMenu.find(">ul>li>ul").addClass("top-level");
	
	// set all sub menu containers
	$mainMenu.find(".top-level>li>ul").addClass("sub-level first");
	$mainMenu.find(".sub-level.first>li>ul").addClass("sub-level second");
	
	// make all of the first list items hovered
	$mainMenu.find(".top-level").find(" > li:eq(0)").addClass("hover");
	
	/**
		handle top level menu hovering
	*/
	$mainMenu.on("mouseenter", ">ul>li", function () {
		
		if (!$(this).hasClass("right")) adjustMenuHeight($(this).children(".top-level"));
	});
	
	var menuLag = false;
	
	/**
		Top level enter
	*/
	$mainMenu.on("mouseenter", ".top-level > li", function () {
		
		var $li = $(this);
		
		if (menuLag) clearTimeout(menuLag);
		
		menuLag = setTimeout(function () {
			
			// remove all other hovers
			$li.parent().children("li").removeClass("hover");
			
			$li.addClass("hover");
			
			if (!$li.parents("li").hasClass("right")) adjustMenuHeight($li.parents(".top-level"));
		}, 200);
	});
	
	$mainMenu.on("mouseleave", ".top-level > li", function () {
		
		//if (!$(this).parents("li").hasClass("right")) adjustMenuHeight($(this).parents(".top-level"));
	});
	
	$mainMenu.on("mouseenter", ".sub-level > li", function () {
		
		if (menuLag) clearTimeout(menuLag);
		
		$(this).addClass("hover");
		
		adjustMenuHeight($(this).parents(".top-level"));
	});
	
	$mainMenu.on("mouseleave", ".sub-level > li", function () {
		
		$(this).removeClass("hover");
		adjustMenuHeight($(this).parents(".top-level"));
	});
	
	// toggle the dropdown mobile menu being opened or closed
	$("body").on("click", "#mobile-menu-slide-out-sidenav-overlay", function () {$mobileMenuButton.toggleClass("open");});
	
	$mobileMenuButton.sideNav();
	// init sidenav for mobile menu and handle on click events
	$mobileMenuButton.on("click", function () {$mobileMenuButton.toggleClass("open");});
	
	var scrollTopPrev = 0;
	$(window).on("scroll", function () {
	
		// get main menu offset plus its height
		var mmPos = $mainMenu.offset().top+$mainMenu.height();
	
		// get the window scroll position relative to the top
		var windowPos = $(this).scrollTop();
		
		// get window width
		var windowWidth = $(window).width();
		
		// we are scrolling up
		if (windowPos < scrollTopPrev) {
		
			// if we cannot see the main menu while scrolling or if we are on a mobile device
			if (windowPos > mmPos || windowWidth <= 600) {
			
				// display the mobile menu
				if (!$mobileMenu.is(":visible")) $mobileMenu.slideDown();
			}
			else {
			
				// otherwise we hide the mobile menu bar and the mobile menu
				if ($mobileMenu.is(":visible")) {
				
					// hide the mobile menu
					$mobileMenu.hide();
					
					// close the mobile dropdown menu
					$mobileMenuButton.removeClass("open");
				}
			}
		}
		else if ($mobileMenu.is(":visible")) {
		
			// hide the mobile menu
			$mobileMenu.slideUp();
			
			// close the mobile dropdown menu
			$mobileMenuButton.removeClass("open");
		}
		
		// set the prev top
		scrollTopPrev = windowPos;
	});
	
	// handle language select clicks
	$ls.on("click", "li", function () {
		
		var name = $(this).find(".name").text();
		var src = $(this).parent().find("li[name='"+name+"'] img").attr("src");
		
		$(".language-select img").attr("src", src);
	});
	
	$mls.on("click", "li", function () {
		
		var name = $(this).find(".name").text();
		var src = $(this).parent().find("li[name='"+name+"'] img").attr("src");
		$(".language-select img").attr("src", src);
	});
	
	// init search modal
	$('.modal-trigger').leanModal();	
	$("#websearch-modal").children("i").on("click", function () {$("#search").val("");});
	
	/**
		Adjust the top level menu height on sub menu change
	*/
	function adjustMenuHeight ($tl) {
				
		var topLevelItemHeight = $tl.children("li").eq(0).height();
		var topLevelItemAmt = $tl.children("li").length;
		var topLevelHeight = topLevelItemHeight * topLevelItemAmt - 3; // subtract 3 because of border
		
		if ($tl.find("ul:visible").length > 0) {
			
			var heightChange = false;
			
			$tl.find("ul:visible").each(function () {
				
				var subListHeight = $(this).height();
				
				if (subListHeight >= topLevelHeight) {
					
					topLevelHeight = subListHeight;
					heightChange = true;
				}
			});
		
			// if there was a height change adjust the top level
			if (heightChange) $tl.height(topLevelHeight);
			
			// adjust the child lists to match the max height of the menu
			$tl.find("ul:visible").height(topLevelHeight);
		}
	}
});