(function ($) {
    "use strict";


    jQuery(document).ready(function($){
        var hasAnim = $('.all-games').hasClass('anim-change');
        hasAnim = false;

        
        $('.game-menu').find('.nav-link').on('click', function(){
            $('.all-games').removeClass('anim-change');
            setTimeout(function() { 
                $('.all-games').addClass('anim-change');
            }, 300);
        });

        // games sidebar navlink activate
        $('.game-menu').find('.nav-link').on('click', function(){
            $(this).addClass('active');
            $(this).parents('.nav-item').siblings().find('.nav-link').removeClass('active');
        });


        /**----------------------
         *  Back to top
         * --------------------**/
        $(document).on('click', '.back-to-top button',function(){
            $('html,body').animate({
                scrollTop:0
            },3000);
        });

       
    });



    // fixed navbar
           
    $(window).on('scroll', function(){
        var headerSection = $('.header');

        if ($(window).scrollTop() > 300) {
            headerSection.addClass('header-fixed fadeInDown animated');
        } else {
            headerSection.removeClass('header-fixed fadeInDown animated');
        }
        
    });


    $(window).on('load',function(){
        /*-----------------
            preloader
        ------------------*/
        var preLoder = $(".preloader");
        preLoder.fadeOut(1000);
       
    });
	
	$('.pop-games').owlCarousel({
		items:6,
		loop:true,
		margin:20,
		nav:false,
		dots: false,
		center:false,
		navText: ["<span class='arrow-left'></span>","<span class='arrow-right'></span>"],
		autoplay:true,
		autoplayTimeout:3000,
		autoplayHoverPause:true,
		responsive:{
			0:{
				items:2,
				center: true,
			},
			600:{
				items:4
			},
			1000:{
				items:6
			}
		}
	});
	$('.nav-desktop').owlCarousel({
		items:8,
		loop:false,
		margin:0,
		autoWidth:false,
		nav:true,
		dots: false,
		autoplay:false,
		navText: ["<i class='fas fa-angle-left'></i>","<i class='fas fa-angle-right'></i>"],
	});
	$('.nav-mobile').owlCarousel({
		items:8,
		loop:false,
		margin:0,
		nav:true,
		dots: false,
		autoplay:false,
		navText: ["<i class='fas fa-angle-left'></i>","<i class='fas fa-angle-right'></i>"],
		responsive:{
			0:{
				items:1
			},
			600:{
				items:5
			},
			1000:{
				items:8
			}
		}
	});
	
	$('.simple-marquee-container').SimpleMarquee({
		duration: 150000,
	});

	function shuffle(array) {
		let currentIndex = array.length,  randomIndex;
	  
		// While there remain elements to shuffle.
		while (currentIndex != 0) {
	  
		  // Pick a remaining element.
		  randomIndex = Math.floor(Math.random() * currentIndex);
		  currentIndex--;
	  
		  // And swap it with the current element.
		  [array[currentIndex], array[randomIndex]] = [
			array[randomIndex], array[currentIndex]];
		}
	  
		return array;
	}

	function addZero(i) {
		if (i < 10) {i = "0" + i}
		return i;
	  }

	jQuery(function($) {
		$('.nav-item').each(function() {
			var pathname = window.location.pathname;
			var navlink = $(this).find('a').attr('href');
			if(pathname == navlink) {
				$(this).find('a').addClass('active');
			}
		});

		$("#dataGame").on("keyup", function() {
			var value = $(this).val().toLowerCase();
			$(".all-games .row .col-md-3").filter(function() {
			  $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
			});
		});

		var getUrlParameter = function getUrlParameter(sParam) {
			var sPageURL = window.location.search.substring(1),
				sURLVariables = sPageURL.split('&'),
				sParameterName,
				i;
		
			for (i = 0; i < sURLVariables.length; i++) {
				sParameterName = sURLVariables[i].split('=');
		
				if (sParameterName[0] === sParam) {
					return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
				}
			}
			return false;
		}

		if(linking == 'no') {
			$('.buttons .loginlink').attr('href', refsite)
			$('.buttons .daftar').attr('href', refsite)
			$('.modal-footerLink.mainlink').attr('href', refsite)
		}
		var refkey = getUrlParameter('ref')
		if(refkey == false) {
			var reflink = refsite
		} else {
			var reflink = refsite+'?ref='+refkey
			$('.mainlink').attr('href', reflink)
			$('a.daftar').attr('href', reflink)
			$('.single-game a').each(function() {
				$(this).attr('href', reflink)
			})
			$('a.nav-link').each(function() {
				var navlink = $(this).attr('href')
				if(refkey) {
					navlink = $(this).attr('href') + '?ref='+refkey
				}
				$(this).attr('href', navlink)
			})
		}

		$('.percent-bar.high').parent().parent().addClass('gacor');
		$('.gacor .play-button .btn').text('Gacor')

		var ptext = document.getElementsByClassName("percent");

		//var random = ["âœ”ï¸âœ–ï¸âœ–ï¸", "âœ–ï¸âœ”ï¸âœ–ï¸", "âœ–ï¸âœ–ï¸âœ”ï¸", "âœ”ï¸âœ–ï¸âœ”ï¸", "âœ–ï¸âœ”ï¸âœ”ï¸", "âœ–ï¸âœ–ï¸âœ–ï¸"];
		var random = ["✅❌❌", "❌✅❌", "❌❌✅", "❌❌❌", "✅✅✅", "❌✅✅", "✅❌✅", "✅✅❌", "✅❌✅"];
		//var random = ["✅❌❌", "❌❌✅", "✅✅❌"];
		var random2 = [10, 20, 30, 50, 70, 100];
		//var random3 = ["Auto", "Auto", "Manual 3", "Manual 5", "Manual 7", "Manual 9", "Auto", "Auto", "Auto", "Auto", "Auto", "Auto"];
		var random3 = ["Auto", "Auto", "Manual", "Manual", "Manual", "Auto"];

		setInterval(function() {
			const d = new Date();
			let seconds = d.getSeconds();
			let minutes = d.getMinutes();
			let hour = d.getHours();
			$('.detik').html(addZero(seconds))
			$('.menit').html(addZero(minutes))
			$('.hour').html(addZero(hour))
		}, 1000)

		for (let i = 0; i < ptext.length; i++) {
			const d = new Date();
			var date = d.getUTCDate();
			var day = d.getUTCDay();
			var year = d.getUTCFullYear();
			var month = d.getUTCMonth();
			var hour = d.getUTCHours();
			var min = d.getMinutes();
			if (min < 30) {
				min = 1;
			} else {
				min = 2;
			}

			var xx = day + year * (month + 1) * date;

			xx = Math.pow(xx, (hour * min));
			xx = xx * x[i];

			if (popGames.includes(i) == true) {
				xx = xx % 27;
				xx += popgameVal;
			} else {
				xx = xx % 83;
				xx += 8;
			}
			/*
			if (i == 4 || i == 9) {
				xx = xx % 27;
				xx += 65;
			} else {
				xx = xx % 83;
				xx += 8;
			}
			*/

			const as = "percent-bar-" + i;
			const asd = "percent-txt-" + i;
			var percentTxt = document.getElementsByClassName("percent-txt-" + i);
			//const gameTitle = $('.percent-txt-' + i).parent().parent().parent().parent().parent().find('.game-img').attr('alt')
			var bar = document.getElementById("percent-bar-" + i);
			var popbar = $('.pop-single-game-wrapper .percent-bar-'+i)
			const bartext = document.getElementById("card-title" + i);
			const playurl = $('.single-game a').attr('href')
			if (percentTxt != null) {
				percentTxt.innerHTML = xx + "%";
				$('.percent-txt-' + i).html(xx + "%")
				$('.pop-percent-txt-'+i).html(xx + "%")
				$('#percent-bar-' + i).attr('aria-valuenow', xx).css('width', xx + "%");
				$('.percent-bar-' + i).attr('aria-valuenow', xx).css('width', xx + "%");
				$('*[data-pola="'+i+'"]').click(function() {
					var parentModalContent = $(this).parent().parent().parent().parent()
					//console.log(parentModalContent)
					const gameTitle = parentModalContent.find('.game-img img').attr('alt')
					$('#polaGacorModalLabel').html(gameTitle)
					var valuenow = $('#percent-bar-' + i).attr('aria-valuenow')
					var gameImg = parentModalContent.find('.game-img').find('img').attr('src')
					$('.jam .game-img img').attr('src', gameImg)
					$('#percentText').html($('.percent-txt-' + i).html())
					$('.percentBar').attr('aria-valuenow', valuenow).css('width', valuenow + "%");
					$('.playgame').attr('href', playurl)
					if(valuenow < 30) {
						$('.percentBar').removeClass('medium high').addClass('low')
					} else if (valuenow > 70) {
						$('.percentBar').removeClass('medium low').addClass('high')
					} else {
						$('.percentBar').removeClass('low high').addClass('medium')
					}
					var jam = $(".jamgacorRange");
					var pola1 = [$(".pola1"), $(".pola2"), $(".pola3")];
					var notes = $('.catatan')
					if (valuenow >= 30) {
						var hour2 = (hour + 7) % 24;
						var target = (valuenow % 3) + 1;
						var randomTarget = (hour2 + target) % 24;
						if (randomTarget < 10) {
							randomTarget = "0" + randomTarget;
						}
						var min1 = valuenow % 60;
						var min2 = (valuenow * date) % 60;
						if (min1 < 10) {
							min1 = "0" + min1;
						}
						if (min2 < 10) {
							min2 = "0" + min2;
						}
						if (hour2 < 10) {
							hour2 = "0" + hour2;
						}
						jam.html(hour2 + ":" + min1 + " - " + randomTarget + ":" + min2);
					/*	var n = i % 6;
						var p = [(x[n] + valuenow) % 6, (x[2 * n] + valuenow) % 6, (x[3 * n] + valuenow) % 6];
						console.log(x[n])
						console.log(p) 

						var n2 = i % 6;
						var p2 = [(x[4 * n2] + valuenow) % 4, (x[5 * n2] + valuenow) % 5, (x[6 * n2] + valuenow) % 5]; */
						let p = [Math.floor((Math.random() * 8)), Math.floor((Math.random() * 8)), Math.floor((Math.random() * 8))]
						let p2 = [Math.floor((Math.random() * 5)), Math.floor((Math.random() * 5)), Math.floor((Math.random() * 5))]

						for (let j = 0; j < 3; j++) {
							for (let k = 0; k < 3; k++) {
								if (p[j] == p[k] && j != k) {
									p[k] = (p[k] + 1) % 6;
								}
							}
						}
						var p3 = [Math.pow(p[0], p2[0]) % 12, Math.pow(p[1], p2[1]) % 12, Math.pow(p[2], p2[2]) % 12];
						for (let j = 0; j < 3; j++) {
						/*	if (random3[p3[j]] == "Auto") {
								pola1[j].html(random[p2[j]] + " - " + random2[p2[j]]);
							} else {
								pola1[j].html(random[p2[j]] + " - " + random2[p2[j]]);
							} */
							//pola1[j].html(random[p[j]] + " - " + random2[p2[j]]);
							pola1[j].html('<span class="row g-0"><span class="col-4 text-end">' + random3[p2[j]] + '</span><span class="col-4 text-center">' + random2[p2[j]] + '</span><span class="col-4 text-start">' + random[p2[j]] + '</span></span>');
						}
					} else {
						jam.html("-");
						pola1[0].html('');
						pola1[1].html('<div class="notes text-start px-2">RTP Permainan ini sedang <span style="font-weight: 600; color: #ef0000;">Rendah</span>, silahkan memilih permainan dengan RTP tinggi untuk memaksimalkan kemenangan. </div>');
						pola1[2].html('');
					}
				})
				if (xx < 30) {
					bar.classList.add("low")
					popbar.addClass('low')
					$('.percent-txt-'+i).parent().parent().parent().removeClass('gacor semi').addClass('rungkad')
				} else if (xx > 70) {
					bar.classList.add("high")
					popbar.addClass('high')
					$('.percent-txt-'+i).parent().parent().parent().removeClass('semi rungkad').addClass('gacor')
				} else {
					bar.classList.add("medium")
					popbar.addClass('medium')
					$('.percent-txt-'+i).parent().parent().parent().removeClass('gacor rungkad').addClass('semi')
				}
			}
		}
		
	});
    
}(jQuery));	