function bfGetLanguage() {
	let countriesArrLang = ["en", "ru", "it", "de", "jp", "es", "sv", "ar", "nl", "fr", "dk", "pt", "pl"];
	let bfGetLanguage = "";
	bfGetLanguage = $("html").attr("lang").substring(0, 2);
	if (bfGetLanguage == 'ja') bfGetLanguage = 'jp';
	var countriesLangName = countriesArrLang.filter(function (item) {
		if (item === bfGetLanguage) {
			return true;
		}
		return false;
	});
	if (countriesLangName.length > 0) {
		// return 'sv';
		return countriesLangName[0];
	} else {
		return "en";
	}
}

function bfSet(name, value) {
	var Days = 1;
	var exp = new Date();
	exp.setTime(exp.getTime() + 60 * 60 * 1000 * 24 * Days);
	document.cookie = name + "=" + encodeURIComponent(value) + ";expires=" + exp.toGMTString();
}

function bfGet(name) {
	var exp = new Date();
	var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
	if (arr = document.cookie.match(reg))
		return decodeURIComponent(arr[2]);
	else
		return null;
}

function bfIsBlank(data) {
	if (
		data == null ||
		data === 'null' ||
		data === '' ||
		data === undefined ||
		data === 'undefined' ||
		data === 'unknown'
	) {
		return true;
	} else {
		return false;
	}
}

$(function () {
	/**
	 * VPN & ISR center popup
	*/
	let btn_interVal = null;
	let btn_normal = '';
	let storeControlName = $(".store-banner").length > 0 ? $(".store-banner").attr("data-control") : 'isrb';
	let storeCenterPopupMintus = bfGetLanguage() + '_' + storeControlName + '_center_popup_mintus' + new Date().getFullYear();
	let storeCenterPopupNum = bfGetLanguage() + '_' + storeControlName + '_center_popup_num' + new Date().getFullYear();
	let storeCenterPopupCookie = bfGetLanguage() + '_' + storeControlName + '_center_popup_' + new Date().getFullYear();
	const countSec = location.host.includes("itopvpn.com") ? 60 : 10;
	const waiteSec = location.host.includes("itopvpn.com") ? 60 : 3;

	let vpnisr_state = null;
	let interVal;

	function btShowNum() {
		if (bfIsBlank(bfGet(storeCenterPopupMintus))) {
			bfSet(storeCenterPopupMintus, countSec);
		}
		let store_shownums = bfGet(storeCenterPopupMintus);
		if (!btn_interVal) {
			btn_interVal = setInterval(() => {
				setTimeout(() => {
					store_shownums--;
					if (store_shownums > 0) {
						bfSet(storeCenterPopupMintus, store_shownums);
						console.log(store_shownums);
						$('.store-pop-wrap .center-pop-sec').html(`(<em>${store_shownums}</em>s)`);
					} else {
						if (btn_interVal) {
							window.clearInterval(btn_interVal);
							clearInterval(btn_interVal);
							btn_interVal = null;
						}
						btCnterPopClose();
					}
				}, 0);
			}, 1000);
		}
	};

	function bfGetQueryVariable(variable) {
		var query = window.location.search.substring(1);
		var vars = query.split("&");
		for (var i = 0; i < vars.length; i++) {
			var pair = vars[i].split("=");
			if (pair[0] == variable) {
				return pair[1];
			}
		}
		return (false);
	}

	function btShowCenterPop() {
		$(".store-pop-wrap").addClass("store-on");
		if ($(".store-banner").attr("data-control") == 'vpn') {
			gtag('event', bfGetLanguage() + '_isr1y1pcpop_show_pc_store_bfpop');
		} else if ($(".store-banner").attr("data-control") == 'isr') {
			gtag('event', bfGetLanguage() + '_isr1y1pcpop_show_' + gaPrefix + '_store_bfpop');
		}
		btShowNum();
		if (interVal) {
			window.clearInterval(interVal);
			clearInterval(interVal);
			interVal = null;
		}
	}

	function btCnterPopClose() {
		bfSet(storeCenterPopupCookie, 'close');
		$(".store-pop-wrap").removeClass("store-on");
		setTimeout(() => {
			$(".store-pop-wrap").remove();
		}, 500);
	}
	// storeCenterPopupNum

	if ($(window).width() >= 769) {
		if (bfIsBlank(bfGet(storeCenterPopupCookie))
			|| bfIsBlank(bfGet(storeCenterPopupNum))) {
			bfSet(storeCenterPopupCookie, 0);
			bfSet(storeCenterPopupNum, 0);
		}

		let countnum = bfGet(storeCenterPopupCookie);
		vpnisr_state = bfGet(storeCenterPopupNum);

		if (vpnisr_state == 'close') {
			console.log('close');
			window.clearInterval(interVal);
			clearInterval(interVal);
			interVal = null;
		} else {
			console.log('no-close');
			interVal = setInterval(() => {
				setTimeout(() => {
					countnum++;
					bfSet(storeCenterPopupNum, countnum);
				}, 0);
			}, 1000);
		}

		let numdsf = 0;

		$(".to-top-etc").css("bottom", " 2%");
		
		if (bfGetQueryVariable('ref') == 'navi_bf'
			|| bfGetQueryVariable('ref') == 'banner_bf'
			|| bfGetQueryVariable('ref') == 'pop_bf'
			|| bfGetQueryVariable('ref') == 'rpop_bf') {
			$(".store-right-pop,.store-pop-wrap").hide();
			numdsf = Math.floor(Math.random() * (1 - 100) + 100);
			if (numdsf >= 1 && numdsf <= 50) {
				$(".store-right-pop[data-type='vpn'],.store-pop-wrap[data-type='vpn']").show();
			} else {
				$(".store-right-pop[data-type='isr'],.store-pop-wrap[data-type='isr']").show();
			}
		} else { 
			$(".store-right-pop,.store-pop-wrap").show();
		}

		// if (bfGet(storeCenterPopupCookie) == 0 && countnum > waiteSec) {
		// 	btShowCenterPop();
		// }
		// document.body.addEventListener('mouseout', function (evt) {
		// 	if (countnum > waiteSec) {
		// 		if (!evt) var evt = window.event
		// 		var to = evt.relatedTarget || evt.toElement
		// 		if (!to || to.nodeName == "HTML") {
		// 			vpnisr_state = bfGet(storeCenterPopupCookie);
		// 			console.log(vpnisr_state);
		// 			if (evt.clientY < 6 && vpnisr_state == 0) {
		// 				btShowCenterPop();
		// 			}
		// 		}
		// 	}
		// });
	}

	$(window).on("scroll", function () {
		if ($(".store-right-pop").length > 0 && $(window).width() >= 769) {
			if (!$(".store-banner").hasClass("store-sold")) { 
				if ($(window).scrollTop() > $(".store-product").offset().top - 200) {
					$(".store-right-pop").addClass("store-on");
				} else {
					$(".store-right-pop").removeClass("store-on");
				}
			}
		}
	});

	$(".store-right-pop").off("click").on("click", function (event) {
		// $("html, body").animate({
		// 	scrollTop: 0
		// }, "slow");
		window.scrollTo({
			top: 0,
			behavior: "smooth",
		});
	});

	$('.store-pop-wrap .center-pop-btn').click(function () {
		btCnterPopClose();
	});

	$('.store-pop-wrap .store-popup-close').click(function () {
		btCnterPopClose();
	});
});