jQuery(document).ready(function($) {
	$(window).load(function() {
		// $(".login-slide-wrapper").css("visibility","visible").hide();
		// if ($(".login-slide-wrapper").length > 0 && $(window).width() > 640) {
		// 	if ($(".alert, #wppb_general_top_error_message",".login-left").length > 0) {
		// 		$(".login-slide-wrapper").slideToggle(0);
		// 		if ($("#login-kt").html().match(/STER .{1}\<\/a\>$/)) {
		// 			var txt = $("#login-kt").html().replace(/.{1}\<\/a\>$/,"&#9650;</a>");
		// 		} else {
		// 			var txt = $("#login-kt").html().replace(/R\<\/a\>$/,"R &#9650;</a>");
		// 		}
		// 		$("body #login-kt").html(txt);
		// 	}
		// }
	});

	// $(".topbar-right .topbar-text").html("<div id=\"login-kt\"><a nohref><i class=\"fa fa-user vntd-icon vntd-icon-small\"></i>LOGIN / REGISTER &#9660;</a></div><div id=\"user-kt\"><i class=\"fa fa-user vntd-icon vntd-icon-small\"></i><span id=\"username-kt\"></span></div>");
	// $(".topbar-left .topbar-text").html("<div id=\"kt-search-locations\"><a href=\"https://play.kingtrivia.com\"><i class=\"fa fa-mobile-alt vntd-icon vntd-icon-small\"></i>GO TO THE APP</a></div>");
	// $("body #login-kt").show();

	$(".top-left-block, .top-right-block, .bottom-left-block, .bottom-right-block", "#two-block-row,#tablet-block-row").hover(function() {
       var siz = ( $(window).width() < 1200 ) ? 0 : 10;
       $( ".text-block", $(this) ).stop().animate({ top: siz+"px" }, 500);
       $( ".desc-block", $(this) ).stop().animate({ "opacity": "1" }, 300);
    },
    function() {
       var siz = ( $(window).width() < 1200 ) ? 200 : 225;
       $( ".text-block", $(this) ).stop().animate({ top: siz+"px" }, 300);
       $( ".desc-block", $(this) ).stop().animate({ "opacity": "0" }, 100);
    });


	if ($(window).width() < 641 && $("#login-kt").html().match(/STER .{1}\<\/a\>$/)) {
		var txt = $("#login-kt").html().replace(/.{2}\<\/a\>$/,"</a>");
		$("#login-kt").html(txt);
	}

	if ($("body").hasClass("logged-in")) {
		$("body #login-kt").hide();

		var keys = {};
		keys['action'] = "getUserMenu";
		$.ajax({
			url: ktUserAjax.ajaxurl,
			type: "POST",
			dataType: "html",
			data: keys,
			success: function(response) {
				var result = $.parseJSON(response);
				$("#username-kt").html(result['first_name']);
				var tmp = $("#kt-user-full-name").html();
				$("#kt-user-full-name").html(tmp+result['first_name']);
				$("#user-kt").show();
			}
		});
		$("a","#logout-kt").prop("href",$("#logoutLink").val());
	} else {
		$("body #user-kt").hide();
		$("body #login-kt").show();
	}

	$("body").on('click', '#user-kt', function(e) {
		$("#kt-overlay").show();
		$("#kt-user-menu").show().animate({ right: "0" },200);
	});
	$("#kt-overlay").on('click', function() {
		$("#kt-user-menu").animate({ right: "-250px" },200,function() {
			$(this).hide();
		});
		$("#kt-overlay").hide();
	});

	// $(window).on('resize orientationchange', function() {
	// 	if ($(window).width() > 640) {
	// 		$(".login-slide-wrapper").css("visibility","visible");
	// 		var txt = $("body #login-kt").html();
	// 		if (txt.match(/R\<\/a\>$/)) { txt = txt.replace(/R\<\/a\>$/,"R  </a>"); }
	// 		txt = txt.replace(/.{1}\<\/a\>$/,"&#9660;</a>");
	// 		$("#login-kt").html(txt);
	// 	} else {
	// 		$(".login-slide-wrapper").hide();
	// 		var txt = $("#login-kt").html();
	// 		if (txt.match(/R\<\/a\>$/)) {
	// 			return;
	// 		} else {
	// 			txt = txt.replace(/.{2}\<\/a\>$/,"</a>");
	// 			$("#login-kt").html(txt);
	// 		}
	// 	}
	// });

	$("#wo-menu-x","#kt-user-menu").click(function() {
		$("#kt-user-menu").animate({ right: "-250px" },200,function() {
			$(this).hide();
		});
		$("#kt-overlay").hide();
	});

	// Drop in the Login Window
	$("body").on( 'click', '#login-kt', function() {
		if ($(window).width() < 640) {
			window.location.href = "/login";
			return;
		}
		if ($(".kt_login_overlay").is(":visible")) {
			$(".kt_login_overlay").hide();
		} else {
			$(".kt_login_overlay").css({ opacity: "0.8"}).show();
		}
		var arro = ($(".login-slide-wrapper").is(":hidden")) ? "&#9650;" : "&#9660;";
		var txt = $(this).html().replace(/.{1}\<\/a\>$/,arro+"</a>");
		$(this).html(txt);
		$(".login-slide-wrapper").slideToggle(300);
	});
	$(".kt_login_overlay").click(function() {
		var arro = ($(".login-slide-wrapper").is(":hidden")) ? "&#9650;" : "&#9660;";
		var txt = $("#login-kt").html().replace(/.{1}\<\/a\>$/,arro+"</a>");
		$("#login-kt").html(txt);
		$(this).hide();
		$(".login-slide-wrapper").slideToggle(300);
	});
	$("#wo-menu-x",".login-slide-wrapper").click(function() {
		var txt = $("#login-kt").html().replace(/.{1}\<\/a\>$/,"&#9660;</a>");
		$("#login-kt").html(txt);
		$(".login-slide-wrapper").slideToggle(300);
		$(".kt_login_overlay").hide();
	});
	if ($(".login-slide-wrapper").length > 0) {
		$(".login-fb").find(".wp-social-login-provider.wp-social-login-provider-facebook").children("img").prop("src","wp-content/themes/waxom-child/images/facebook-connect.png");
	}
	if ($(".wp-social-login-provider.wp-social-login-provider-facebook",".login-fb").length > 0) {
		var home = window.location.href.replace(/([a-zA-Z])\/.*/,'$1');
		$(".wp-social-login-provider.wp-social-login-provider-facebook").children("img").prop("src",home+"/wp-content/themes/waxom-child/images/facebook-connect.png");
	}
	$("input",".form-submit").removeClass();


	// Slide in FEEDBACK form
	$("#feedback_click,.kt_feedback_overlay").click(function() { 
		if (parseInt($(".kt_feedback").css("right").replace(/px$/,'')) < 0) {
			$("#univ-create-team").hide();
			$(".kt_feedback_overlay").show().css({ opacity: "0.8" });
			$(".kt_feedback").animate({ right : 0 }, 300);
		} else {
			$(".kt_feedback").animate({ right : "-300px" }, 300, function() {
				$(".kt_feedback_overlay").css({ opacity: 0 }).hide();
				$("#univ-create-team").show();
			});
		}
	});


	$(".menu_standout > a").on('click', function(e) {
		e.preventDefault();
		e.stopPropagation();
		$(".dropdown-toggle",".menu_standout").click();
	});
	$(".menu_droponly > a").on('click', function(e) {
		e.preventDefault();
		e.stopPropagation();
		$(".dropdown-toggle",".menu_droponly").click();
	});


	// Set SESSION Variable on CREATE A TEAM button
	$("#univ-create-team").click(function() {
		if (!$("body").hasClass("logged-in")) {
			document.cookie = "STYXKEY_goto=cat; path=/;";
			var loc = $(location).attr('href').split("?");
			window.location = loc[0].replace(/\/$/,'') + "/?auto=login";
		} else {
			window.location = "/create-a-team";
		}
	});


	// Check for MAIL SENT on Contact Form 7 -- Send Result Message
	document.addEventListener( 'wpcf7mailsent', function( event ) {
		console.log("FIRING!");
		if ( '4038' == event.detail.contactFormId ) {
			$('.wpcf7-use-title-as-watermark').each(function(i,n) { $(n).val($(n).attr('title')).addClass('watermark'); });
			$('#feedback_click').click();
			alert('Thanks for the feedback!');
		} else if ( '3693' == event.detail.contactFormId ) {
			alert("Thanks for Inquiring!\n\nSomeone will get back to you soon!");
		} else {
			alert("MESSAGE SENT!\n\nWe will get back to you as soon as possible.\n\nThank you!");
		}
	}, false );


	// veented slider hack to get around tiling slide bug
	let checker = {count: 0}
	checker.id = setInterval(checkSlideInit, 200, $, checker)


});

function checkSlideInit($, checker) {
	checker.count++
	if (!$(".veented-slider-holder").length) {
		console.log("No Slider present... will stop checking")
		if (checker.id) clearInterval(checker.id)
	}
	else {
		console.log("resizing...")
		var resizeEvent = new Event('resize');
		window.dispatchEvent(resizeEvent);
		if (checker.id && checker.count >= 10) clearInterval(checker.id)
	}
}


	