<?xml version="1.0" encoding="UTF-8"?>
<krpano>
    <events name="mobile_autofit_events" keep="true"
            onxmlcomplete="apply_mobile_autofit();"
            onresize="apply_mobile_autofit();" />

    <action name="apply_mobile_autofit">
        if(device.mobile,
            set(stagescale, 1.0);
        );
        injectKolorMenuMobileScroll();
    </action>

    <action name="injectKolorMenuMobileScroll" type="Javascript"><![CDATA[
        if (!document.getElementById('kolormenu-mobile-scroll-css')) {
            var style = document.createElement('style');
            style.id = 'kolormenu-mobile-scroll-css';
            style.innerHTML =
                '@media only screen and (max-width: 1024px) {' +
                '  #panoramaMenu, [id^="panoramaMenu"], .kolorMenu {' +
                '    max-height: 70vh !important;' +
                '    height: auto !important;' +
                '    overflow-y: auto !important;' +
                '    overflow-x: hidden !important;' +
                '    -webkit-overflow-scrolling: touch !important;' +
                '    touch-action: pan-y !important;' +
                '  }' +
                '  #panoramaMenu ul, [id^="panoramaMenu"] ul, .kolorMenu ul {' +
                '    max-height: 70vh !important;' +
                '    height: auto !important;' +
                '    overflow-y: auto !important;' +
                '    touch-action: pan-y !important;' +
                '  }' +
                '}';
            document.head.appendChild(style);
        }

        var attempts = 0;
        var bindScrollInterval = setInterval(function() {
            attempts++;
            var menuEl = document.getElementById('panoramaMenu') || document.querySelector('[id^="panoramaMenu"], .kolorMenu');
            if (menuEl) {
                if (!menuEl.dataset.scrollBound) {
                    menuEl.dataset.scrollBound = 'true';
                    menuEl.addEventListener('touchstart', function(e) { e.stopPropagation(); }, { passive: true });
                    menuEl.addEventListener('touchmove', function(e) { e.stopPropagation(); }, { passive: true });
                }
                clearInterval(bindScrollInterval);
            } else if (attempts >= 25) {
                clearInterval(bindScrollInterval);
            }
        }, 200);
    ]]></action>
</krpano>
