	(function() {
		// Config (AJAX endpoint + nonce for the contact-tap beacon) is
		// attached before this file by paintops_render_app_nav().
		var cfg = window.paintopsAppNavCfg || {};

		// Close the "More" dropdown on an outside click, since <details>
		// only closes natively when its own summary is clicked again.
		document.addEventListener('click', function(e) {
			document.querySelectorAll('.pab-more[open]').forEach(function(el) {
				if (!el.contains(e.target)) {
					el.removeAttribute('open');
				}
			});
		});

		// Customer-contact tap logging (Mike 2026-07-23): ANY element on
		// an app page carrying data-po-comm (call|text|email) +
		// data-po-job beacons a communications-log entry as it's tapped.
		// Capture phase + sendBeacon so the tel:/sms:/mailto: navigation
		// is never delayed or blocked; see paintops_ajax_log_contact.
		document.addEventListener('click', function(e) {
			var commEl = e.target && e.target.closest ? e.target.closest('[data-po-comm]') : null;
			if (!commEl) { return; }
			try {
				var fd = new FormData();
				fd.append('action', 'paintops_log_contact');
				fd.append('nonce', cfg.logContactNonce || '');
				fd.append('job_id', commEl.getAttribute('data-po-job') || '0');
				fd.append('contact_type', commEl.getAttribute('data-po-comm') || '');
				fd.append('recipient', commEl.getAttribute('data-po-recipient') || '');
				fd.append('src', commEl.getAttribute('data-po-src') || '');
				navigator.sendBeacon(cfg.ajaxUrl || '', fd);
			} catch (err) {}
		}, true);
	})();

		(function() {
			var bar = document.querySelector('.paintops-bottomnav');
			if (!bar) { return; }
			// The bar is always shown. On form screens the fixed Save bar is
			// lifted to sit ABOVE it (see .paintops-sticky-save in
			// job-style.php), so the two stack instead of overlapping.
			document.body.classList.add('paintops-nav-bottom-active');
			var moreBtn  = bar.querySelector('.pbn-more-btn');
			var sheet    = document.querySelector('.pbn-sheet');
			var backdrop = document.querySelector('.pbn-sheet-backdrop');
			function openSheet() {
				if (sheet) { sheet.hidden = false; }
				if (backdrop) { backdrop.hidden = false; }
				if (moreBtn) { moreBtn.setAttribute('aria-expanded', 'true'); }
			}
			function closeSheet() {
				if (sheet) { sheet.hidden = true; }
				if (backdrop) { backdrop.hidden = true; }
				if (moreBtn) { moreBtn.setAttribute('aria-expanded', 'false'); }
			}
			if (moreBtn) {
				moreBtn.addEventListener('click', function() {
					if (sheet && sheet.hidden === false) { closeSheet(); } else { openSheet(); }
				});
			}
			if (backdrop) { backdrop.addEventListener('click', closeSheet); }
			document.addEventListener('keydown', function(e) { if (e.key === 'Escape') { closeSheet(); } });
		})();

		(function() {
			// Keep the app "jailed" (Mike 2026-07-23): any link that leaves
			// the PaintOps app page, another site entirely, or this site's
			// own theme header/footer/menu pages, opens in a NEW tab, so
			// the app (especially the installed home-screen version) never
			// drifts away from itself. In-app links are same-origin links
			// back to this same page path (the app routes by query string).
			// tel:/sms:/mailto:, hash toggles, and links that already set
			// their own target are left alone.
			var paintopsAppPath = window.location.pathname.replace(/\/+$/, '');
			document.addEventListener('click', function(e) {
				var a = e.target && e.target.closest ? e.target.closest('a[href]') : null;
				if (!a) { return; }
				var rawHref = a.getAttribute('href') || '';
				if (rawHref === '' || rawHref.charAt(0) === '#') { return; }
				if (a.target && a.target !== '' && a.target !== '_self') { return; }
				var url;
				try { url = new URL(a.href, window.location.href); } catch (err) { return; }
				if (url.protocol !== 'http:' && url.protocol !== 'https:') { return; }
				if (url.origin === window.location.origin && url.pathname.replace(/\/+$/, '') === paintopsAppPath) { return; }
				a.target = '_blank';
				a.rel = (a.rel ? a.rel + ' ' : '') + 'noopener';
			}, true);

			// Double-submit guard (Mike 2026-07-23): once a form submits,
			// further submits of the same form are swallowed and its submit
			// buttons disable, so a double-tap can't post twice. Buttons
			// disable on a 0ms timeout so the CLICKED button's name/value
			// still serializes into the POST (handlers key on it). Re-arms
			// after 10s in case client-side validation blocked navigation.
			document.addEventListener('submit', function(e) {
				var f = e.target;
				if (!f || f.tagName !== 'FORM') { return; }
				if (f.getAttribute('data-paintops-submitted') === '1') { e.preventDefault(); return; }
				f.setAttribute('data-paintops-submitted', '1');
				setTimeout(function() {
					var btns = f.querySelectorAll('button[type="submit"], input[type="submit"], button:not([type])');
					for (var i = 0; i < btns.length; i++) { btns[i].disabled = true; }
					setTimeout(function() {
						f.removeAttribute('data-paintops-submitted');
						for (var j = 0; j < btns.length; j++) { btns[j].disabled = false; }
					}, 10000);
				}, 0);
			}, true);
		})();
