// COOKIE
function setCookie(cname, cvalue, exdays) {
	var d = new Date();
	d.setTime(d.getTime() + (exdays*24*60*60*1000));
	var expires = "expires="+ d.toUTCString();
	document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
	var name = cname + "=";
	var decodedCookie = decodeURIComponent(document.cookie);
	var ca = decodedCookie.split(';');
	for(var i = 0; i <ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') {
			c = c.substring(1);
		}
		if (c.indexOf(name) == 0) {
			return c.substring(name.length, c.length);
		}
	}
	return "";
}
// MESSAGE
function message(mes) {
	$("body").append('<div class=message>'+mes+'</div>');
}
// Show ads every time
var times = 1;
function letAds() {
	if (times <= 3) {
		AD_COUNTDOWN = 15;
		AD_COUNTDOWN_TOSKIP = 6;
		//document.getElementById("outsome").innerHTML += AD_COUNTDOWN;
		document.getElementById("preloads").style.display = "block";
		document.getElementById("prban").style.marginTop = "12%";
		document.getElementById("prban").style.marginBottom = "6%";
		document.getElementById("skip").style.display = "none";
		document.getElementById("count").style.display = "block";
		document.getElementById("count").innerHTML = 5;
		setTimeout("countdown()",1000);
		setTimeout("countdown2()",1000);
		times++;
	}
}


// EARN COINS
function earnCoins(amount, goal) {
	var current = Math.floor(document.getElementById("usercoins").innerHTML);
	var now = current;
	function addCoins() {
		if (now != amount+current) {
			now += 1;
			document.getElementById("usercoins").innerHTML = now;
			$("body").append('<div class=coin_moves></div>');
			setTimeout(addCoins, 100);
			var audio = new Audio('../../images/sound/coin.mp3');
			try {audio.play();} catch {}
		}
		else {
			$('.coin_moves').remove();
			document.getElementById("pop_coins").classList.add('fade');
			setTimeout(function(){$('#pop_coins').remove();}, 3000);
		}
	}
	setTimeout(addCoins, 500);
	$("body").append('<div id=pop_coins><div>'+goal+'</div><p>'+amount+'</p></div>');

	//var iframe = document.getElementById("capthaframe");
	//recaptchaResponse = iframe.contentWindow.document.getElementById('g-recaptcha-response').value;
	//$.ajax({url: root+"script/earnCoins.php?amount="+amount+'&g-recaptcha-response='+recaptchaResponse, success: function(respond) {}});
	$.ajax({url: root+"script/earnCoins.php?amount="+amount, success: function(respond) {}});

    var audio = new Audio('../../images/sound/award.mp3');
	audio.play();
}


// --- GAMEFIN --- >
function gfMenu(on) {
	var button = document.getElementById("bm");
	var menu = document.getElementById("gfmenu");
	if (on == true) {
		menu.style.display = "none";
		button.style.display = "block";
	}
	else if (menu.style.display != "block") {
		menu.style.display = "block";
		button.style.display = "none";
	}
	else {
		menu.style.display = "none";
		button.style.display = "block";
	}
}

function vote(a) {
	var tit = document.getElementById("gameTit").innerHTML;
	$.ajax({
		url: root+"script/rateGame.php?tit="+tit+"&vote="+a,
		success: function(respond) {
			var out_txt = document.getElementById("voteoutput");
            if (respond == 7) {
            	message(LANG[13]);
            }
            else if (respond == 6) {
                message(LANG[14]);
            }
            else {
            	var rate = document.getElementById("rate");
            	rate.innerHTML = respond;
            	message(LANG[15]);
            	if (a == 'like') {
            	    document.getElementById('likebut').classList.add('but_like_on');
            	}
            	else if (a == 'dislike') {
            	    document.getElementById('disbut').classList.add('but_dislike_on');
            	}

            	// Prize
            	var votedToday = Math.floor(getCookie('votedToday'));
            	if (votedToday < 15) {
            		earnCoins(3,LANG[24]);
            		setCookie('votedToday', votedToday+1, 1);
            	}
            }
		}
	});
}
function addToFav(tit) {
	//var tit = document.getElementById("gameTit").innerHTML;
	$.ajax({
		url: root+"script/addToFav.php?tit="+tit,
		success: function(respond) {
            if (respond == 1) {
            	message(LANG[27]);
				document.getElementById("favbut").classList.remove('but_fav');
				document.getElementById("favbut").classList.add('but_fav_on');

				// Prize
            	var favsToday = Math.floor(getCookie('favsToday'));
            	if (favsToday < 1) {
            		earnCoins(3,LANG[25]);
            		setCookie('favsToday', favsToday+1, 1);
            	}
            }
            else if (respond == 0) {
                message(LANG[16]);
                document.getElementById("favbut").classList.remove('but_fav_on');
				document.getElementById("favbut").classList.add('but_fav');
            }
            else if (respond == 2) {
                message(LANG[17]+', <a href="'+root+'login">'+LANG[18]+'</a> '+LANG[19]);
            }
            else {
                message(LANG[20]);
            }
		}
	});
}

