/* combine : /javascript/common/wpi-functions.js*/
// Utility js to support (new) responsive headers
var common = (function () {
    "use strict";
    var i = null;

    //Check if child is descendent of parent
    function isDescendant(parent, child) {
        var node = child.parentNode;
        while (node !== null) {
            if (node === parent) {
                return true;
            }
            node = node.parentNode;
        }
        return false;
    }

    //Get Siblings of the passed element
    function getSiblings(elem) {
        var siblings = [],
            sibling = elem.parentNode.firstChild;
        for (i; sibling; sibling = sibling.nextSibling) {
            if (sibling.nodeType === 1 && sibling !== elem) {
                siblings.push(sibling);
            }
        }
        return siblings;
    }

    return {
        isDescendant: isDescendant,
        getSiblings: getSiblings
    };
}());

var windowEvents = (function () {
    "use strict";
    var root = document.getElementsByTagName("html")[0];
    // Detect Touch Device
    function isTouch() {
        if (("ontouchstart" in window) || window.DocumentTouch && document instanceof DocumentTouch) {
            root.classList.add('gui-touch');
        } else {
            root.classList.add('gui-no-touch');
        }
    }
    isTouch();
}());

var guiDropdowns = (function () {
    "use strict";
    var toggle = document.querySelectorAll(".gui-dropdown-toggle"),
        item = document.querySelectorAll(".gui-dropdown-menu a"),
        activeDropdown;
    
    //If click target isn't dropdown toggle close all dropdowns
    var closeDropdown = function (e) {
        if (!e.target.classList.contains("gui-dropdown-toggle")) {
            if (activeDropdown) {
                activeDropdown.parentNode.classList.remove("gui-active");
                activeDropdown.setAttribute("aria-expanded", "false");
                activeDropdown = false;
                window.removeEventListener('click', closeDropdown);
            }
        }
    };

    //Handle Toggle Click
    function handleToggle() {
        if (activeDropdown === this) {
            this.setAttribute("aria-expanded", "false");
            window.removeEventListener('click', closeDropdown);
            activeDropdown = false;
        } else if (activeDropdown) {
            activeDropdown.parentNode.classList.remove("gui-active");
            activeDropdown.setAttribute("aria-expanded", "false");
            this.setAttribute("aria-expanded", "true");
            activeDropdown = this;
            window.removeEventListener('click', closeDropdown);
        } else {
            this.setAttribute("aria-expanded", "true");
            activeDropdown = this;
            window.addEventListener('click', closeDropdown);
        }
        this.parentNode.classList.toggle("gui-active");
    }

    //Handle Selection Click
    function handleSelection() {
        var siblings = common.getSiblings(this.parentNode),
            textValue = activeDropdown.querySelector(".gui-dropdown-toggle-text");

        for(var i = 0; i < siblings.length; i++) {
        	var elem = siblings[i];
            if (elem.classList.contains("gui-active")) {
                elem.classList.remove("gui-active");
            }
        }
        if (textValue) {
            textValue.textContent = this.textContent;
            this.parentNode.classList.add("gui-active");
        }
    }

    //Add Event Listeners
    for(var i = 0; i < toggle.length; i++) {
        var elem = toggle[i];
        elem.addEventListener("click", handleToggle);
    };
    for(var i = 0; i < item.length; i++) {
        var elem = item[i];
        elem.addEventListener("click", handleSelection);
    };

}());

