/* global dineroScreenReaderText */
/**
 * Theme functions file.
 *
 * Contains handlers for navigation.
 */

jQuery(function($){
 	// "use strict";
  //  	jQuery('.main-menu-navigation > ul').superfish({
		// delay:       500,
		// animation:   {opacity:'show',height:'show'},
		// speed:       'fast'
  //  	});

   	$( window ).scroll( function() {
		if ( $( this ).scrollTop() > 200 ) {
			$( '.back-to-top' ).addClass( 'show-back-to-top' );
		} else {
			$( '.back-to-top' ).removeClass( 'show-back-to-top' );
		}
	});

	// Click event to scroll to top.
	$( '.back-to-top' ).click( function() {
		$( 'html, body' ).animate( { scrollTop : 0 }, 500 );
		return false;
	});

	jQuery(function($){
		setTimeout(function(){
			$(".preloader").delay(1000).fadeOut("slow");
		});
	});
});

// function dinero_open() {
// 	jQuery(".sidenav").addClass('show');
// }
// function dinero_close() {
// 	jQuery(".sidenav").removeClass('show');
//     jQuery( '.mobile-menu' ).focus();
// }

function dinero_menuAccessibility() {
	var links, i, len,
	    dinero_menu = document.querySelector( '.nav-menu' ),
	    dinero_iconToggle = document.querySelector( '.nav-menu ul li:first-child a' );
    
	let dinero_focusableElements = 'button, a, input';
	let dinero_firstFocusableElement = dinero_iconToggle; // get first element to be focused inside menu
	let dinero_focusableContent = dinero_menu.querySelectorAll(dinero_focusableElements);
	let dinero_lastFocusableElement = dinero_focusableContent[dinero_focusableContent.length - 1]; // get last element to be focused inside menu

	if ( ! dinero_menu ) {
    	return false;
	}

	links = dinero_menu.getElementsByTagName( 'a' );

	// Each time a menu link is focused or blurred, toggle focus.
	for ( i = 0, len = links.length; i < len; i++ ) {
	    links[i].addEventListener( 'focus', toggleFocus, true );
	    links[i].addEventListener( 'blur', toggleFocus, true );
	}

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

      	// Move up through the ancestors of the current link until we hit .mobile-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( 'focus' ) ) {
	          		self.className = self.className.replace( ' focus', '' );
	          	} else {
	          		self.className += ' focus';
	          	}
	      	}
	      	self = self.parentElement;
      	}
	}
    
	// Trap focus inside modal to make it ADA compliant
	document.addEventListener('keydown', function (e) {
	    let isTabPressed = e.key === 'Tab' || e.keyCode === 9;

	    if ( ! isTabPressed ) {
	    	return;
	    }

	    if ( e.shiftKey ) { // if shift key pressed for shift + tab combination
	      	if (document.activeElement === dinero_firstFocusableElement) {
		        dinero_lastFocusableElement.focus(); // add focus for the last focusable element
		        e.preventDefault();
	      	}
	    } else { // if tab key is pressed
	    	if (document.activeElement === dinero_lastFocusableElement) { // if focused has reached to last focusable element then focus first focusable element after pressing tab
		      	dinero_firstFocusableElement.focus(); // add focus for the first focusable element
		      	e.preventDefault();
	    	}
	    }
	});   
}

jQuery(function($){
	$('.mobile-menu').click(function () {
	    dinero_menuAccessibility();
  	});

	new WOW().init();
});

(function( $ ) {
	$(window).scroll(function(){
	  	var sticky = $('.dinero-header'),
	    scroll = $(window).scrollTop();

	  	if (scroll >= 100) sticky.addClass('sticky-header');
	  	else sticky.removeClass('sticky-header');
	});
})( jQuery );



(function( $ ) {
	var masthead, menuToggle, siteNavContain, siteNavigation;

	function initMainNavigation( container ) {

		// Add dropdown toggle that displays child menu items.
		var dropdownToggle = $( '<button />', { 'class': 'dropdown-toggle', 'aria-expanded': false })
			.append( dineroScreenReaderText.icon )
			//.append( $( '<span />', { 'class': 'screen-reader-text', text: dineroScreenReaderText.expand }) );

		container.find( '.menu-item-has-children > a, .page_item_has_children > a' ).after( dropdownToggle );

		// Set the active submenu dropdown toggle button initial state.
		container.find( '.current-menu-ancestor > button' )
			.addClass( 'toggled-on' )
			.attr( 'aria-expanded', 'true' )
			.find( '.screen-reader-text' )
			.text( dineroScreenReaderText.collapse );
		// Set the active submenu initial state.
		container.find( '.current-menu-ancestor > .sub-menu' ).addClass( 'toggled-on' );

		container.find( '.dropdown-toggle' ).click( function( e ) {
			var _this = $( this ),
				screenReaderSpan = _this.find( '.screen-reader-text' );

			e.preventDefault();
			_this.toggleClass( 'toggled-on' );
			_this.next( '.children, .sub-menu' ).toggleClass( 'toggled-on' );

			_this.attr( 'aria-expanded', _this.attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );

			screenReaderSpan.text( screenReaderSpan.text() === dineroScreenReaderText.expand ? dineroScreenReaderText.collapse : dineroScreenReaderText.expand );
		});
	}

	initMainNavigation( $( '.sidenav' ) );

	masthead       = $( '#header' );
	menuToggle     = masthead.find( '.mobile-menu' );
	menuClose     = masthead.find( '.closebtn' );
	siteNavContain = masthead.find( '.sidenav' );
	siteNavigation = masthead.find( '.sidenav > div > ul' );

	// Enable menuToggle.
	(function() {

		// Return early if menuToggle is missing.
		if ( ! menuToggle.length ) {
			return;
		}

		// Add an initial value for the attribute.
		menuToggle.attr( 'aria-expanded', 'false' );

		menuToggle.on( 'click.dinero', function() {
			siteNavContain.toggleClass( 'toggled-on' );

			$( this ).attr( 'aria-expanded', siteNavContain.hasClass( 'toggled-on' ) );
		});

		menuClose.on( 'click.dinero', function() {
			siteNavContain.removeClass( 'toggled-on' );

			$( this ).attr( 'aria-expanded', siteNavContain.hasClass( 'toggled-on' ) );
		});
	})();

	// Fix sub-menus for touch devices and better focus for hidden submenu items for accessibility.
	(function($) {
		if ( ! siteNavigation.length || ! siteNavigation.children().length ) {
			return;
		}
	})();
})( jQuery );