// Element.closest polyfill
if (!Element.prototype.closest) {
    Element.prototype.closest = function(css) {
      var node = this;
    
      while (node) {
        if (node.matches(css)) return node;
        else node = node.parentElement;
      }
      return null;
    };
}
// history back without direct link
document.documentElement.addEventListener('click', function(e) {
    var closePopupBtn = e.target.closest('.t-popup__close');
    if (!!closePopupBtn && closePopupBtn.href && window.history) {
        e.preventDefault();
        window.history.back();
    }
});
// set current url
if (window.location.href.indexOf('tproduct') === -1) {
    setCookie('beforePopupUrl', window.location.href);
}
// utils
function getCookie(name) {
  var matches = document.cookie.match(new RegExp(
    "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
  ));
  return matches ? decodeURIComponent(matches[1]) : undefined;
}
function setCookie(name, value) {
  options = {
    path: '/',
  };
  if (options.expires instanceof Date) {
    options.expires = options.expires.toUTCString();
  }
  var updatedCookie = encodeURIComponent(name) + "=" + encodeURIComponent(value);
  for (var optionKey in options) {
    updatedCookie += "; " + optionKey;
    var optionValue = options[optionKey];
    if (optionValue !== true) {
      updatedCookie += "=" + optionValue;
    }
  }
  document.cookie = updatedCookie;
}