/**
 * File navigation.js.
 *
 * Handles toggling the navigation menu for small screens and enables TAB key
 * navigation support for dropdown menus.
 */
( function() {
    var gridfeel_secondary_container, gridfeel_secondary_button, gridfeel_secondary_menu, gridfeel_secondary_links, gridfeel_secondary_i, gridfeel_secondary_len;

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

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

    gridfeel_secondary_menu = gridfeel_secondary_container.getElementsByTagName( 'ul' )[0];

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

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

    gridfeel_secondary_button.onclick = function() {
        if ( -1 !== gridfeel_secondary_container.className.indexOf( 'gridfeel-toggled' ) ) {
            gridfeel_secondary_container.className = gridfeel_secondary_container.className.replace( ' gridfeel-toggled', '' );
            gridfeel_secondary_button.setAttribute( 'aria-expanded', 'false' );
            gridfeel_secondary_menu.setAttribute( 'aria-expanded', 'false' );
        } else {
            gridfeel_secondary_container.className += ' gridfeel-toggled';
            gridfeel_secondary_button.setAttribute( 'aria-expanded', 'true' );
            gridfeel_secondary_menu.setAttribute( 'aria-expanded', 'true' );
        }
    };

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

    // Each time a menu link is focused or blurred, toggle focus.
    for ( gridfeel_secondary_i = 0, gridfeel_secondary_len = gridfeel_secondary_links.length; gridfeel_secondary_i < gridfeel_secondary_len; gridfeel_secondary_i++ ) {
        gridfeel_secondary_links[gridfeel_secondary_i].addEventListener( 'focus', gridfeel_secondary_toggleFocus, true );
        gridfeel_secondary_links[gridfeel_secondary_i].addEventListener( 'blur', gridfeel_secondary_toggleFocus, true );
    }

    /**
     * Sets or removes .focus class on an element.
     */
    function gridfeel_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( 'gridfeel-focus' ) ) {
                    self.className = self.className.replace( ' gridfeel-focus', '' );
                } else {
                    self.className += ' gridfeel-focus';
                }
            }

            self = self.parentElement;
        }
    }

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

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

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

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


( function() {
    var gridfeel_primary_container, gridfeel_primary_button, gridfeel_primary_menu, gridfeel_primary_links, gridfeel_primary_i, gridfeel_primary_len;

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

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

    gridfeel_primary_menu = gridfeel_primary_container.getElementsByTagName( 'ul' )[0];

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

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

    gridfeel_primary_button.onclick = function() {
        if ( -1 !== gridfeel_primary_container.className.indexOf( 'gridfeel-toggled' ) ) {
            gridfeel_primary_container.className = gridfeel_primary_container.className.replace( ' gridfeel-toggled', '' );
            gridfeel_primary_button.setAttribute( 'aria-expanded', 'false' );
            gridfeel_primary_menu.setAttribute( 'aria-expanded', 'false' );
        } else {
            gridfeel_primary_container.className += ' gridfeel-toggled';
            gridfeel_primary_button.setAttribute( 'aria-expanded', 'true' );
            gridfeel_primary_menu.setAttribute( 'aria-expanded', 'true' );
        }
    };

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

    // Each time a menu link is focused or blurred, toggle focus.
    for ( gridfeel_primary_i = 0, gridfeel_primary_len = gridfeel_primary_links.length; gridfeel_primary_i < gridfeel_primary_len; gridfeel_primary_i++ ) {
        gridfeel_primary_links[gridfeel_primary_i].addEventListener( 'focus', gridfeel_primary_toggleFocus, true );
        gridfeel_primary_links[gridfeel_primary_i].addEventListener( 'blur', gridfeel_primary_toggleFocus, true );
    }

    /**
     * Sets or removes .focus class on an element.
     */
    function gridfeel_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( 'gridfeel-focus' ) ) {
                    self.className = self.className.replace( ' gridfeel-focus', '' );
                } else {
                    self.className += ' gridfeel-focus';
                }
            }

            self = self.parentElement;
        }
    }

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

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

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

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