/**
 * File navigation.js.
 *
 * Handles toggling the navigation menu for small screens and enables TAB key
 * navigation support for dropdown menus.
 */
( function() {
    var freshwp_primary_container, freshwp_primary_button, freshwp_primary_menu, freshwp_primary_links, freshwp_primary_i, freshwp_primary_len;

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

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

    freshwp_primary_menu = freshwp_primary_container.getElementsByTagName( 'ul' )[0];

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

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

    freshwp_primary_button.onclick = function() {
        if ( -1 !== freshwp_primary_container.className.indexOf( 'freshwp-toggled' ) ) {
            freshwp_primary_container.className = freshwp_primary_container.className.replace( ' freshwp-toggled', '' );
            freshwp_primary_button.setAttribute( 'aria-expanded', 'false' );
            freshwp_primary_menu.setAttribute( 'aria-expanded', 'false' );
        } else {
            freshwp_primary_container.className += ' freshwp-toggled';
            freshwp_primary_button.setAttribute( 'aria-expanded', 'true' );
            freshwp_primary_menu.setAttribute( 'aria-expanded', 'true' );
        }
    };

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

    // Each time a menu link is focused or blurred, toggle focus.
    for ( freshwp_primary_i = 0, freshwp_primary_len = freshwp_primary_links.length; freshwp_primary_i < freshwp_primary_len; freshwp_primary_i++ ) {
        freshwp_primary_links[freshwp_primary_i].addEventListener( 'focus', freshwp_primary_toggleFocus, true );
        freshwp_primary_links[freshwp_primary_i].addEventListener( 'blur', freshwp_primary_toggleFocus, true );
    }

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

            self = self.parentElement;
        }
    }

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

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

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

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