/**
 * Theme Name:     Twenty Fourteen Child
 * Author:         the WordPress team
 * Template:       twentyfourteen
 * Text Domain:	   twenty-fourteen-child
 * Description:    In 2014, our default theme lets you create a responsive magazine website with a sleek, modern design. Feature your favorite homepage content in either a grid or a slider. Use the three widget areas to customize your website, and change your content\&#039;s layout with a full-width page template and a contributor page to show off your authors. Creating a magazine website with WordPress has never been easier.
 */

add_action('wp_footer', function () {
    ?>
    <script>
    document.addEventListener('DOMContentLoaded', function () {
        // 👉 Сохраняем вкладку
        document.querySelectorAll('.kt-tab-title').forEach(tab => {
            tab.addEventListener('click', () => {
                const tabId = tab.getAttribute('href');
                if (tabId) {
                    sessionStorage.setItem('activeTab', tabId);
                }
            });
        });

        // 👉 Сохраняем аккордеон
        document.querySelectorAll('.kt-blocks-accordion-header').forEach(btn => {
            btn.addEventListener('click', () => {
                const panelId = btn.getAttribute('aria-controls');
                if (panelId) {
                    sessionStorage.setItem('activeAccordionPanel', panelId);
                }
            });
        });

        // ⏪ Восстановление вкладки
        const savedTabId = sessionStorage.getItem('activeTab');
        if (savedTabId) {
            const tabLink = document.querySelector('.kt-tab-title[href="' + savedTabId + '"]');
            if (tabLink) {
                tabLink.click();
            }
        }

        // 🔁 Проверка и восстановление аккордеона (отложенная)
        function restoreAccordionByPanelId(panelId, attempts = 10) {
            if (!panelId || attempts <= 0) return;

            const btn = document.querySelector('[aria-controls="' + panelId + '"]');
            const panel = document.getElementById(panelId);
            if (btn && panel && !panel.classList.contains('kt-accordion-panel-active')) {
                btn.click();
                return;
            }

            setTimeout(() => restoreAccordionByPanelId(panelId, attempts - 1), 300);
        }

        const savedAccordion = sessionStorage.getItem('activeAccordionPanel');
        if (savedAccordion) {
            restoreAccordionByPanelId(savedAccordion);
        }

        // 🔗 Если есть якорь — пробуем открыть аккордеон
        const hash = window.location.hash;
        if (hash.startsWith('#acc')) {
            const tryAccordionByAnchor = (attempts = 10) => {
                const anchor = document.querySelector(hash);
                if (!anchor || attempts <= 0) return;

                const btn = anchor.querySelector('.kt-blocks-accordion-header');
                if (btn) {
                    btn.click();
                    return;
                }
                setTimeout(() => tryAccordionByAnchor(attempts - 1), 300);
            };

            tryAccordionByAnchor();
        }
    });
    </script>
    <?php
}, 100);



body:not(.logged-in) .only-logged {
    display: none !important;
}






