(function () {
  var VERSION = '3';
  var base = (function () {
    var s = document.currentScript;
    return s ? s.src.replace('assets/js/partials.js', '') : '';
  })();

  function get(url) {
    var key = VERSION + ':' + url;
    var cached = sessionStorage.getItem(key);
    if (cached) return Promise.resolve(cached);
    return fetch(url).then(function (r) { return r.text(); }).then(function (html) {
      sessionStorage.setItem(key, html);
      return html;
    });
  }

  function replace(id, html) {
    var el = document.getElementById(id);
    if (!el) return;
    var tmp = document.createElement('div');
    tmp.innerHTML = html.trim();
    el.parentNode.replaceChild(tmp.firstElementChild, el);
  }

  var tasks = [];
  if (document.getElementById('nav-placeholder'))
    tasks.push(get(base + 'assets/html/nav.html').then(function (h) { replace('nav-placeholder', h); }));
  if (document.getElementById('footer-placeholder'))
    tasks.push(get(base + 'assets/html/footer.html').then(function (h) { replace('footer-placeholder', h); }));

  Promise.all(tasks).then(function () {
    if (typeof fitAll === 'function') fitAll();
  });
})();
