/*  5965ea66-5f8d-44e3-9a82-7814a96bd0e2 */

/**
 *	playzo-pixel 1.0
 *	@author Christoph Suess <christoph.suess@playzo.de>
 * 	@date 22.07.2019
 *
 * USAGE
 * =========
 * 1. Implement the Javascript Pixel Tag into your site at the very bottom of the <body>.
 * 2. Use the HTTP-GET Parameters &cid=000&aid=000&source1=XXX&source2=XXX&source3=XXX to track the campaignId (cid) and the adId (aid) on the page where the pixel is put into. They will be saved to db.
 *
 * <head></head>
 * <body>
 * Hello World
 * 
 * <script src="https://backend.playzo.de/index.php/tracking/pixel/index?action=landing&uid=0000000000&puid=0000"></script>
 * </body>
 * 
 * PARAMETERS OF THE PIXEL:
 *	action = string representing the place of integration
 *	uid = int (optional) representing the external user-id (of the project, not backend-user-id!)
 *	puid = mixed (optional) playzo-backend-user-id (id as int or userKey as string) 
 *
 */

var pzcc = playzo_getCookie('playzo_cc');
var bIsLoggedin = (!isNaN(parseInt('')) || !isNaN(parseInt('')) || !isNaN(parseInt('')) );
if( (pzcc && pzcc.indexOf('analysis') > 0 ) || bIsLoggedin) {

	var playzo_referrer = document.referrer;
	var playzo_query = window.location.search.substring(1);
	var playzo_qs = playzo_parse_query_string(playzo_query);
	var playzo_Uid = '5965ea66-5f8d-44e3-9a82-7814a96bd0e2';
	
	var playzo_xhr = new XMLHttpRequest();
	playzo_xhr.open('GET', 'https://backend.playzo.de/index.php/tracking/pixel/track' + 
		'?prms=' + (JSON.stringify(playzo_qs) == '{"":"undefined"}' ? '' : JSON.stringify(playzo_qs) )  + 
		'&url=' + encodeURIComponent(window.location) + 
		'&rfr=' + encodeURIComponent(playzo_referrer) +
		'&guid=' + playzo_Uid +  
		'&uid=' + 
		'&puid=' +
		'&act=landing');
	playzo_xhr.onload = function() {
	    if (playzo_xhr.status === 200) {
	        console.log('playzo-pixel loaded');
	        
	        var playzo_div = document.createElement("div");
			playzo_div.innerHTML = playzo_xhr.responseText;
			document.body.appendChild(playzo_div);
	    }
	    else {
	        console.log('playzo-pixel: Request failed.  Returned status of ' + playzo_xhr.status);
	    }
	};
	playzo_xhr.send();
}


function playzo_parse_query_string(query) {
  var vars = query.split("&");
  var query_string = {};
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split("=");
    var key = decodeURIComponent(pair[0]);
    var value = decodeURIComponent(pair[1]);
    if (typeof query_string[key] === "undefined") {
      query_string[key] = decodeURIComponent(value);
    } else if (typeof query_string[key] === "string") {
      var arr = [query_string[key], decodeURIComponent(value)];
      query_string[key] = arr;
    } else {
      query_string[key].push(decodeURIComponent(value));
    }
  }
  return query_string;
}
function playzo_getCookie(name) {
    var v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
    return v ? v[2] : null;
}


/**
 * Consent Banner (CC)
 */
if(!bIsLoggedin) {
	var link = document.createElement("link");
	link.setAttribute("rel","stylesheet");
	link.setAttribute("href","https://backend.playzo.de//assets/css/consent.css");
	var head = document.getElementsByTagName("head")[0];
	head.appendChild(link);
	var playzo_div = document.createElement("div");
	playzo_div.innerHTML = '<div id="cookie_consent_handle_playzo" class="consent_handle" style="display:none;"><a href="javascript:openPlayzoConsent();" id="consent_handle"></a></div><div id="cookie_consent_playzo" class="consent" style="display:none;"><h1 id="playzoConsentSettingsHeadline">Cookie-Settings</h1><a href="javascript:savePlayzoConsent(\'essential,analysis,marketing\')" class="btnAcceptGreen" id="playzoAcceptAllBtn">Accept all</a><a href="javascript:savePlayzoConsent(\'essential\')" class="btnGrey" id="playzoOnlyEssentialBtn">Essential only</a><a href="javascript:savePlayzoConsent(\'custom\')" class="btnGrey" id="playzoSaveAndCloseBtn">save and close</a><img id="playzoConsentLogo" src="https://backend.playzo.de//assets/images/logos/logo.png" /><div id="playzoConsentSettings"><ul><li><label for="pzConsentCheckbx_essential"><input type="checkbox" id="pzConsentCheckbx_essential" value="1" disabled checked><strong>Essential</strong><br>Essential Cookies enable us to provide basic features and are mandatory for the page to work properly.</label></li><li><label for="pzConsentCheckbx_analysis"><input type="checkbox" id="pzConsentCheckbx_analysis" value="1"><strong>Analysis</strong><br>Analysis-Cookies enable us to track how the website is being used. We use it to improve the website and our services.</label></li><li><label for="pzConsentCheckbx_marketing"><input type="checkbox" id="pzConsentCheckbx_marketing" value="1"><strong>Marketing (Funding of Website)</strong><br>Marketing-Cookies are being used by thir parties or publishers to provide you with personalized advertisements. This is being performed by tracking visitors across websites.</label></li></ul><div id="consentLinks"><a href="https://playzo.de/de/impressum" target="_blank">Imprint</a> | <a href="https://www.playzo.de/datenschutz" target="_blank">Privacy Policy</a></div></div></div>';
	document.body.appendChild(playzo_div);
	
	if(pzcc == null) {
		document.getElementById('cookie_consent_playzo').style.display='block';
	} else {
		if(pzcc.indexOf('analysis') > 0) document.getElementById('pzConsentCheckbx_analysis').checked='1';
		if(pzcc.indexOf('marketing') > 0) document.getElementById('pzConsentCheckbx_marketing').checked='1';
		document.getElementById('cookie_consent_handle_playzo').style.display='block';
	}
}

function openPlayzoConsent() {
	document.getElementById('cookie_consent_playzo').style.display='block';
	document.getElementById('cookie_consent_handle_playzo').style.display='none';
}
function closePlayzoConsent() {
	document.getElementById('cookie_consent_playzo').style.display='none';
	document.getElementById('cookie_consent_handle_playzo').style.display='block';
}
function savePlayzoConsent(sSettings) {
	var cookieValue = '';
	if(sSettings == 'custom') {
		cookieValue = 'essential,' + (document.getElementById('pzConsentCheckbx_analysis').checked == 1 ? 'analysis,' :'')
			 + (document.getElementById('pzConsentCheckbx_marketing').checked == 1 ? 'marketing,' :'');
	} else {
		cookieValue = sSettings;
	}
	pzConsent_setCookie('playzo_cc', cookieValue, 1200);
	closePlayzoConsent();
	window.location.href = window.location.pathname + window.location.search + window.location.hash;
}
function pzConsent_setCookie(name,value,days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = name + "=" + (value || "")  + expires + "; SameSite=Lax; path=/";
}

