$ = jQuery
jQuery(document).ready(function () {
  // main menu
//   $(".header-menu-icon").click(function () {
//     $("body").addClass("menu-open");
//   });
//   $(".header-menu-close-icon, .page-overlay").click(function () {
//     $("body").removeClass("menu-open");
//   });
//   $(".menu-top-menu-container .menu-item-has-children > a").append('<span></span>');
//   $('.menu-top-menu-container .menu-item-has-children > a').find('span').addClass('sub-menu-open');

//   $(document).on('click', '.menu-top-menu-container .menu-item-has-children > a > span', function (e) {
//     e.preventDefault();
//     var $this = $(this);
//     $this.parent('a').next('.menu-top-menu-container ul.sub-menu').toggle('slow');
//     $this.parent('a').toggleClass('.menu-top-menu-container sub-menu-close');
//   });

	  $(".header-menu-icon").click(function () {
    $("body").addClass("menu-open");
	  HederMenu();
  });
  $(".header-menu-close-icon, .page-overlay").click(function () {
    $("body").removeClass("menu-open");
  });

// appned class on menu-item-has-children

  $(".menu-top-menu-container .menu-item-has-children > a").append('<a href="javascript:void(0)"></a>');
  $('.menu-top-menu-container .menu-item-has-children > a').find('a').addClass('sub-menu-open');
	
  $(document).on('click', '.menu-top-menu-container .menu-item-has-children > a > a', function (e) {
    e.preventDefault();
    var $this = $(this);
    $this.parent('a').next('.menu-top-menu-container ul.sub-menu').toggle('slow');
    $this.parent('a').toggleClass('.menu-top-menu-container sub-menu-close');
  });
	 function HederMenu(e){
		 		var focusableEls = $('.menu-top-menu-container li a[href]:not([disabled]), .menu-top-menu-container a, .menu-item-has-children a .sub-menu-open');
        var firstFocusableEl = focusableEls[0];
        var lastFocusableEl = focusableEls[focusableEls.length - 1];
        var KEYCODE_TAB = 9;
		 
		  $('.menu-top-menu-container').on('keydown', function (e) {
            if (e.key === 'Tab' || e.keyCode === KEYCODE_TAB) {

                if ( e.shiftKey && document.activeElement === firstFocusableEl ) {
                  e.preventDefault();
                  lastFocusableEl.focus();
					
                } else {
                  if (document.activeElement === lastFocusableEl) {
                      e.preventDefault();
                    firstFocusableEl.focus();
                  }
                }    
            }
        });
	 }

   // appned class on page_item_has_children

  $(".menu-top-menu-container .page_item_has_children > a").append('<a href="javascript:void(0)"></a>');
  $('.menu-top-menu-container .page_item_has_children > a').find('a').addClass('sub-menu-open');
  
  $(document).on('click', '.menu-top-menu-container .page_item_has_children > a > a', function (e) {
    e.preventDefault();
    var $this = $(this);
    $this.parent('a').next('.menu-top-menu-container ul.children').toggle('slow');
    $this.parent('a').toggleClass('.menu-top-menu-container sub-menu-close');
  });
   function HederMenu(e){
        var focusableEls = $('.menu-top-menu-container li a[href]:not([disabled]), .menu-top-menu-container a, .page_item_has_children a .sub-menu-open');
        var firstFocusableEl = focusableEls[0];
        var lastFocusableEl = focusableEls[focusableEls.length - 1];
        var KEYCODE_TAB = 9;
     
      $('.menu-top-menu-container').on('keydown', function (e) {
            if (e.key === 'Tab' || e.keyCode === KEYCODE_TAB) {

                if ( e.shiftKey && document.activeElement === firstFocusableEl ) {
                  e.preventDefault();
                  lastFocusableEl.focus();
          
                } else {
                  if (document.activeElement === lastFocusableEl) {
                      e.preventDefault();
                    firstFocusableEl.focus();
                  }
                }    
            }
        });
   }

	$(document).on('keyup',function(e){
		if ( e.keyCode === 27 && $('body').hasClass('menu-open') ) {
			$("body").removeClass('menu-open');
		}
	});


  // search toggle
  var removeClass = true;
  $(".search-toggle").click(function () {
	   FunSearch();
  });
	
	function FunSearch(e){
		$(".search-section").toggleClass('search-open');
		setTimeout(function(){
			$('.search-section form input[type="search"]').focus();
		},500);

		removeClass = false;
		var focusableEls = $('.search-section a[href]:not([disabled]), .search-section input[type="submit"]:not([disabled]), .search-section input:not([disabled]) ');
		var firstFocusableEl = focusableEls[0];
		var lastFocusableEl = focusableEls[focusableEls.length - 1];
		var KEYCODE_TAB = 9;
		$('.search-section').on('keydown', function (e) {
			if (e.key === 'Tab' || e.keyCode === KEYCODE_TAB) {

				if ( e.shiftKey && document.activeElement === lastFocusableEl ) {
					e.preventDefault();
					lastFocusableEl.focus();
				} else {
					if (document.activeElement === lastFocusableEl) {
						e.preventDefault();
						firstFocusableEl.focus();
					}
				}    
			}
		});
	}
  // when clicking the div : never remove the class
  $(".site-header .search-form label input").click(function () {
    removeClass = false;
  });
  // when click event reaches "html" : remove class if needed, and reset flag
  $("html, .close-icon").click(function () {
    if (removeClass) {
      $(".search-section").removeClass('search-open');
    }
    removeClass = true;
  });

	$(document).on('keyup',function(e){
		if ( e.keyCode === 27 && $('.search-section').hasClass('search-open') ) {
			$(".search-section").removeClass('search-open');
		}
	});



  /* back-to-top button*/

  $('.back-to-top').hide();
  $('.back-to-top').on("click", function (e) {
    e.preventDefault();
    $('html, body').animate({
      scrollTop: 0
    }, 'slow');
  });


  $(window).scroll(function () {
    var scrollheight = 400;
    if ($(window).scrollTop() > scrollheight) {
      $('.back-to-top').fadeIn();

    } else {
      $('.back-to-top').fadeOut();
    }
  });



  // slider

  var owllogo = $(".main-slider");

  owllogo.owlCarousel({
    items: 1,
    loop: true,
    nav: false,
    dots: true,
    smartSpeed: 900,
    autoplay: true,
    autoplayTimeout: 5000,
    fallbackEasing: 'easing',
    transitionStyle: "fade",
    autoplayHoverPause: true,
    animateOut: 'fadeOut'
  });


  var owllogo = $(".service-post-slider");
  owllogo.owlCarousel({
    items: 1,
    loop: true,
    nav: false,
    dots: true,
    smartSpeed: 900,
    autoplay: true,
    autoplayTimeout: 5000,
    fallbackEasing: 'easing',
    transitionStyle: "fade",
    autoplayHoverPause: true,
    animateOut: 'fadeOut'
  });

   $('#slct').on('change', function(evt){
      var current_val = $(this).val();
      console.log(current_val);
      var sorting_query = $('#sorting-query').val();
      sorting_query = JSON.parse(sorting_query);
      evt.preventDefault();
      jQuery.post(
          rt_agency_pro_script_vars.ajaxurl, 
          {
              'action': 'load_search_results',
              'orderby' : current_val,
              'sorting-query' : sorting_query,
          }, 
          function(response){
               $('.tab-content').html(response);
          }
      );
  })
  // counter js

  function count($this) {
    var current = parseInt($this.html(), 10);
    current = current + 1; /* Where 1 is increment */

    $this.html(++current);
    if (current > $this.data('count')) {
      $this.html($this.data('count'));
    } else {
      setTimeout(function () {
        count($this)
      }, 250);
    }
  }

  jQuery(".start-count").each(function () {
    jQuery(this).data('count', parseInt(jQuery(this).html(), 10));
    jQuery(this).html('0');
    count(jQuery(this));
  });

  jQuery('.testimonial-slider').owlCarousel({
    center: true,
    loop: false,
    margin: 30,
    dots: true,
    nav: false,
    autoplay: true,
    responsive: {
      0: {
        items: 1
      },
      992: {
        items: 2
      }
    }
  });

});

