(function ($) {

	function EVAWPloadMorePosts() {
		var page = 2;
		jQuery(document).on('click', 'a.evawp-load-more', function (e) {
			e.preventDefault();
			var button = jQuery(this);
			jQuery.ajax({
				type: "POST",
				url: EVAWPLOADMORE.admin_url,
				data: {
					nonce: EVAWPLOADMORE.nonce,
					action: 'view_more_posts',
					query: EVAWPLOADMORE.posts,
					page: page
				},
				beforeSend: function () {
					button.html('<i class="fas fa-spinner fa-spin"></i>&nbsp; Loading');
				},
				success: function (data) {
					console.log( 'sfasdfsdf' );
					if (data) {
						jQuery("#evawp-main-content").append(data);
						if (page == EVAWPLOADMORE.max_page) {
							button.remove();
						}
						button.html(EVAWPLOADMORE.pagination_text);
						page++;
					} else {
						button.remove();
					}
				}
			})
		});
	}


	// mobile menu plugin
	const classToggler = function (param) {

		this.animation = param.animation,
			this.toggler = param.toggler,
			this.className = param.className,
			this.exceptions = param.exceptions;

		this.init = function () {
			var that = this;
			// for stop propagation
			var stopToggler = this.implode(this.exceptions);
			if (typeof stopToggler !== 'undefined') {
				$(document).on('click', stopToggler, function (e) {
					e.stopPropagation();
				});
			}

			// for toggle class
			var toggler = this.implode(this.toggler);
			if (typeof toggler !== 'undefined') {

				$(document).on('click', toggler, function (e) {
					e.stopPropagation();
					e.preventDefault();
					that.toggle();
				});
			}
		}

		//class toggler
		this.toggle = function () {
			var selectors = this.implode(this.animation);
			if (typeof selectors !== 'undefined') {
				$(selectors).toggleClass(this.className);
				if ($(selectors).hasClass(this.className)) {
					$('.evawp-menu-list > li:first-child a').focus();
				} else {
					$('#menu-icon').focus();
				}
			}
		}

		// array selector maker
		this.implode = function (arr, imploder) {

			// checking arg is array or not
			if (!(arr instanceof Array)) {
				return arr;
			}
			// setting default imploder
			if (typeof imploder == 'undefined') {
				imploder = ',';
			}

			// making selector
			var data = arr;
			var ele = '';
			for (var j = 0; j < arr.length; j++) {
				ele += arr[j];
				if (j !== arr.length - 1) {
					ele += imploder;
				}
			}
			data = ele;
			return data;
		}
	} //End mobileMenu

	$.fn.evawpMobileMenu = function (config) {

		/* defining default config*/
		var defaultConfig = {
			icon: '#menu-icon',
			closeIcon: true,
			overlay: true
		}
		$.extend(defaultConfig, config);
		var wrapperId = '#' + this.attr('id');
		if (!$(wrapperId).length) {
			console.error('Selected Element not found in DOM (Mobile menu plugin)');
			return this;
		}

		var _this = this;
		var shiftMenu = function () {

			var mobileMenuHTML = '<div>' + $(wrapperId).html() + '</div>',
				that = this;

			mobileMenuHTML = $(mobileMenuHTML).find('*').each(function (index, value) {
				var id = $(value).attr('id');
				if (id) {
					$(value).attr('id', 'evawp-' + id);
				}
			});

			/* constructor function */
			this.init = function () {
				$(document).ready(function () {
					that.createMenu();
					that.addDownArrow();
					that.toggleSubUl();
					that.menuToggler();
					that.addClassOnFirstUl();

				});
			};

			this.createMenu = function () {
				var closeHTML = defaultConfig.closeIcon ? this.closeMenuIcon() : null,
					overlayHTML = defaultConfig.overlay ? this.addOverlay() : null;
				$('body').append('<div class="evawp-mobile-menu" id="evawp-mobile-menu">' + closeHTML + '<ul class="evawp-menu-list">' + mobileMenuHTML.html() + '</ul><button class="circular-focus screen-reader-text" data-goto=".evawp-inner-box">Circular focus</button></div>' + overlayHTML)
			};

			this.closeMenuIcon = function () {
				return ('<div class="evawp-close-wrapper"><button data-goto=".evawp-menu-list > li:last-child a" class="circular-focus screen-reader-text">circular focus</button> <button tabindex="0" class="evawp-inner-box" id="evawp-close"><span class="evawp-inner"></span></button> </div>');
			};

			this.addOverlay = function () {
				return ('<div class="evawp-mobile-menu-overlay"></div>');
			};

			this.addClassOnFirstUl = function () {
				if ($('#evawp-mobile-menu ul').first().hasClass('menu')) { } else {
					$('#evawp-mobile-menu ul').first().addClass('menu');
				}
			}

			this.addDownArrow = function () {
				var $mobileMenu = $('#evawp-mobile-menu'),
					$hasSubUl = $('#evawp-mobile-menu .menu-item-has-children'),
					haveClassOnLi = $mobileMenu.find('.menu-item-has-children');

				if (haveClassOnLi.length > 0) {
					$hasSubUl.children('a').append('<a href="#" class="evawp-arrow-box"><span class="evawp-down-arrow"></span></a>');
				} else {
					$('#evawp-mobile-menu ul li:has(ul)').children('a').append('<a href="#" class="evawp-arrow-box"><span class="evawp-down-arrow"></span></a>');
				}
			};

			this.toggleSubUl = function () {
				$(document).on('click', '.evawp-arrow-box', toggleSubMenu);

				function toggleSubMenu(e) {
					e.stopPropagation();
					e.preventDefault();
					$(this).toggleClass('open').parent().next().slideToggle();
				}
			};

			this.menuToggler = function () {
				var menuConfig = {
					animation: ['.evawp-mobile-menu-overlay', '#evawp-mobile-menu', 'body', '#menu-icon'],
					exceptions: ['#evawp-mobile-menu'],
					toggler: ['#menu-icon', '.evawp-mobile-menu-overlay', '#evawp-close'],
					className: 'evawp-menu-open'
				};
				new classToggler(menuConfig).init();
			};

		}; /* End shiftMenu */

		/* instance of shiftmenu */
		new shiftMenu().init();

	};

	//banner slider
	$(document).ready(function () {
		$('.evawp-slider-init').slick({
			infinite: true,
			slidesToShow: 3,
			slidesToScroll: 1,
			autoplay: true,
			autoplaySpeed: 3000,
			arrows: true,
			dots: true,
			responsive: [{
				breakpoint: 1024,
				settings: {
					slidesToShow: 2,
					slidesToScroll: 3,
					infinite: true,
					dots: true
				}
			},
			{
				breakpoint: 600,
				settings: {
					slidesToShow: 1,
					slidesToScroll: 2
				}
			}

			]

		});

		$(window).on("scroll", function () {
			if ($(window).scrollTop() > 100) {
				$("body").addClass('evawp-scrolled-down');
			} else {
				$("body").removeClass('evawp-scrolled-down');
			}
		});
		//scroll to top
		$('.evawp-scroll-top').click(function (e) {
			e.preventDefault();
			$("body,html").animate({
				scrollTop: 0,
			}, 300);
			return false;
		});
		//sticky js
		$(".evawp-top-wrapper").sticky({
			topSpacing: 0
		});
		//mobile menu init
		$('#site-navigation').evawpMobileMenu();

		// load more ajax
		EVAWPloadMorePosts();

		jQuery(document).on("focus", ".circular-focus", function () {
			jQuery(jQuery(this).data("goto")).focus();
		});


		jQuery(document).on("click", '.evawp-mobile-menu ul li:not(".menu-item-has-children") a', function () {
			jQuery("button#evawp-close").trigger("click");
		});
	});

	//preloader
	$(window).load(function () {
		$(".evawp-loader-wrapper").fadeOut();
		$(".loader").delay(1000).fadeOut("slow");
	});
})(jQuery)