/**
 * ITSWEBER CMS – Header Search Toggle (v3.3.0)
 * Handles the search icon dropdown in the site header.
 */
(function () {
    'use strict';

    var toggle = document.querySelector('.header-search-toggle');
    var dropdown = document.querySelector('.header-search-dropdown');
    if (!toggle || !dropdown) return;

    toggle.addEventListener('click', function (e) {
        e.stopPropagation();
        var isOpen = !dropdown.hidden;
        dropdown.hidden = isOpen;
        toggle.setAttribute('aria-expanded', String(!isOpen));
        if (!isOpen) {
            var input = dropdown.querySelector('input[type="search"]');
            if (input) input.focus();
        }
    });

    // Close on click outside
    document.addEventListener('click', function (e) {
        if (!dropdown.hidden && !dropdown.contains(e.target) && e.target !== toggle) {
            dropdown.hidden = true;
            toggle.setAttribute('aria-expanded', 'false');
        }
    });

    // Close on Escape
    document.addEventListener('keydown', function (e) {
        if (e.key === 'Escape' && !dropdown.hidden) {
            dropdown.hidden = true;
            toggle.setAttribute('aria-expanded', 'false');
            toggle.focus();
        }
    });
})();
