"use strict"; /* Module: Common AA Javscript Description: Javscript used by all pages. History: 10-02-2017 MKM : Initial coding. 13-03-2017 MKM : Use windowWidth variable to remember the window width and only hide the mobile menu on resize if the page width has actually changed. This is because Chrome on Android fires a window resize event even if the page size hasn't changed, see http://stackoverflow.com/questions/17328742/mobile-chrome-fires-resize-event-on-scroll Also on window resize update the mfToggleText to "Show Menu". */ var windowWidth = 0; // If there's a URL fragment then jump back to the top of the page and then let code further down do a smooth scroll if (window.location.hash) { setTimeout(function() { window.scroll(0, 0); }, 1); } $(document).ready(function() { windowWidth = $(window).width(); // If jumping to an URL fragment then scroll smoothly to it var urlHash = window.location.hash; if (urlHash) { $(window).on("load",function() { var headerHeight = $("header").outerHeight(); $("html, body").animate({ scrollTop: $(urlHash).offset().top - headerHeight }, 1200); }); } // Show the current page on the nav menu as active var pgurl = window.location.href.substr(window.location.href .lastIndexOf("/") + 1); $("#mfMenu li a").each(function() { if ($(this).attr("href") === pgurl || (pgurl === "" && $(this).attr("href") === "index.html")) { $(this).addClass("active"); } }); // Handle mobile menu $("#mfToggleMenu").click(function(e) { e.preventDefault(); $("#mfToggleText").text($("#mfMenu").is(":visible") ? "Show Menu" : "Hide Menu"); $("#mfMenu").slideToggle("fast"); }); }); // Open a new window with a specific width and height function displayWindow(url, width, height) { var fullurl = "http://www.aatravel.co.nz/" + url; window.open(fullurl, "aaPopUp", "width=" + width + ",height=" + height + ",resizable=yes,scrollbars=yes"); } $(window).resize(function() { if($(window).width() != windowWidth) { if(!$("#mfToggleMenu").is(":visible")) { $("#mfMenu").css("display","block"); } else { $("#mfMenu").css("display","none"); } $("#mfToggleText").text("Show Menu"); windowWidth = $(window).width(); } });