var guiHeader = (function () {
    "use strict";
    // Variables
    var i,
        body = document.querySelector("body"),
        root = document.getElementsByTagName("html")[0],
        wrapper = document.querySelector(".gui-wrapper");

    // Header Constructor
    window.Header = function (userDefaults) {
        var self = this,
            defaults = {
                containerClass: "gui-header",
                mainLinkBreakpoint: 767,
                navBreakpoint: 767,
                stickyDefault: false,
                stickyOffCanvas: false,
                topLinkBreakpoint: 767
            };

        // Create options by extending defaults with the passed in arugments
        if (userDefaults && typeof userDefaults === "object") {
            this.options = extendDefaults(defaults, userDefaults);
        } else {
            this.options = defaults;
        }

        // Set Container
        this.container = document.querySelector("." + this.options.containerClass);
        


        // Set Navigation Type (Off Canvas or Default)
        function setNav() {
            if (window.innerWidth <= self.options.navBreakpoint) {
                self.container.classList.add("gui-nav-off-canvas");
                if (self.container.classList.contains("gui-nav-default")) {
                    self.container.classList.remove("gui-nav-default");
                }
                if (self.options.stickyOffCanvas === true) {
                    self.container.classList.add("gui-header-sticky");
                } else if (self.container.classList.contains("gui-header-sticky")) {
                    self.container.classList.remove("gui-header-sticky");
                }
            } else {
                self.container.classList.add("gui-nav-default");
                if (self.container.classList.contains("gui-nav-off-canvas")) {
                    self.container.classList.remove("gui-nav-off-canvas");
                }
                if (self.options.stickyDefault === true) {
                    self.container.classList.add("gui-header-sticky");
                    mediaQueryMatchList();
                } else if (self.container.classList.contains("gui-header-sticky")) {
                    self.container.classList.remove("gui-header-sticky");
                }
            }

        }

        // Set Navigation Type Onload
        setNav();

        // Set Elements
        this.activeSub = null;
        this.body = document.body;
        this.wrapper = document.querySelector(".gui-wrapper");
        this.currentWindowWidth = window.innerWidth;
        this.headerMainLinks = this.container.querySelector(".gui-header-main-links");
        this.headerTopLinks = document.querySelector(".gui-header-top-links");
        this.mainLinks = this.container.querySelector(".gui-main-links");
        this.nav = this.container.querySelector(".gui-nav");
        this.navListExtra = this.container.querySelector(".gui-nav-list-extra");
        this.navOverlay = this.container.querySelector(".gui-nav-overlay");
        this.prevWindowWidth = window.innerWidth;
        this.search = this.container.querySelector(".gui-search");
        this.searchToggle = this.container.querySelector(".gui-search-toggle");
        this.showNavBtn = this.container.querySelector(".gui-nav-toggle");
        this.subNav = this.container.querySelectorAll(".gui-sub-nav > a");
        this.subNavToggle = this.container.querySelectorAll(".gui-sub-nav-toggle");
        this.topLinks = document.querySelector(".gui-top-links");

        // Toggle Search Visibility
        function toggleSearch() {
            if (self.search.classList.toggle("gui-show")) {
            	self.search.querySelector("[name='searchString']").focus();
            	if(typeof NBROWN !== "undefined" && typeof NBROWN.searchHistoryService !== "undefined" && typeof NBROWN.searchHistoryService.controller !== "undefined"){
            	    NBROWN.searchHistoryService.controller.read();
            	}
            } else {
            	if(typeof NBROWN !== "undefined" && typeof NBROWN.searchHistoryService !== "undefined" && typeof NBROWN.searchHistoryService.controller !== "undefined"){
                    NBROWN.searchHistoryService.controller.close();
                }
            }
        }

        // Position Sub Nav (Default Navigation)
        function positionSubNav(navItem) {
            var posLeft = navItem.offsetLeft,
                navWidth = navItem.parentNode.offsetWidth,
                subContent = navItem.querySelector(".gui-sub-nav-content"),
                subContentWidth = subContent.offsetWidth;
            subContent.style.right = "";
            if (navWidth - posLeft < subContentWidth) {
                subContent.style.right = 0;
            }
        }

        // Show Navigation (Off Canvas)
        function showNav() {
            window.scrolled = window.scrollY;
            self.body.classList.add("gui-lock-canvas");
            self.container.classList.add("gui-nav-open");
            self.nav.addEventListener("transitionend", showNavOverlay);
            if(self.wrapper != null) self.wrapper.scrollTo(0, window.scrolled);
        }

        // Show Navigation Overlay (Off Canvas)
        function showNavOverlay() {
            self.navOverlay.style.opacity = 0.75;
            self.navOverlay.addEventListener("click", hideNavOverlay);
            self.nav.removeEventListener("transitionend", showNavOverlay);
        }

        // Hide Navigation Overlay (Off Canvas)
        function hideNavOverlay() {
            self.navOverlay.style.opacity = 0;
            self.navOverlay.addEventListener("transitionend", hideNav);
        }

        // Hide Navigation (Off Canvas)
        function hideNav() {
            var activeSubNav = document.querySelector(".gui-sub-nav.gui-show");
            self.body.classList.remove("gui-lock-canvas");
            self.container.classList.remove("gui-nav-open");
            self.navOverlay.removeEventListener("transitionend", hideNav);
            window.scrollTo(0, window.scrolled);
            if (activeSubNav != null ) activeSubNav.classList.remove("gui-show");
        }

        function closeNav() {
            self.activeSub.parentNode.classList.remove("gui-show");
            self.activeSub = null;
        }

        // Setup Main & Top Links
        if (this.container.contains(this.mainLinks)) {
            moveLinks(this.options.mainLinkBreakpoint, this.headerMainLinks, this.navListExtra, this.mainLinks);
        }
        if (this.container.contains(this.topLinks)) {
            moveLinks(this.options.topLinkBreakpoint, this.headerTopLinks, this.navListExtra, this.topLinks);
        }

        // Event Listeners

        // Search Toggle
        if (this.searchToggle) {
            this.searchToggle.addEventListener("click", toggleSearch);
        }

        // Navigation
        if (this.nav) {
            window.addEventListener("resize", function () {
                self.currentWindowWidth = window.innerWidth;

                // Check If Crossed Nav Breakpoint if So Reset Nav & Minibag & Set to Correct Nav State
                if ((self.currentWindowWidth <= self.options.navBreakpoint) && (self.prevWindowWidth > self.options.navBreakpoint) || (self.currentWindowWidth > self.options.navBreakpoint) && (self.prevWindowWidth <= self.options.navBreakpoint)) {
                    if (self.nav.classList.contains("gui-nav-open")) {
                        self.activeSub = null;
                        hideNavOverlay();
                        hideNav();
                        hideMinibag();
                        hideMinibagOverlay();
                    }
                    setNav();
                }

                // Check If Crossed Main Link Breakpoint & Put Main Links in correct place
                if (self.container.contains(self.mainLinks)) {
                    if ((self.currentWindowWidth <= self.options.mainLinkBreakpoint) && (self.prevWindowWidth > self.options.mainLinkBreakpoint) ||
                    (self.currentWindowWidth > self.options.mainLinkBreakpoint) && (self.prevWindowWidth <= self.options.mainLinkBreakpoint)) {
                        moveLinks(self.options.mainLinkBreakpoint, self.headerMainLinks, self.navListExtra, self.mainLinks);
                    }
                }

                // Check If Crossed Top Link Breakpoint & Put Top Links in correct place
                if (document.contains(self.topLinks)) {
                    if ((self.currentWindowWidth <= self.options.topLinkBreakpoint) && (self.prevWindowWidth > self.options.topLinkBreakpoint) ||
                    (self.currentWindowWidth > self.options.topLinkBreakpoint) && (self.prevWindowWidth <= self.options.topLinkBreakpoint)) {
                        moveLinks(self.options.topLinkBreakpoint, self.headerTopLinks, self.navListExtra, self.topLinks);
                    }
                }

                self.prevWindowWidth = window.innerWidth;
            });
            this.showNavBtn.addEventListener("click", showNav);
        }

        // Sub Nav Toggles
        for (i = 0; i < this.subNav.length; i += 1) {
            this.subNav[i].addEventListener("mouseover", function () {
                positionSubNav(this.parentNode);
            });
            this.subNav[i].addEventListener("click", handleNavClick);
        }

        // Handle Nav Click
        function handleNavClick(e) {
            if (self.container.classList.contains("gui-nav-off-canvas")) {
                e.preventDefault();
                var parent = this.parentNode,
                    siblings = common.getSiblings(parent);

                for (i = 0; i < siblings.length; i += 1) {
                    if (siblings[i].classList.contains("gui-show")) {
                        siblings[i].classList.remove("gui-show");
                    }
                }
                parent.classList.toggle("gui-show");
                parent.scrollIntoView({ behavior: 'smooth', block: "start" });
            } else if (root.classList.contains('gui-touch')) {
                self.closeBtn = this.parentNode.querySelector(".gui-btn-close");
                e.preventDefault();
                if (this === self.activeSub) {
                    window.location = this.getAttribute("href");
                } else {
                    if (self.activeSub) {
                        self.activeSub.parentNode.classList.remove("gui-show");
                    }
                    self.activeSub = this;
                    this.parentNode.classList.add("gui-show");
                    positionSubNav(this.parentNode);
                    self.closeBtn.addEventListener("click", closeNav);
                }
            }
        }
    };

    // Private Methods

    // Extend Defaults
    function extendDefaults(source, properties) {
        var property;
        for (property in properties) {
            if (properties.hasOwnProperty(property)) {
                source[property] = properties[property];
            }
        }
        return source;
    }

    // Move Links
    function moveLinks(breakpoint, headerLinkContainer, navLinkContainer, links) {
        if (window.innerWidth <= breakpoint) {
            if (headerLinkContainer.firstChild) {
                navLinkContainer.appendChild(links);
            }
        } else {
            if (navLinkContainer.firstChild) {
                headerLinkContainer.appendChild(links);
            }
        }
    }
    
    // Intersection Observer
    function intObsSticky() {
    	
	    var stickyContainer = document.querySelector(".gui-header-sticky");
	    var topContainer = document.querySelector(".gui-header-top");
	    var topContainerHeight = topContainer.offsetHeight;

	    // Intersection Observer for the Header Top
	    var observerTop = new IntersectionObserver(
	        ([e]) => e.target.classList.toggle('gui-header-top-hidden', e.intersectionRatio < 1),
	    	{
	    		root: null,
	    		rootMargin: topContainerHeight + "px 0px 0px 0px",
	    		threshold: [1]
	    	}
	    );
	    observerTop.observe(topContainer);

	    // Intersection Observer for the Header Sticky Navigation
	    var observerSticky = new IntersectionObserver(
	    	([e]) => e.target.classList.toggle('gui-header-stuck', e.intersectionRatio < 1),
	    	{
	    		root: null,
	    		rootMargin: "-1px 0px 0px 0px",
	    		threshold: [1]
	    	}
	    );
	    observerSticky.observe(stickyContainer);

	    // Heights for the Column content in the Dropdown Navigation
	    function activateDropHeight() {
			var windowInnerHeight = window.innerHeight;
			var headerHeightSticky = document.querySelector('.gui-header').offsetHeight;
			var topLinksHidden = topContainer.classList;
			var bottomSpace;
			var navContainer = document.querySelector('.gui-sub-nav-content');
			var navContainerColumns = document.getElementsByClassName('gui-sub-nav-content-grid');
			var navContainerPaddingTop = parseInt(window.getComputedStyle(navContainer).getPropertyValue('padding-top'));
			var navContainerPaddingBottom = parseInt(window.getComputedStyle(navContainer).getPropertyValue('padding-bottom'));
			var navContainerPadding = navContainerPaddingTop + navContainerPaddingBottom;
			var topHeight = topContainerHeight === 0 ? '40' : topContainerHeight;

			if (topLinksHidden.contains('gui-header-top-hidden')) {
				bottomSpace = topHeight;
			} else {
				bottomSpace = topHeight * 2;
			}

			var navDropdownMaxHeight = windowInnerHeight - (headerHeightSticky + bottomSpace + navContainerPadding);
			
			for(var i = 0; i < navContainerColumns.length; i++)
			{
				navContainerColumns[i].style.maxHeight = navDropdownMaxHeight + "px";
			}
	    }

	    activateDropHeight();

	    // Mutation Observer  for the Header Top
	    var observeHeader = new MutationObserver(function (event) {
			activateDropHeight();
		});

		observeHeader.observe(topContainer, {
			attributes: true, 
			attributeFilter: ['class'],
			childList: false, 
			characterData: false
		});
    }

    // Media Query breakpoints
	var mediaQueryXs = window.matchMedia('(min-width: 768px) and (max-width: 1023px)');
	var mediaQueryLg = window.matchMedia('(min-width: 1024px)');

	// Media Query breakpoint (Xs)
	function mediaQueryMatchesXs() {
		if (mediaQueryXs.matches) {
		    intObsSticky();
		} else {
			return;
		}
	}
	
	// Media Query breakpoint (Lg)
	function mediaQueryMatchesLg() {
		if (mediaQueryLg.matches) {
			intObsSticky();
		} else {
			return;
		}
	}

	// Media Query breakpoint Event Listeners
	function mediaQueryMatchList() {
		if (mediaQueryXs.addEventListener || mediaQueryLg.addEventListener) {
			mediaQueryXs.addEventListener('change', mediaQueryMatchesXs);
			mediaQueryLg.addEventListener('change', mediaQueryMatchesLg);
		} else {
			mediaQueryXs.addListener(mediaQueryMatchesXs);
			mediaQueryLg.addListener(mediaQueryMatchesLg);
		}
		mediaQueryMatchesXs(mediaQueryXs);
		mediaQueryMatchesLg(mediaQueryLg);
	}
    
}());/* complete : /javascript/common/wpi-functions.js*/


