/**
 * File navigation.js.
 *
 * Handles toggling the navigation menu for small screens and enables TAB key
 * navigation support for dropdown menus.
 */
( function() {
    var gridread_secondary_container, gridread_secondary_button, gridread_secondary_menu, gridread_secondary_links, gridread_secondary_i, gridread_secondary_len;

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

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

    gridread_secondary_menu = gridread_secondary_container.getElementsByTagName( 'ul' )[0];

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

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

    gridread_secondary_button.onclick = function() {
        if ( -1 !== gridread_secondary_container.className.indexOf( 'gridread-toggled' ) ) {
            gridread_secondary_container.className = gridread_secondary_container.className.replace( ' gridread-toggled', '' );
            gridread_secondary_button.setAttribute( 'aria-expanded', 'false' );
            gridread_secondary_menu.setAttribute( 'aria-expanded', 'false' );
        } else {
            gridread_secondary_container.className += ' gridread-toggled';
            gridread_secondary_button.setAttribute( 'aria-expanded', 'true' );
            gridread_secondary_menu.setAttribute( 'aria-expanded', 'true' );
        }
    };

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

    // Each time a menu link is focused or blurred, toggle focus.
    for ( gridread_secondary_i = 0, gridread_secondary_len = gridread_secondary_links.length; gridread_secondary_i < gridread_secondary_len; gridread_secondary_i++ ) {
        gridread_secondary_links[gridread_secondary_i].addEventListener( 'focus', gridread_secondary_toggleFocus, true );
        gridread_secondary_links[gridread_secondary_i].addEventListener( 'blur', gridread_secondary_toggleFocus, true );
    }

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

            self = self.parentElement;
        }
    }

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

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

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

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


( function() {
    var gridread_primary_container, gridread_primary_button, gridread_primary_menu, gridread_primary_links, gridread_primary_i, gridread_primary_len;

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

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

    gridread_primary_menu = gridread_primary_container.getElementsByTagName( 'ul' )[0];

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

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

    gridread_primary_button.onclick = function() {
        if ( -1 !== gridread_primary_container.className.indexOf( 'gridread-toggled' ) ) {
            gridread_primary_container.className = gridread_primary_container.className.replace( ' gridread-toggled', '' );
            gridread_primary_button.setAttribute( 'aria-expanded', 'false' );
            gridread_primary_menu.setAttribute( 'aria-expanded', 'false' );
        } else {
            gridread_primary_container.className += ' gridread-toggled';
            gridread_primary_button.setAttribute( 'aria-expanded', 'true' );
            gridread_primary_menu.setAttribute( 'aria-expanded', 'true' );
        }
    };

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

    // Each time a menu link is focused or blurred, toggle focus.
    for ( gridread_primary_i = 0, gridread_primary_len = gridread_primary_links.length; gridread_primary_i < gridread_primary_len; gridread_primary_i++ ) {
        gridread_primary_links[gridread_primary_i].addEventListener( 'focus', gridread_primary_toggleFocus, true );
        gridread_primary_links[gridread_primary_i].addEventListener( 'blur', gridread_primary_toggleFocus, true );
    }

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

            self = self.parentElement;
        }
    }

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

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

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

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