

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 () {
const storeVpnCountdownCookie = bfGetLanguage() + '_vpn_' + new Date().getFullYear() + '_Countdown';
const storeVpnMintus = location.host.includes("itopvpn.com") ? 20 : 20;
let bfStartTime = '00',
	bfCountdownS = '30',
	bfCountdown = null,
	bfCountdownMs = '000',
	bfIsHidden = false,
	store_data = null,
	bfMobData = null;

function storeVPNSoldOut() {
	$(`.countdownM`).html('00');
	$(`.countdownS`).html('30');
}

function storeVPNCountTime() {
	if (bfIsHidden) return
	bfStartTime -= 50;
	var leftTime = bfStartTime;
	let d, h, m, s, ms;
	if (leftTime <= 24 * 60 * 60 * 1000) {
		d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
		h = Math.floor(((leftTime / 1000 / 60 / 60) % 24) + 24 * d);
		m = Math.floor((leftTime / 1000 / 60) % 60);
		s = Math.floor((leftTime / 1000) % 60);
		ms = Math.floor(leftTime % 1000);
		if (ms < 100) {
			ms = '0' + ms;
		}
		if (s < 10) {
			s = '0' + s;
		}
		if (m < 10) {
			m = '0' + m;
		}
		// if(h < 10) {
		h = h;
		// }
	}

	

	if ((new Date().getTime() - bfCountdown) > 30 &&
		(new Date().getTime() - bfCountdown) < 24 * 60 * 60 * 1000) {
		console.log('end');
		storeVPNSoldOut();
		bfStartTime = 'end';
		return;
	}
	else {
		$(`.countdownM`).html(m);
	  $(`.countdownS`).html(s);
	}
	setTimeout(storeVPNCountTime, 50);
}
	if ($(window).width() >= 769) {
		bfCountdown = bfGet(storeVpnCountdownCookie);
		if (bfIsBlank(bfCountdown)) {
			$(".store-vpn-item.vpn-active").show();
			bfCountdown = new Date().getTime() + storeVpnMintus * 60 * 1000;
			bfSet(storeVpnCountdownCookie, bfCountdown);
			bfStartTime = storeVpnMintus * 60 * 1000;
			storeVPNCountTime();
		} else {
			if (new Date().getTime() - bfCountdown >= 24 * 60 * 60 * 1000) {
				$(".store-vpn-item.vpn-active").show();
				bfCountdown = new Date().getTime() + storeVpnMintus * 60 * 1000;
				bfSet(storeVpnCountdownCookie, bfCountdown);
				bfStartTime = 24 * 60 * 60 * 1000;
				storeVPNCountTime();
			} else if ((new Date().getTime() - bfCountdown) > 0 &&
				(new Date().getTime() - bfCountdown) < 24 * 60 * 60 * 1000) {
				console.log('end');
				storeVPNSoldOut();
				bfStartTime = 'end';
			} else {
				$(".store-vpn-item.vpn-active").show();
				bfStartTime = bfCountdown - new Date().getTime();
				storeVPNCountTime();
			}
		}
	} else {
		$(".store-banner.store-vpn").removeClass("store-sold");
		$(".store-vpn-item.vpn-active").slideDown();
	}
});