jQuery(function($){
	// jQuery Match Height init
	$('.featured-content-section .entry-container, .recent-blog-content-wrapper .entry-container').matchHeight();

	/** Main Menu **/
	$('body').on('click touch','#search-toggle a', function(event){
		event.preventDefault();
		$('#search-toggle').toggleClass('search-open');
		$('.search-content-wrapper-top #search-container').stop().fadeToggle();
	});


   $('body').on('click touch','.menu-toggle', function(){
		$('.search-content-wrapper-top #search-container').stop().fadeOut();
		$('.overlay').css({'opacity': '1', 'display': 'block'});
		$('.menu-inside-wrapper').stop().animate({right: '0'});
		$('.menu-toggle').removeClass('menu-toggle').addClass('menu-close');
		$('.menu-label').text(personalTrainerText.menuCloseLabel);
		$('body').addClass('menu-is-on');
		$('#search-toggle').removeClass('search-open');
	});


	$('body').on('click touch','.menu-close', function(){
		$('.menu-inside-wrapper').stop().animate({right: '-1024'});
		$('.menu-close').addClass('menu-toggle').removeClass('menu-close');
		$('.overlay').css({'opacity': '0', 'display': 'none'});
		$('.menu-label').text(personalTrainerText.menuLabel);
		$('body').removeClass('menu-is-on');
	});

	//hide it when clicking anywhere else except the popup
	$(document).on('click touch', function(event) {
	  if (!$(event.target).parents().addBack().is('.menu-toggle, .menu-close')) {
	    $('.menu-inside-wrapper').stop().animate({right: '-1024'});
	    $( '.menu-close' ).addClass( 'menu-toggle' ).removeClass( 'menu-close' );
		$('.overlay').css({'opacity': '0', 'display': 'none'});
		$('.menu-label').text('MENU');
		$('body').removeClass('menu-is-on');
	  }
	});

	// Stop propagation to prevent hiding "menu-inside-wrapper" when clicking on it
	$('.menu-inside-wrapper').on('click touch', function(event) {
	 	event.stopPropagation();
	});

	/*Fixed navigation*/
	var stickyNav = function(){
		var scrollTop = $(window).scrollTop();

		if (scrollTop > 1) {
			$('body').addClass('fixed-nav');
		} else {
			$('body').removeClass('fixed-nav');
		}
	};

	/*Onload*/
	stickyNav();

	/*On Scroll*/
	$(window).on( 'scroll',function() {
		stickyNav();
	});


	/*.menu-item-has-children*/

	$('.menu-item-has-children > a, .main-navigation .page_item_has_children > a').after( '<button class="dropdown-toggle" aria-expanded="false"><span class="screen-reader-text">expand child menu</span></button>');

	$('.main-navigation button.dropdown-toggle').on( 'click',function() {
		$(this).stop(true, true).toggleClass('active');
		$(this).parent().find('.children, .sub-menu').first().stop(true, true).slideToggle();
	});


	/*Scroll Top*/
	var offset = 250;
	var duration = 300;

	$(window).on( 'scroll',function() {
		if ($(this).scrollTop() >= offset) {
			$('#scrollup').fadeIn(duration);
		} else {
			$('#scrollup').fadeOut(duration);
		}
	});

	$('body').on('click','.scrollup', function(e){
		e.preventDefault();
		$('html, body').animate({scrollTop: 0}, duration);
		return false;
	})

	/*
	 * Masonry
	 */

	//Masonry blocks
	$blocks = $('#masonry-wrapper');

	$blocks.imagesLoaded(function(){
		$blocks.masonry({
			itemSelector: '.grid-item',
			columnWidth: '.grid-item',
			// slow transitions
			transitionDuration: '1s'
		});

		// Fade blocks in after images are ready (prevents jumping and re-rendering)
		$('.grid-item').fadeIn();
		$blocks.find( '.grid-item' ).animate( {
			'opacity' : 1
		} );

	});

	$(document).ready( function() { setTimeout( function() { $blocks.masonry(); }, 2000); });

	$(window).on( 'resize', function () {
		$blocks.masonry();
	});

	// When Jetpack Infinite scroll posts have loaded
	$( document.body ).on( 'post-load', function () {
		var $container = $('#masonry-wrapper');
		$container.masonry( 'reloadItems' );

		$blocks.imagesLoaded(function(){
			$blocks.masonry({
				itemSelector: '.grid-item',
				columnWidth: '.grid-item',
				// slow transitions
				transitionDuration: '1s'
			});

			// Fade blocks in after images are ready (prevents jumping and re-rendering)
			$('.grid-item').fadeIn();
			$blocks.find( '.grid-item' ).animate( {
				'opacity' : 1
			} );

		});
		$(document).ready( function() { setTimeout( function() { $blocks.masonry(); }, 2000); });

	});

	/*Click and scrolldown from silder image*/
	$('body').on('click touch','.scroll-down', function(e){
		var Sclass = $(this).parent().next().attr('class');
		var Sclass_array = Sclass.split(" ");
		var scrollto = $('.' + Sclass_array[0] ).offset().top - 55;
		$('html, body').animate({
			scrollTop: scrollto
		}, 1000);
	});


	/*If body has class header-media-disabled */
	if ( $('body').hasClass('header-media-disabled') ) {
		var  mn = $('#masthead');
	    mns = "main-nav-scrolled";
	    hdr = $('#masthead').offset().top;

		$(window).on( 'scroll',function() {
		  if( $(this).scrollTop() >= hdr ) {
		    mn.addClass(mns);
		  } else {
		    mn.removeClass(mns);
		  }
		});
	}

	// Add header video class after the video is loaded.
	$( document ).on( 'wp-custom-header-video-loaded', function() {
		$('body').addClass( 'has-header-video' );
	});
});
