/**
 * File navigation.js.
 *
 * Handles toggling the navigation menu for small screens and enables TAB key
 * navigation support for dropdown menus.
 */
( function() {
    var gridflex_secondary_container, gridflex_secondary_button, gridflex_secondary_menu, gridflex_secondary_links, gridflex_secondary_i, gridflex_secondary_len;

    gridflex_secondary_container = document.getElementById( 'gridflex-secondary-navigation' );
    if ( ! gridflex_secondary_container ) {
        return;
    }

    gridflex_secondary_button = gridflex_secondary_container.getElementsByTagName( 'button' )[0];
    if ( 'undefined' === typeof gridflex_secondary_button ) {
        return;
    }

    gridflex_secondary_menu = gridflex_secondary_container.getElementsByTagName( 'ul' )[0];

    // Hide menu toggle button if menu is empty and return early.
    if ( 'undefined' === typeof gridflex_secondary_menu ) {
        gridflex_secondary_button.style.display = 'none';
        return;
    }

    gridflex_secondary_menu.setAttribute( 'aria-expanded', 'false' );
    if ( -1 === gridflex_secondary_menu.className.indexOf( 'nav-menu' ) ) {
        gridflex_secondary_menu.className += ' nav-menu';
    }

    gridflex_secondary_button.onclick = function() {
        if ( -1 !== gridflex_secondary_container.className.indexOf( 'gridflex-toggled' ) ) {
            gridflex_secondary_container.className = gridflex_secondary_container.className.replace( ' gridflex-toggled', '' );
            gridflex_secondary_button.setAttribute( 'aria-expanded', 'false' );
            gridflex_secondary_menu.setAttribute( 'aria-expanded', 'false' );
        } else {
            gridflex_secondary_container.className += ' gridflex-toggled';
            gridflex_secondary_button.setAttribute( 'aria-expanded', 'true' );
            gridflex_secondary_menu.setAttribute( 'aria-expanded', 'true' );
        }
    };

    // Get all the link elements within the menu.
    gridflex_secondary_links    = gridflex_secondary_menu.getElementsByTagName( 'a' );

    // Each time a menu link is focused or blurred, toggle focus.
    for ( gridflex_secondary_i = 0, gridflex_secondary_len = gridflex_secondary_links.length; gridflex_secondary_i < gridflex_secondary_len; gridflex_secondary_i++ ) {
        gridflex_secondary_links[gridflex_secondary_i].addEventListener( 'focus', gridflex_secondary_toggleFocus, true );
        gridflex_secondary_links[gridflex_secondary_i].addEventListener( 'blur', gridflex_secondary_toggleFocus, true );
    }

    /**
     * Sets or removes .focus class on an element.
     */
    function gridflex_secondary_toggleFocus() {
        var self = this;

        // Move up through the ancestors of the current link until we hit .nav-menu.
        while ( -1 === self.className.indexOf( 'nav-menu' ) ) {

            // On li elements toggle the class .focus.
            if ( 'li' === self.tagName.toLowerCase() ) {
                if ( -1 !== self.className.indexOf( 'gridflex-focus' ) ) {
                    self.className = self.className.replace( ' gridflex-focus', '' );
                } else {
                    self.className += ' gridflex-focus';
                }
            }

            self = self.parentElement;
        }
    }

    /**
     * Toggles `focus` class to allow submenu access on tablets.
     */
    ( function( gridflex_secondary_container ) {
        var touchStartFn, gridflex_secondary_i,
            parentLink = gridflex_secondary_container.querySelectorAll( '.menu-item-has-children > a, .page_item_has_children > a' );

        if ( 'ontouchstart' in window ) {
            touchStartFn = function( e ) {
                var menuItem = this.parentNode, gridflex_secondary_i;

                if ( ! menuItem.classList.contains( 'gridflex-focus' ) ) {
                    e.preventDefault();
                    for ( gridflex_secondary_i = 0; gridflex_secondary_i < menuItem.parentNode.children.length; ++gridflex_secondary_i ) {
                        if ( menuItem === menuItem.parentNode.children[gridflex_secondary_i] ) {
                            continue;
                        }
                        menuItem.parentNode.children[gridflex_secondary_i].classList.remove( 'gridflex-focus' );
                    }
                    menuItem.classList.add( 'gridflex-focus' );
                } else {
                    menuItem.classList.remove( 'gridflex-focus' );
                }
            };

            for ( gridflex_secondary_i = 0; gridflex_secondary_i < parentLink.length; ++gridflex_secondary_i ) {
                parentLink[gridflex_secondary_i].addEventListener( 'touchstart', touchStartFn, false );
            }
        }
    }( gridflex_secondary_container ) );
} )();


