/**
 * Gallery lightbox: click image → overlay with prev/next.
 * Initializes after DOM ready so #nzp-igl-lb exists even if printed before this script in the footer.
 */
(function () {
	'use strict';

	function initLightbox() {
		var lb = document.getElementById('nzp-igl-lb');
		if (!lb) {
			return;
		}

		var imgEl = lb.querySelector('.nzp-igl-lb__img');
		var capEl = lb.querySelector('.nzp-igl-lb__cap');
		var titleEl = lb.querySelector('.nzp-igl-lb__title');
		var triggers = [];
		var index = 0;

		function getRoot(el) {
			return el && el.closest ? el.closest('.nzp-igl') : null;
		}

		function collect(root) {
			return Array.prototype.slice.call(root.querySelectorAll('.nzp-igl__lb-trigger'));
		}

		function show() {
			var tr = triggers[index];
			if (!tr || !imgEl) {
				return;
			}
			var src = tr.getAttribute('data-lb-src') || '';
			var alt = tr.getAttribute('data-lb-alt') || '';
			var cap = tr.getAttribute('data-lb-cap') || '';
			var tit = tr.getAttribute('data-lb-title') || '';
			imgEl.src = src;
			imgEl.alt = alt;
			if (capEl) {
				capEl.textContent = cap;
			}
			if (titleEl) {
				titleEl.textContent = tit;
			}
			var prev = lb.querySelector('.nzp-igl-lb__prev');
			var next = lb.querySelector('.nzp-igl-lb__next');
			if (prev) {
				prev.disabled = triggers.length <= 1;
			}
			if (next) {
				next.disabled = triggers.length <= 1;
			}
		}

		function open(trigger) {
			var root = getRoot(trigger);
			if (!root) {
				return;
			}
			triggers = collect(root);
			index = triggers.indexOf(trigger);
			if (index < 0) {
				index = 0;
			}
			show();
			lb.hidden = false;
			lb.setAttribute('aria-hidden', 'false');
			document.body.classList.add('nzp-igl-lb-open');
			document.documentElement.classList.add('nzp-igl-lb-open');
		}

		function close() {
			lb.hidden = true;
			lb.setAttribute('aria-hidden', 'true');
			document.body.classList.remove('nzp-igl-lb-open');
			document.documentElement.classList.remove('nzp-igl-lb-open');
			if (imgEl) {
				imgEl.removeAttribute('src');
			}
		}

		function step(delta) {
			if (triggers.length === 0) {
				return;
			}
			index = (index + delta + triggers.length) % triggers.length;
			show();
		}

		document.addEventListener('click', function (e) {
			var t = e.target.closest('.nzp-igl__lb-trigger');
			if (t) {
				e.preventDefault();
				e.stopPropagation();
				open(t);
				return;
			}
			if (e.target.closest('.nzp-igl-lb__close') || e.target.classList.contains('nzp-igl-lb__backdrop')) {
				close();
			}
		});

		document.addEventListener('keydown', function (e) {
			if (lb.hidden) {
				return;
			}
			if (e.key === 'Escape') {
				close();
			} else if (e.key === 'ArrowLeft') {
				step(-1);
			} else if (e.key === 'ArrowRight') {
				step(1);
			}
		});

		lb.addEventListener('click', function (e) {
			if (e.target.closest('.nzp-igl-lb__prev')) {
				e.preventDefault();
				step(-1);
			} else if (e.target.closest('.nzp-igl-lb__next')) {
				e.preventDefault();
				step(1);
			}
		});
	}

	if (document.readyState === 'loading') {
		document.addEventListener('DOMContentLoaded', initLightbox);
	} else {
		initLightbox();
	}
})();
