$(document).ready(function() {
	//FOR MENU
	var url = window.location;
	$("#content ul.navbar-nav a").each(function(){
		if($(this).prop("href")==url){
			$(this).parent().addClass("active");
			$(this).append($('<span class="sr-only">').text('(current)'))
		}
	});

	//$("#marquee ul").liScroll();

	/* MODAL */
	$('#myModal').on('show.bs.modal', function (event) {
	  var button = $(event.relatedTarget) // Button that triggered the modal
	  var recipient = button.data('title') // Extract info from data-* attributes
	  var targetUrl = button.data('url') // Extract info from data-* attributes
	  // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
	  // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
	  var modal = $(this)
	  modal.find('.modal-title').text(recipient)
	  modal.find('.modal-body').append($('<iframe class="iframe">').prop("src", targetUrl));
	}).on('hidden.bs.modal', function (event){
	  var modal = $(this);
	  $(".loading").show();
	  modal.find('.modal-body .iframe').remove();
	});

	/* Promotions banner slidedown */
	$(document).on("click", ".aimg", function(){
		$(this).closest(".abox").find(".acon").slideToggle(300);
	});

	let findImgHost = setInterval(function () {
		if (typeof entityData == 'object') {
			clearTimeout(findImgHost);

			let domain = entityData.imageDomain || '';

			// Request by pragmatic
			let hot = $('.games .home-games:first-child .game-items, .featured .home-games:first-child .game-items');

			hot.find('[action="slot-pm"], [gc="38"]').remove();
			hot.prepend(`
				<a class="box" href="/secure/shr/playGame.html?type=slot-pm&gc=vs20olympgcl" rel="nofollow" target="_blank">
					<div class="box-item">
						${$('#showD').is(':visible') ? '<span><button>Main</button></span>' : ''}
						<picture>
							<source srcset="${domain}/common/assets/images/home/pp-request/vs20olympgcl.webp" type="image/webp">
							<img src="${domain}/common/assets/images/home/pp-request/vs20olympgcl.png" width="200" height="200" alt="Fortune of Olympus" loading="lazy">
						</picture>
					</div>
					<span>Fortune of Olympus</span>
				</a>
				<a class="box" href="/secure/shr/playGame.html?type=slot-pm&gc=vswaysmahwblck" rel="nofollow" target="_blank">
					<div class="box-item">
						${$('#showD').is(':visible') ? '<span><button>Main</button></span>' : ''}
						<picture>
							<source srcset="${domain}/common/assets/images/home/pp-request/vswaysmahwblck.webp" type="image/webp">
							<img src="${domain}/common/assets/images/home/pp-request/vswaysmahwblck.png" width="200" height="200" alt="Mahjong Wins 3 - Black Scatter" loading="lazy">
						</picture>
					</div>
					<span>Mahjong Wins 3</span>
				</a>
			`);

			let poplar = $('.games .home-games:eq(1) .game-items, .featured .home-games:eq(1) .game-items');

			poplar.find('[action="pm-suit"], [href*="sg=pm"]').remove();
			poplar.prepend(`
				<a class="box" href="/secure/shr/playGame.html?type=slot-pm&gc=vs20olympgold" rel="nofollow" target="_blank">
					<div class="box-item">
						${$('#showD').is(':visible') ? '<span><button>Main</button></span>' : ''}
						<picture>
							<source srcset="${domain}/common/assets/images/home/pp-request/vs20olympgold.webp" type="image/webp">
							<img src="${domain}/common/assets/images/home/pp-request/vs20olympgold.png" width="200" height="200" alt="Gates of Olympus SS" loading="lazy">
						</picture>
					</div>
					<span>Gates of Olympus SS</span>
				</a>
				<a class="box" href="/secure/shr/playGame.html?type=pm-suit" rel="nofollow" target="_blank">
					<div class="box-item">
						${$('#showD').is(':visible') ? '<span><button>Main</button></span>' : ''}
						<picture>
							<source srcset="${domain}/common/assets/images/home/pp-request/24D-Spin.webp" type="image/webp">
							<img src="${domain}/common/assets/images/home/pp-request/24D-Spin.png" width="200" height="200" alt="24D Spin" loading="lazy">
						</picture>
					</div>
					<span>24D Spin</span>
				</a>
			`);

			// End request by pragmatic
		}
	}, 500);

	let marqueeInt = setInterval(() => {
		if ($('marquee').length) {
			clearInterval(marqueeInt);
			loadScript('/js/jquery.marquee.min.js').then(() => {
				$('marquee').marquee({
					duration: 25000,
					gap: 0,
					delayBeforeStart: 0,
					direction: 'left',
					duplicated: false,
					pauseOnHover: true // This replaces the manual hover code above
				});
			});
		}
	}, 500);
});

function resetForm(formId){
	$(formId)[0].reset();
	$(formId).find('.form-control').removeClass('valid');
	$(formId).find('.form-control').removeClass('error');
	$(formId).find('label.error').text("").hide();
}

function sessionLogout(){
	alertMsg(messages.sessionLogout);
	delete_cookie('logged');
	setTimeout(function(){
		ajaxCaller('get', logout_link, '', function(){});
	}, 2000);
	//setTimeout( function() {  window.location = logout_link}, 2000 );
}

function iframeLoad(title){
	$(".loading").fadeOut();
	if(typeof title != 'undefined'){
		$('#myModal').find('.modal-title').text(title)
	}
}

function setCookie(cname, cvalue) {
	var d = new Date();
	d.setTime(d.getTime() + (30 * 24 * 60 * 60 * 1000));
	var expires = "expires="+d.toUTCString();
	document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
	var name = cname + "=";
	var ca = document.cookie.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 "";
}
