

$(function () {
const storeVpnCountdownCookie = bfGetLanguage() + '_vpn_' + new Date().getFullYear() + '_Countdown';
const storeVpnMintus = location.host.includes("itopvpn.com") ? 20 : 1;
// const storeVpnMintus = 20;
let bfStartTime = '00',
	bfCountdownS = '00',
	bfCountdown = null,
	bfCountdownMs = '000',
	bfIsHidden = false,
	store_data = null,
	bfMobData = null;

function storeVPNSoldOut() {
	$(`.countdownM`).html('00');
	$(`.countdownS`).html('30');
	// $(".store-banner.store-vpn").addClass("store-sold");
	// $(".store-vpn-item.vpn-active").slideUp();
	// $(".store-right-pop").removeClass("store-on");
	// $(".store-vpn-title .title-left").html($(".store-vpn-title .title-left").attr("data-sold"));
	// $(".store-vpn-title .title-right").html($(".store-vpn-title .title-right").attr("data-sold"));
	// $(".store-banner.store-vpn .store-vpn-item").eq(2).find(".store-vpn-btn").removeClass("vpn-white-btn").addClass("vpn-red-btn");
}

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 (!location.host.includes("itopvpn.com")) {
	// 	console.log(location.host.includes("itopvpn.com") ? '' : `m: ${m} s: ${s}`);
	// }
	$(`.countdownM`).html(m);
	$(`.countdownS`).html(s);
	if ((new Date().getTime() - bfCountdown) > 3000 &&
		(new Date().getTime() - bfCountdown) < 24 * 60 * 60 * 1000) {
		// console.log('end');
		storeVPNSoldOut();
		bfStartTime = 'end';
		return;
	}
	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();
				console.log(new Date().getTime(),bfCountdown);
			} else if ((new Date().getTime() - bfCountdown) > 3000 &&
				(new Date().getTime() - bfCountdown) < 24 * 60 * 60 * 1000) {
				console.log('end');
				storeVPNSoldOut();
				$(".store-vpn-item.vpn-active").show()
				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();
	}
});