jQuery(function($){

	// jQuery Match Height init
	$('.recent-blog-content-wrapper .hentry-inner').matchHeight();

// Enable menuToggle.

 ( function() {

        // Assume the initial scroll position is 0.
        var scroll = 0;

        // Wait for a click on one of our menu toggles.
        $('.menu-toggle').on( 'click', function(e) {

        	e.preventDefault();

            // Assign this (the button that was clicked) to a variable.
            var button = this;

            // Gets the actual menu (parent of the button that was clicked).
            var menu = $( this ).parents( '.menu-wrapper' );

            // Remove selected classes from other menus.
            $( '.menu-toggle' ).not( button ).removeClass( 'selected' );
            $( '.menu-wrapper' ).not( menu ).removeClass( 'is-open' );

            // Toggle the selected classes for this menu.
            $( button ).toggleClass( 'selected' );
            $( menu ).toggleClass( 'is-open' );

            // Is the menu in an open state?
            var is_open = $( menu ).hasClass( 'is-open' );

            // If the menu is open and there wasn't a menu already open when clicking.
            if ( is_open && ! jQuery( 'body' ).hasClass( 'menu-open' ) ) {

                // Get the scroll position if we don't have one.
                if ( 0 === scroll ) {
                    scroll = $( 'body' ).scrollTop();
                }

                // Add a custom body class.
                $( 'body' ).addClass( 'menu-open' );

            // If we're closing the menu.
            } else if ( ! is_open ) {

                $( 'body' ).removeClass( 'menu-open' );
                $( 'body' ).scrollTop( scroll );
                scroll = 0;
            }

        } );

        // Close menus when somewhere else in the document is clicked.
        $( document ).on( 'click', function() {

            $( 'body' ).removeClass( 'menu-open' );
            $( '.menu-toggle' ).removeClass( 'selected' );
            $( '.menu-wrapper' ).removeClass( 'is-open' );
        } );

        // Stop propagation if clicking inside of our main menu.
        $( '.menu-toggle, .dropdown-toggle, .search-field, #site-navigation, #search-container' ).on( 'click', function( e ) {

            e.stopPropagation();
        } );

      } )();

	/*.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();
		$(this).attr( 'aria-expanded', $(this).attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
	});


	/*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); });

	});

	/*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' );
	});


	/*Our Menu Script for mobile */
	$('.menu-content-wrapper .ui-nav-collapse a').on('click', function(e){

		 e.preventDefault();

		 if( !$(this).parent().hasClass('ui-state-active') ){

		  $('.ui-tabs-panel').removeClass('active-tab').fadeOut(0);
		  $('.ui-nav-collapse').removeClass('ui-state-active');
		  $(this).parent().addClass('ui-state-active');

		  var  getID = $(this).attr('href');

		  $(getID).addClass('active-tab').fadeOut(0).fadeIn(500);

		  var anchoID = $(this).offset().top;

		  $("html, body").animate({ scrollTop: anchoID - 15}, "slow");
		}

	});


	/*Our Menu Script for pc */
	$('.menu-content-wrapper .tabs-nav ul.ui-tabs-nav li a').on('click', function(e){
		e.preventDefault();

		if( !$(this).parent().hasClass('ui-state-active') ){
			$('.ui-tabs-tab').removeClass('ui-state-active');
			$('.ui-tabs-panel').removeClass('active-tab').fadeOut(0);

			$(this).parent().addClass('ui-state-active');


			var anchorAttr = $(this).attr('href');

			$(anchorAttr).addClass('active-tab').fadeOut(0).fadeIn(500);
		}

	});
});
