'use strict';

logger.info('md.u:' + md.u);
md.cookiepol = function() {
  function setCookie(key, value) {
    var expires = new Date();
    expires.setTime(expires.getTime() + 365 * 24 * 60 * 60 * 1000);
    document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();
  }
  function getCookie(key) {
    var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
    return keyValue ? keyValue[2] : null;
  }

  if (getCookie('accept') == undefined) {
    $('.cookiewarning').css('display', 'inline-block');
  }
  $('button.acceptcookie').on('click', function() {
    let accord ="ok"
    if ($(this).attr('data-accord')){
       accord = $(this).attr('data-accord')
    }
    setCookie('accept', accord);
    $('.cookiewarning').css('display', 'none');
  });
};
md.tg = function(v) {
  md.cookiepol();
  var l = window.location.href.split('#coo');
  logger.info(l);
  if (l.length == 2) {
    v += l[1];
  }
  var r = localStorage.getItem('r');
  logger.info(r);
  if (r == undefined) {
    r = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
      var r = (Math.random() * 16) | 0,
        v = c == 'x' ? r : (r & 0x3) | 0x8;
      return v.toString(16);
    });
    localStorage.setItem('r', r);
  }
  //$.get(md.u+'/?a=tg&r='+r+v,function(data,status){});
  logger.info(
    md.u + '/?a=tg&r=' + r + '&tit=' + encodeURIComponent(document.title) + v
  );
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {};
  xhttp.open(
    'GET',
    md.u + '/?a=tg&r=' + r + '&tit=' + encodeURIComponent(document.title) + v,
    true
  );
  xhttp.send();
};

$(document).ready(function() {
  md.cookiepol();
  $('[data-tgmd]').on('click', function() {
    md.tg('&s=' + $(this).data('tgmd'));
  });
  $('[data-tgmd]').on('load', function() {
    md.tg('&s=' + $(this).data('tgmd'));
  });
  md.tg('&s=s0');
});
