/**
 * Desktop navbar menu
 *
 */
jQuery( function( $ ) {
	'use strict';

	$( '.fancify-menu,.fancify-burger-menu' ).superfish({
		pathClass: 'fancify-with-ul',
		delay: 500, // one second delay on mouseout
		animation: {
			opacity: 'show'
		}, // fade-in and slide-down animation
		speed: 'slow', // faster animation speed
		disableHI: false,
		autoArrows: true,
		onInit: putArrow() // callback function fires once Superfish is initialised – 'this' is the containing ul
		// disable generation of arrow mark-up
	});

	/**
	 * fancify-burger menu for mobile and tablet
	 */

	jQuery( '.fancify-burger, .fancify-header-main-burgernav-overlay' ).on(
		'click',
		function() {
			var transition = 0.15;
			if (
				jQuery( '.fancify-header-main-burgernav' ).hasClass(
					'fancify-header-main-burgernav-show'
				)
			) {
				jQuery( '.fancify-header-main-burgernav-main li' ).each( function(
					index,
					element
				) {
					jQuery( this ).css( 'transition-delay', '' );
				});
			} else {
				jQuery( '.fancify-header-main-burgernav-main li' ).each( function(
					index,
					element
				) {
					jQuery( this ).css( 'transition-delay', transition + 's' );
					transition += 0.15;
				});
			}
			jQuery( '.fancify-burger' ).toggleClass(
				'fancify-header-main-burgernav-clicked'
			);
			jQuery( '.fancify-header-main-burgernav-overlay' ).toggleClass(
				'fancify-header-main-burgernav-show'
			);
			jQuery( '.fancify-header-main-burgernav' ).toggleClass(
				'fancify-header-main-burgernav-show'
			);
			jQuery( 'body' ).toggleClass( 'fancify-header-main-burgernav-overflow' );
		}
	);
	jQuery( '.fancify-burger, .fancify-header-main-burgernav-overlay' ).on(
		'keypress',
		function( e ) {
			if ( 13 === e.which ) {

				//Enter key pressed
				let transition = 0.15;
				if (
					jQuery( '.fancify-header-main-burgernav' ).hasClass(
						'fancify-header-main-burgernav-show'
					)
				) {
					jQuery( '.fancify-header-main-burgernav-main li' ).each( function(
						index,
						element
					) {
						jQuery( this ).css( 'transition-delay', '' );
					});
				} else {
					jQuery( '.fancify-header-main-burgernav-main li' ).each( function(
						index,
						element
					) {
						jQuery( this ).css( 'transition-delay', transition + 's' );
						transition += 0.15;
					});
				}
				jQuery( '.fancify-burger' ).toggleClass(
					'fancify-header-main-burgernav-clicked'
				);
				jQuery( '.fancify-header-main-burgernav-overlay' ).toggleClass(
					'fancify-header-main-burgernav-show'
				);
				jQuery( '.fancify-header-main-burgernav' ).toggleClass(
					'fancify-header-main-burgernav-show'
				);
				jQuery( 'body' ).toggleClass( 'fancify-header-main-burgernav-overflow' );
			}
		}
	);

	function putArrow() {

		// Add arrow icon to menu
		jQuery( '.menu-item-has-children' )
			.find( 'a:first' )
			.append( '<i class="fancify-font fancify-font-angle-down-1"></i>' );

		// Burger menu items Toggle Slide
		jQuery( '.menu-item-has-children' )
			.find( 'i' )
			.on( 'click', function( e ) {
				e.preventDefault();
			});
		jQuery( '.menu-item-has-children' )
			.find( 'a' )
			.on( 'focus click', function( e ) {

				//jQuery(this).parent().removeClass('hover show');
				//jQuery(this).parent().find('.sub-menu.hover').removeClass('hover show');
				jQuery( this ).parent( 'ul,li' ).toggleClass( 'sfHover' );
				jQuery( this ).next( 'ul.sub-menu' ).toggleClass( 'sfHover' );

				//jQuery(this).parent().find(".sub-menu:first").addClass('hover show');
			});
	}
	$( function() {
		let $li;

		function lastItemDepth( depth, $menu ) {
			let max = depth;
			$li = $menu.find( '> li:last-child' );

			//onsole.log($menu, $li.hasClass('menu-item-has-children'), $menu.hasClass('sub-menu'), depth);

			if ( $li.hasClass( 'menu-item-has-children' ) ) {
				let innerDepth = lastItemDepth( depth + 1, $li.find( '> .sub-menu' ) );

				max = innerDepth > max ? innerDepth : max;
			} else if ( $menu.hasClass( 'sub-menu' ) ) {
				let innerDepth = depth + 1;

				max = innerDepth > max ? innerDepth : max;
			}

			return max;
		}

		function lastItemDepths( $menu ) {
			var depths = [];

			for ( let i = 0; i < $menu.find( '> li' ).length; i++ ) {
				$li = $menu.find( '> li:nth-of-type(' + ( i + 1 ) + ')' );

				if ( $li.hasClass( 'menu-item-has-children' ) ) {
					depths[i] = lastItemDepth( i, $li.find( '.sub-menu' ) );
				} else {
					depths[i] = 1;
				}
			}

			//console.log(depths)
			return depths;
		}

		// Main menu Accessibity
		jQuery( '.fancify-menu' )
			.find( 'a' )
			.on( 'blur', function() {
				var $this = $( this );
				var depths = lastItemDepths(
					$( '.fancify-header-main-menu .fancify-menu' )
				);
				var depth = 0;
				var isLastItem = false;

				while ( ! $this.hasClass( 'fancify-menu' ) ) {
					if ( 'li' === $this.prop( 'tagName' ).toLowerCase() ) {
						isLastItem = isLastItem || -1 === $this.next().index();
						depth++;

						if ( -1 === $this.next().index() ) {
							$( $this ).parent().removeClass( 'sfHover' );
						}
						if (
							$this.parent().hasClass( 'fancify-menu' ) &&
							depth - 1 === depths[$this.index()] &&
							isLastItem
						) {

							//console.log($this.parent().hasClass('fancify-menu'), (depth - 1), depths[$this.index()], isLastItem)

							$( '.sub-menu' ).removeClass( 'sfHover' );
						}
					}

					$this = $this.parent();
				}
			});

		// Burger Nav menu Accessibity
		jQuery( '.fancify-burger-menu' )
			.find( 'a' )
			.on( 'blur', function() {
				var $this = $( this );
				var depths = lastItemDepths(
					$( '.fancify-header-main-burgernav .fancify-burger-menu' )
				);
				var depth = 0;
				var isLastItem = false;

				while ( ! $this.hasClass( 'fancify-burger-menu' ) ) {
					if ( 'li' === $this.prop( 'tagName' ).toLowerCase() ) {
						isLastItem = isLastItem || -1 === $this.next().index();
						depth++;

						if ( -1 === $this.next().index() ) {
							$( $this ).parent().removeClass( 'sfHover' );
						}
						if (
							$this.parent().hasClass( 'fancify-burger-menu' ) &&
							depth - 1 === depths[$this.index()] &&
							isLastItem
						) {
							$( 'ul.sub-menu, li' ).removeClass( 'sfHover' );
						}
					}
					$this = $this.parent();
				}
			});
	});

	// ADD class for focus on Keyboard navigation while using tab
	$( function() {
		var focusable = jQuery( '.fancify-header-main-burgernav' ).find(
			'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
		);
		var lastFocusable = focusable[focusable.length - 1];
		var wasLast = false;
		jQuery( 'body' ).on( 'keyup', function( e ) {

			//if (e.keyCode === 9) {
			if (
				! $( document.activeElement )
				.parent()
				.parent()
				.hasClass( 'fancify-burger-menu' ) &&
				wasLast
			) {
				jQuery( '.fancify-burger' ).toggleClass(
					'fancify-header-main-burgernav-clicked'
				);
				jQuery( '.fancify-header-main-burgernav-overlay' ).toggleClass(
					'fancify-header-main-burgernav-show'
				);
				jQuery( '.fancify-header-main-burgernav' ).toggleClass(
					'fancify-header-main-burgernav-show'
				);
				jQuery( 'body' ).toggleClass( 'fancify-header-main-burgernav-overflow' );
			}

			if ( document.activeElement === lastFocusable ) {
				wasLast = true;
			} else {
				wasLast = false;
			}
			if ( 9 === e.keyCode ) {
				$( '.fancify-outline' ).removeClass( 'fancify-outline' );
				let that = $( ':focus' );
				$( that ).addClass( 'fancify-outline' );
			}
		});
	});
	new Swiper( '.fancify-postformat-gallery', {
		calculateHeight: true,
		autoHeight: true,
		loop: true,
		slidesPerView: 1,
		navigation: {
			nextEl: '.swiper-button-next',
			prevEl: '.swiper-button-prev'
		}
	});

	new Swiper( '.fancify-post-suggestion-container', {
		slidesPerView: 3,
		spaceBetween: 26,
		loop: true,
		pagination: {
			el: '.fancify-post-suggestion-swiper-pagination',
			clickable: true
		},
		navigation: {
			nextEl: '.slider-control-left',
			prevEl: '.slider-control-right'
		},
		breakpoints: {

			// when window width is <= 640px
			1366: {
				slidesPerView: 3,
				spaceBetween: 26
			},
			768: {
				slidesPerView: 2,
				spaceBetween: 26
			},
			360: {
				slidesPerView: 1
			}
		}
	});

	/**
	 * Go to Top Function
	 * Using jquery.scrollTo Plugin
	 *
	 * @since 1.0
	 */
	$( '.fancify-goto-top' ).on( 'click', function() {
		$.scrollTo( 0, 800, {
			queue: true
		});
	});

	$( window ).scroll( function() {
		if ( 400 < $( this ).scrollTop() ) {
			$( '.fancify-goto-top' ).fadeIn();
		} else {
			$( '.fancify-goto-top' ).fadeOut();
		}
	});

	$( '.fancify-video-shortcode' ).removeAttr( 'controls' );
	$( '.fancify-postformat-video' ).on( 'click', function() {
		if ( $( '.fancify-video-shortcode' ).attr( 'controls' ) ) {
			$( '.fancify-video-shortcode' ).removeAttr( 'controls', 'controls' );
			$( '.fancify-video-shortcode' ).trigger( 'pause' );
		} else {
			$( '.fancify-video-shortcode' ).attr( 'controls', 'controls' );
			$( '.fancify-video-shortcode' ).trigger( 'play' );
		}

		//$('.fancify-video-shortcode').get(0).play();
		$( this ).toggleClass( 'fancify-video-play-button' );
		$( this ).find( '.fancify-article-category' ).toggle();
	});

	/**
	 * PreLoader Progress bar
	 *
	 * 1- set progressbar
	 * 2-set interval each 150ms with incrimenting 10-100 % by random nubmber between 1-6
	 *
	 */
	$.fancifyProgressbar = {};

	$.fancifyProgressbar.options = {
		classname: 'fancify-preloader-progressbar',
		id: 'fancify-preloader-progressbar',
		target: document.getElementById( 'fancify-preloader-progressbar' )
	};

	$.fancifyProgressbar.run = new Nanobar( $.fancifyProgressbar.options );
	$.fancifyProgressbar.precentage = 10;
	$.fancifyProgressbar.interval = setInterval( () => {
		$.fancifyProgressbar.run.go( $.fancifyProgressbar.precentage ); // size bar 76%
		if ( 90 < $.fancifyProgressbar.precentage ) {
			clearInterval( $.fancifyProgressbar.interval );
		} else {
			$.fancifyProgressbar.precentage += Math.floor( Math.random() * 6 ) + 1;
		}
	}, 150 );

	setTimeout( function() {
		$( '#fancify-preloader-progressbar' )
			.find( '.bar' )
			.css( 'background', fancifyObject.preloader_color );
	});
});
