/* Plain JavaScript mobile navigation helper for the legacy PCR site. */
(function () {
  function ready(fn) {
    if (document.readyState !== 'loading') {
      fn();
    } else {
      document.addEventListener('DOMContentLoaded', fn);
    }
  }

  ready(function () {
    var nav = document.getElementById('navigation');
    if (!nav) return;

    document.body.className += (document.body.className ? ' ' : '') + 'mobile-js';

    if (!document.getElementById('mobile-menu-toggle')) {
      var button = document.createElement('button');
      button.id = 'mobile-menu-toggle';
      button.type = 'button';
      button.setAttribute('aria-controls', 'navigation');
      button.setAttribute('aria-expanded', 'false');
      button.appendChild(document.createTextNode('Menu'));
      nav.insertBefore(button, nav.firstChild);
    }

    var toggle = document.getElementById('mobile-menu-toggle');
    toggle.onclick = function () {
      var isOpen = nav.className.indexOf('is-open') !== -1;
      if (isOpen) {
        nav.className = nav.className.replace(/\bis-open\b/g, '').replace(/\s+/g, ' ');
        toggle.setAttribute('aria-expanded', 'false');
      } else {
        nav.className += ' is-open';
        toggle.setAttribute('aria-expanded', 'true');
      }
    };
  });
}());