( function() {
    var gridflex_primary_container, gridflex_primary_button, gridflex_primary_menu, gridflex_primary_links, gridflex_primary_i, gridflex_primary_len;

    gridflex_primary_container = document.getElementById( 'gridflex-primary-navigation' );
    if ( ! gridflex_primary_container ) {
        return;
    }

    gridflex_primary_button = gridflex_primary_container.getElementsByTagName( 'button' )[0];
    if ( 'undefined' === typeof gridflex_primary_button ) {
        return;
    }

    gridflex_primary_menu = gridflex_primary_container.getElementsByTagName( 'ul' )[0];

    // Hide menu toggle button if menu is empty and return early.
    if ( 'undefined' === typeof gridflex_primary_menu ) {
        gridflex_primary_button.style.display = 'none';
        return;
    }

    gridflex_primary_menu.setAttribute( 'aria-expanded', 'false' );
    if ( -1 === gridflex_primary_menu.className.indexOf( 'nav-menu' ) ) {
        gridflex_primary_menu.className += ' nav-menu';
    }

    gridflex_primary_button.onclick = function() {
        if ( -1 !== gridflex_primary_container.className.indexOf( 'gridflex-toggled' ) ) {
            gridflex_primary_container.className = gridflex_primary_container.className.replace( ' gridflex-toggled', '' );
            gridflex_primary_button.setAttribute( 'aria-expanded', 'false' );
            gridflex_primary_menu.setAttribute( 'aria-expanded', 'false' );
        } else {
            gridflex_primary_container.className += ' gridflex-toggled';
            gridflex_primary_button.setAttribute( 'aria-expanded', 'true' );
            gridflex_primary_menu.setAttribute( 'aria-expanded', 'true' );
        }
    };

    // Get all the link elements within the menu.
    gridflex_primary_links    = gridflex_primary_menu.getElementsByTagName( 'a' );

    // Each time a menu link is focused or blurred, toggle focus.
    for ( gridflex_primary_i = 0, gridflex_primary_len = gridflex_primary_links.length; gridflex_primary_i < gridflex_primary_len; gridflex_primary_i++ ) {
        gridflex_primary_links[gridflex_primary_i].addEventListener( 'focus', gridflex_primary_toggleFocus, true );
        gridflex_primary_links[gridflex_primary_i].addEventListener( 'blur', gridflex_primary_toggleFocus, true );
    }

    /**
     * Sets or removes .focus class on an element.
     */
    function gridflex_primary_toggleFocus() {
        var self = this;

        // Move up through the ancestors of the current link until we hit .nav-menu.
        while ( -1 === self.className.indexOf( 'nav-menu' ) ) {

            // On li elements toggle the class .focus.
            if ( 'li' === self.tagName.toLowerCase() ) {
                if ( -1 !== self.className.indexOf( 'gridflex-focus' ) ) {
                    self.className = self.className.replace( ' gridflex-focus', '' );
                } else {
                    self.className += ' gridflex-focus';
                }
            }

            self = self.parentElement;
        }
    }

    /**
     * Toggles `focus` class to allow submenu access on tablets.
     */
    ( function( gridflex_primary_container ) {
        var touchStartFn, gridflex_primary_i,
            parentLink = gridflex_primary_container.querySelectorAll( '.menu-item-has-children > a, .page_item_has_children > a' );

        if ( 'ontouchstart' in window ) {
            touchStartFn = function( e ) {
                var menuItem = this.parentNode, gridflex_primary_i;

                if ( ! menuItem.classList.contains( 'gridflex-focus' ) ) {
                    e.preventDefault();
                    for ( gridflex_primary_i = 0; gridflex_primary_i < menuItem.parentNode.children.length; ++gridflex_primary_i ) {
                        if ( menuItem === menuItem.parentNode.children[gridflex_primary_i] ) {
                            continue;
                        }
                        menuItem.parentNode.children[gridflex_primary_i].classList.remove( 'gridflex-focus' );
                    }
                    menuItem.classList.add( 'gridflex-focus' );
                } else {
                    menuItem.classList.remove( 'gridflex-focus' );
                }
            };

            for ( gridflex_primary_i = 0; gridflex_primary_i < parentLink.length; ++gridflex_primary_i ) {
                parentLink[gridflex_primary_i].addEventListener( 'touchstart', touchStartFn, false );
            }
        }
    }( gridflex_primary_container ) );
} )();