(function() {
  'use strict';
  if ( typeof CookieJarData === 'undefined' ) return;

  var consentCookie = CookieJarData.consentCookie || 'cookiejar_consent';

  if ( document.cookie.indexOf(consentCookie + '=1') !== -1 ) {
    return;
  }

  function setConsent() {
    var settings = CookieJarData.settings || {};
    var days = settings.expiration_days || 180;
    var date = new Date();
    date.setTime(date.getTime() + (days*24*60*60*1000));
    document.cookie = consentCookie + '=1; expires=' + date.toUTCString() + '; path=/; SameSite=Lax';
    var banner = document.getElementById('cookiejar-banner');
    if ( banner && banner.parentNode ) {
      banner.parentNode.removeChild(banner);
    }
    var ev;
    try {
      ev = new CustomEvent('cookiejarConsentGranted');
    } catch(e) {
      ev = document.createEvent('Event');
      ev.initEvent('cookiejarConsentGranted', true, true);
    }
    document.dispatchEvent(ev);
  }

  document.addEventListener('click', function(e) {
    if ( e.target && e.target.classList && e.target.classList.contains('cookiejar-accept') ) {
      setConsent();
    }
  });
})();