// Load More
var alreadyLoadedChars = 9;
var alreadyLoadedGames = 9;
var alreadyLoadedCats = 9;
var amount = 9;
function loadMore(what, button) {
	button.innerHTML = '<div class=loading></div>';

	if (what == 'chars') {
		$.ajax({
			url: root+"script/loadMore.php?q="+"&start="+Math.round(alreadyLoadedChars)+"&what="+what+"&amount="+amount,
			success: function(data) {
				button.innerHTML = LANG[22];
				$("#gfchars").append(data);
				alreadyLoadedChars += amount;
				if (data=="") {button.style.display = "none";}
			}
		});
	}
	else if (what == 'games') {
		$.ajax({
			url: root+"script/loadMore.php?q="+"&start="+Math.round(alreadyLoadedGames)+"&what="+what+"&amount="+amount,
			success: function(data) {
				button.innerHTML = LANG[22];
				$("#gfgames").append(data);
				alreadyLoadedGames += amount;
				if (data=="") {button.style.display = "none";}
			}
		});
	}
	else if (what == 'cats') {
		$.ajax({
			url: root+"script/loadMore.php?q="+"&start="+Math.round(alreadyLoadedCats)+"&what="+what+"&amount="+amount,
			success: function(data) {
				button.innerHTML = LANG[22];
				$("#gfcats").append(data);
				alreadyLoadedCats += amount;
				if (data=="") {button.style.display = "none";}
			}
		});
	}
}

function fullScreen() {
	elem = document.getElementById("gfgame");
	document.getElementById("cfs").style.display = "block";
	if (elem.requestFullscreen) {
		elem.requestFullscreen();
	} else if (elem.mozRequestFullScreen) { /* Firefox */
		elem.mozRequestFullScreen();
	} else if (elem.webkitRequestFullscreen) { /* Chrome, Safari and Opera */
		elem.webkitRequestFullscreen();
	} else if (elem.msRequestFullscreen) { /* IE/Edge */
		elem.msRequestFullscreen();
	}
}

function cancelFullScreen() {
	document.getElementById("cfs").style.display = "none";
	if (document.exitFullscreen) {
		document.exitFullscreen();
	} else if (document.mozCancelFullScreen) { /* Firefox */
		document.mozCancelFullScreen();
	} else if (document.webkitExitFullscreen) { /* Chrome, Safari and Opera */
		document.webkitExitFullscreen();
	} else if (document.msExitFullscreen) { /* IE/Edge */
		document.msExitFullscreen();
	}
}

// PRIZES
/*function timeOnSite() {
	var timeToday = Math.floor(getCookie('timeToday'));
	var timeNow = Date.now();

	if (timeToday == 0) {
		timeToday = timeNow;
		setCookie('timeToday', timeToday, 1);
	}
	else if (timeNow-timeToday >= 60000 && getCookie('timePrize') < 1) {
		earnCoins(5,"play games for 1 minute");
		setCookie('timePrize', 1, 1);
	}
	else if (timeNow-timeToday >= 300000 && getCookie('timePrize') < 2) {
		earnCoins(10,"play games for 2 minutes");
		setCookie('timePrize', 2, 1);
	}
	else if (timeNow-timeToday >= 3600000 && getCookie('timePrize') < 3) {
		earnCoins(25,"play games for 1 hour");
		setCookie('timePrize', 3, 1);
	}
}*/
function gamesPlayed() {
	var playsToday = Math.floor(getCookie('playsToday'));
	if (playsToday >= 2 && getCookie('playsPrize') < 1) {
		earnCoins(3,"3 "+LANG[26]);
		setCookie('playsPrize', 1, 1);
	}
	else if (playsToday >= 4 && getCookie('playsPrize') < 2) {
		earnCoins(5,"5 "+LANG[26]);
		setCookie('playsPrize', 2, 1);
	}
	else if (playsToday >= 6 && getCookie('playsPrize') < 3) {
		earnCoins(10,"10 "+LANG[26]);
		setCookie('playsPrize', 3, 1);
	}
	setCookie('playsToday', playsToday+1, 1);
}
// Some button functions
function reload() {
	window.location.reload();
}
function back() {
	window.history.go(-1);
}
function home() {
	window.location.href = rootla;
}
function sound() {
	var audio = new Audio('../../images/sound/blank.mp3');
	try {audio.play();} catch {}
	document.getElementById("sound").style.display = "none";
}

// Remove google badge
document.addEventListener('DOMContentLoaded', () => {
const observer = new MutationObserver(() => {
	document.querySelector(".google-revocation-link-placeholder")?.remove();
});
observer.observe(document.body, {
	childList: true,
});
});

// LOAD
window.onload = function() {
	//setInterval(timeOnSite, 5000); // Check Time on Site
	//setInterval(letAds, 90000); // Show ads every time
	if (window.location.href.indexOf("/gamefin") > -1) {setTimeout(gamesPlayed, 1000);} // gamePlays
};

