var CCControl = new L42CC(document.getElementById("l42-cc__body"), "dietzel.at");

CCControl.registerAction(CCControl.ACTIONTYPES.ACTIVATE_STATISTICS, function(){
  if (document.getElementById("ga-script")) return;

  var scriptNode = document.createElement("script");
  scriptNode.setAttribute("id", "ga-script");
  scriptNode.setAttribute("async", "async");
  scriptNode.setAttribute("src", "https://www.googletagmanager.com/gtag/js?id=" + cookieconsent_analyticsid );
  document.head.appendChild(scriptNode);

  try {
    // TODO: eval if error is thrown because gtag should not be available
    // TODO: at this point
    setTimeout( function (){
      gtag("js", new Date());
      gtag("config", cookieconsent_analyticsid , { "anonymize_ip": true }); 
    },1000);
  } catch (e) {
    // noop
  }
});

if (CCControl.cookieStatus.STATISTICS == 'true') {
  CCControl._processActions(CCControl.ACTIONTYPES.ACTIVATE_STATISTICS);
}


function initActionButtons(){
  if (document.cookie.indexOf(CCControl.COOKIES.STATISTICS+"=true") === -1) {
    document.getElementById('accept_all').classList.remove('inactive');
    document.getElementById('decline_all').classList.add('inactive');
  } else {
    document.getElementById('accept_all').classList.add('inactive');
    document.getElementById('decline_all').classList.remove('inactive');
  } 
}

function declineAction(e) {
  CCControl.deleteAllCookies();
  CCControl.setCookie(CCControl.COOKIES.CONFIRMED, "true");
  initActionButtons();
}

function consentAction(e) {
  CCControl.setStatistics(true);
  CCControl.confirmAction();
  initActionButtons();
}

if (document.getElementById('accept_all') && document.getElementById('decline_all')) {
  initActionButtons();
}