document.addEventListener('DOMContentLoaded', function () {
    const content = document.getElementById('content');

    if (!content) {
        return;
    }

    requestAnimationFrame(function () {
        content.classList.add('page-visible');
    });

    document.addEventListener('click', function (event) {
        const link = event.target.closest('a');

        if (!link) {
            return;
        }

        const base = document.querySelector('base[target]');
        const target = link.getAttribute('target') || (base && base.getAttribute('target')) || '';

        if (
            event.defaultPrevented ||
            event.button !== 0 ||
            event.ctrlKey ||
            event.metaKey ||
            event.shiftKey ||
            event.altKey ||
            (target && target.toLowerCase() !== '_self') ||
            link.hasAttribute('download')
        ) {
            return;
        }

        const url = new URL(link.href, window.location.href);

        if (url.origin !== window.location.origin) {
            return;
        }

        // Fragment navigation keeps this document; no load/pageshow follows.
        const href = link.getAttribute('href');
        if (!href || (
            url.pathname === window.location.pathname &&
            url.search === window.location.search &&
            href.indexOf('#') !== -1
        )) {
            return;
        }

        event.preventDefault();

        content.classList.remove('page-visible');
        content.classList.add('page-leaving');

        window.setTimeout(function () {
            window.location.href = url.href;
        }, 111);
    });
});

window.addEventListener('pageshow', function () {
    const content = document.getElementById('content');

    if (!content) {
        return;
    }

    content.classList.remove('page-leaving');

    requestAnimationFrame(function () {
        content.classList.add('page-visible');
    });
});

////////////////////////////////////////////////////////////////////////////////////////////////////

u5cmsswitchtowebappifscreennarrowerthan = 1234;
(function(){if(top!==self)return;function g(n){const v=document.cookie.match('(^|;)\\s*'+n+'\\s*=\\s*([^;]+)');return v?v.pop():""}function s(n,v,d=30){document.cookie=`${n}=${v}; expires=${new Date(Date.now()+d*864e5).toUTCString()}; path=/`}const p=new URLSearchParams(location.search),u=p.get("lowstage")==="on",c=g("lowstage")==="on";if(u){s("lowstage","on")}else if(!c){const h=location.href;/:\/\/([^/]*)(-a\.|-r\.)/.test(h)&&(alert("THIS IS A NON PRODUCTIVE STAGE. THEREFORE YOU ARE DIVERTED TO THE PRODUCTIVE STAGE. TO OVERRULE THIS, ADD THE GET-PARAMETER lowstage=on TO YOUR NON-PROD-URL"),location.href=h.replace(/-a\./g,".").replace(/-r\./g,"."))}})();

u5cmsswitchtowebapp = false;
if(location.href.indexOf('recowithoutcodenzh')<0)if(top===self)if((window.innerWidth < u5cmsswitchtowebappifscreennarrowerthan && document.cookie.indexOf('u5cmsfrcthsvw=desktop') < 0) || window.location.href.indexOf('mobile=on') > 0 || document.cookie.indexOf('u5cmsfrcthsvw=mobile') > -1) u5cmsswitchtowebapp = true;

function isIE2() {
    var ua = window.navigator.userAgent; //Check the userAgent property of the window.navigator object
    var msie = ua.indexOf('MSIE '); // IE 10 or older
    var trident = ua.indexOf('Trident/'); //IE 11

    return (msie > 0 || trident > 0);
}

if(isIE2())u5cmsswitchtowebapp = false;

if (u5cmsswitchtowebapp && document.getElementsByTagName('html')[0]) {
    document.getElementsByTagName('html')[0].style.opacity = 0;
    setTimeout("document.getElementsByTagName('html')[0].style.opacity=1", 777);
}

function u5mkmobile() {
    webappmobilesymbol = '&#128241;';

if(u5cmsswitchtowebapp) {
if(document.getElementsByName('fsearch')[0])document.getElementsByName('fsearch')[0].style.visibility='hidden';
if(document.getElementById('languages')) {
document.getElementById('languages').style.border=0;
document.getElementById('languages').style.marginTop='17px';
document.getElementById('languages').style.float='right';

if(location.href.indexOf('/pages/')<0) {
setTimeout("if(document.getElementById('webapp_monafloat'))if(document.getElementById('webapp_monafloat').innerHTML.indexOf('search')<0)document.getElementById('webapp_monafloat').innerHTML+='<div style=margin-top:11px><a style=margin-left:11px href=?c=_search&q=>&#128269;</a></div>'",1111);
setTimeout("if(document.getElementById('webapp_monafloat'))if(document.getElementById('webapp_monafloat').innerHTML.indexOf('search')<0)document.getElementById('webapp_monafloat').innerHTML+='<div style=margin-top:11px><a style=margin-left:11px href=?c=_search&q=>&#128269;</a></div>'",3333);
setTimeout("if(document.getElementById('webapp_monafloat'))if(document.getElementById('webapp_monafloat').innerHTML.indexOf('search')<0)document.getElementById('webapp_monafloat').innerHTML+='<div style=margin-top:11px><a style=margin-left:11px href=?c=_search&q=>&#128269;</a></div>'",7777);
}
}
}

    if (u5cmsswitchtowebapp) {
        jQuery.loadScript = function(url, callback) {
            jQuery.ajax({
                url: url,
                dataType: 'script',
                success: callback,
                async: true
            });
        }
        if (typeof jsmobilegeneralloaded == 'undefined') jQuery.loadScript('https://info.agov.ch/r/jsmobilegeneral.css', function() {

            //START SITE SPECIFIC CODE TO CREATE A WEB APP. IF YOU CHANGE THE HTML TEMPLATE MAY BE YOU HAVE TO MAKE CHANGES HERE TOO.

            frombtmwebappnavlinkscrollto = 0;
            webappdesktopsymbol = '&#128187;';

            if (document.getElementById('body')) {
            document.getElementById('body').style.marginTop='-20px';
            document.getElementById('body').style.maxWidth='500px';
            document.getElementById('body').style.background='white';
}
            if (document.getElementById('clickhome')) document.getElementById('clickhome').style.width='auto';
            
            if (document.getElementById('webapp_mona')) {
                document.getElementById('webapp_mona').style.margin = '-12px 0 70px 10px';
                document.getElementById('webapp_mona').style.fontSize = '120%';
            }
            if (document.getElementById('webapp_monalink')) {
                document.getElementById('webapp_monalink').style.marginTop = '10px';
            }
            if (document.getElementById('webapp_monalinka')) {
                document.getElementById('webapp_monalinka').style.background = '#fafafa';
                document.getElementById('webapp_monalinka').style.display = 'block';
                document.getElementById('webapp_monalinka').style.padding = '3px';
            }
            if (document.getElementById('webapp_monaclose')) {
                document.getElementById('webapp_monaclose').style.background = '#e63320';
            }
            if (document.getElementById('webapp_monalinkbtm')) {
                document.getElementById('webapp_monalinkbtm').style.padding = '20px 0  20px 0';
            }
            if (document.getElementById('webapp_monalinkabtm')) {
                document.getElementById('webapp_monalinkabtm').style.background = '#fafafa';
                document.getElementById('webapp_monalinkabtm').style.display = 'block';
                document.getElementById('webapp_monalinkabtm').style.padding = '3px';
            }
            if (document.getElementById('navigation')) {
                document.getElementById('navigation').style.display = 'none';
            }            
             if (document.getElementById('main')) {
                document.getElementById('main').style.width = '100%';
                document.getElementById('main').style.boxShadow= 'none';
            }
            if (document.getElementById('header')) {
                document.getElementById('header').style.borderRadius = 0;
                document.getElementById('header').style.boxShadow = 'none';
                document.getElementById('header').style.width = '100%';
                document.getElementById('header').style.height = '100px';
                document.getElementById('header').style.marginTop = '-10px';
                document.getElementById('header').style.lineHeight= '97%';
            }
            if (document.getElementById('bit')) {
                //document.getElementById('bit').style.position = 'absolute';
                document.getElementById('bit').style.marginLeft = '10%';
                //document.getElementById('bit').style.top = '155px';
            }
             if (document.getElementById('vline1')) {
                document.getElementById('vline1').style.display= 'none';
            }
             if (document.getElementById('vline2')) {
                document.getElementById('vline2').style.display= 'none';
            }

            if (document.getElementById('outer')) {
	    document.getElementById('outer').style.margin = 0;
	    document.getElementById('outer').style.padding = 0;
	    document.getElementById('outer').style.width = '100%';
	    document.getElementById('outer').style.boxShadow = 'none';
	    }
                if (document.getElementById('content')) {
                document.getElementById('content').style.minHeight = '100px';
	        document.getElementById('content').style.borderRight = 0;
                document.getElementById('content').style.padding = 0;
                document.getElementById('content').style.margin = 0;
if (document.getElementById('checkboxlang'))document.getElementById('content').style.marginTop='111px';                
document.getElementById('content').style.width = '100%';

                if (!document.getElementById('contentinner')) {
                    var content = document.getElementById('content');
                    var contentinner = document.createElement('div');
                    contentinner.id = 'contentinner';

                    while (content.firstChild) {
                        contentinner.appendChild(content.firstChild);
                    }

                    content.appendChild(contentinner);
                }

                if (document.getElementById('contentinner')) {
                    document.getElementById('contentinner').style.padding = '7px';
                    document.getElementById('content').style.height = document.getElementById('contentinner').style.height;
                }
            }
            if (document.getElementById('news')) {
                document.getElementById('news').style.background='#fafafa';
                document.getElementById('news').style.marginLeft='7%';
                document.getElementById('news').style.padding='0 33px 55px 33px';
                document.getElementById('news').style.width='70%';
               }
            if (document.getElementById('footer')) {
                document.getElementById('footer').style.padding = 0;
                document.getElementById('footer').style.margin = 0;
                document.getElementById('footer').style.width = '100%';
                document.getElementById('footer').style.lineHeight = '130%';
                document.getElementById('footer').innerHTML = '<div id="footerinner">' + document.getElementById('footer').innerHTML + '</div>';
            }
            if (document.getElementById('footerinner')) {
                document.getElementById('footerinner').style.padding = '20px';
            }

            if (document.getElementById('anmelden')) {
                document.getElementById('anmelden').style.display= 'inline-block';                
                document.getElementById('anmelden').style.marginLeft= '22px';                
                }

            if (document.getElementById('registrieren')) {
                document.getElementById('registrieren').style.display= 'inline-block';                
                document.getElementById('registrieren').style.marginLeft= '22px';                
            }

            if (document.getElementById('bit')) {
                document.getElementById('bit').style.position= 'absolute';
                document.getElementById('bit').style.left = '70px';
                document.getElementById('bit').style.top = '20px';
                document.getElementById('bit').style.fontSize= '70%';
document.getElementById('bit').style.marginLeft= '0px';

            }            
	
	    if (document.getElementById('metanavi')) {
                document.getElementById('metanavi').style.float= 'right';
                document.getElementById('metanavi').style.marginTop= '50px';
                document.getElementById('metanavi').style.width= '425px';
                document.getElementById('metanavi').style.padding= '0';

            }
            e=document.getElementById('webapp_mona').getElementsByTagName('ul');
                for(i=0;i<e.length;i++) {
                e[i].style.width=Math.floor(document.getElementById('content').clientWidth*0.8)+'px';
            }

            e=document.getElementById('header').getElementsByTagName('img');
                for(i=0;i<e.length;i++) {
                e[i].style.marginTop='22px';
            }
            
            e=document.getElementsByTagName('form');
                for(i=0;i<e.length;i++) {
                e[i].style.width='95%';
            }

            e=document.getElementsByTagName('dl');
                for(i=0;i<e.length;i++) {
                e[i].style.float='none';
                e[i].style.margin='auto';
            }

if(document.getElementById('emptying')) {
document.getElementById('emptying').style.margin=0;
}

e=document.getElementsByTagName('img')[0];
e.src='/r/agovsquare/agovsquare_de.png';
e.style.height='50px';
e.style.width='50px';
e.style.position='absolute';
e.style.left='0px';


            e=document.getElementsByTagName('img');
                for(i=0;i<e.length;i++) {
                if(e[i].src.indexOf('extern.svg')>0)e[i].style.width='10px';
            }

if(!document.getElementById('cbl_it'))document.getElementById('webapp_mona').innerHTML=document.getElementById('webapp_mona').innerHTML.replace(/background : gray/g,'');
if(document.getElementById('cbl_it'))if(document.getElementById('cbl_it').style.visibility=='hidden')document.getElementById('webapp_mona').innerHTML=document.getElementById('webapp_mona').innerHTML.replace(/background : gray/g,'');
if(document.getElementsByClassName('helpclosediv')[0])document.getElementsByClassName('helpclosediv')[0].style.marginTop='-70px';

            //END SITE SPECIFIC CODE TO CREATE A WEB APP
            if (document.getElementsByTagName('html')[0]) {
                //setTimeout("window.scrollTo(0,93)",1);
                document.getElementsByTagName('html')[0].style.opacity = 1;
            }
        });
    }
    if (document.cookie.indexOf('u5cmsfrcthsvw=desktop') > -1) document.getElementById('footer').innerHTML += ('<div id="webapp_monadesktop" style="position:fixed;top:100px;left:-30px;z-index:9999999;cursor:pointer;font-size:100px" onclick="document.cookie=\'u5cmsfrcthsvw=mobile;\';location.href=location.href.replace(/mobile=off/,\'\')">' + webappmobilesymbol + '</div>');

}

///////////////////////////////////////////////////////

if (u5cmsswitchtowebapp === true) {

  window.addEventListener('load', () => {
    document.querySelectorAll('iframe').forEach(iframe => {
      const src = iframe.getAttribute('src') || '';
      if (src.includes('authuser.php')) {
        iframe.style.marginTop = '22px';
        iframe.style.marginBottom = '22px';
        iframe.style.cssFloat = '';
        iframe.style.display = 'block';
        iframe.style.marginLeft = 'auto';
        iframe.style.marginRight = 'auto';
        iframe.style.transform = 'translateX(44px)';
        iframe.removeAttribute('width');
      }
    });
  });
}

/////////////////////////////////////////////////////// 

if (top !== self) {
    (function () {
        function showContent() {
            var content = document.getElementById('content');

            if (content) {
                content.style.opacity = '1';
                return true;
            }

            return false;
        }

        if (!showContent()) {
            var observer = new MutationObserver(function () {
                if (showContent()) {
                    observer.disconnect();
                }
            });

            observer.observe(document.documentElement, {
                childList: true,
                subtree: true
            });
        }
    })();
}

/*
 * Progressive enhancement for the u5CMS mobile controls.
 * Append this block to the end of jsmobilespecific.css.
 *
 * It does not replace or move existing controls and does not modify their
 * existing onclick handlers. It only adds accessibility attributes,
 * keyboard support and a consistent visual treatment.
 */
(function () {
    'use strict';

    var STYLE_ID = 'u5-mobile-tools-enhancement-style';
    var SVG_NS = 'http://www.w3.org/2000/svg';

    var labels = {
        de: {
            group: 'Seitenwerkzeuge',
            close: 'Navigation schliessen',
            desktop: 'Desktop-Ansicht anzeigen',
            search: 'Website durchsuchen'
        },
        fr: {
            group: 'Outils de la page',
            close: 'Fermer la navigation',
            desktop: 'Afficher la version pour ordinateur',
            search: 'Rechercher sur le site'
        },
        it: {
            group: 'Strumenti della pagina',
            close: 'Chiudi la navigazione',
            desktop: 'Mostra la versione desktop',
            search: 'Cerca nel sito'
        },
        rm: {
            group: 'Utensils da la pagina',
            close: 'Serrar la navigaziun',
            desktop: 'Mussar la versiun desktop',
            search: 'Tschertgar sin la pagina'
        },
        en: {
            group: 'Page tools',
            close: 'Close navigation',
            desktop: 'Show desktop view',
            search: 'Search website'
        }
    };

    function getLanguage() {
        var lang = (document.documentElement.getAttribute('lang') || '').toLowerCase().split('-')[0];

        if (!labels[lang]) {
            var match = location.pathname.toLowerCase().match(/^\/(de|fr|it|rm|en)(?:\/|$)/);
            if (match) lang = match[1];
        }

        return labels[lang] ? lang : 'de';
    }

    function addStyles() {
        if (document.getElementById(STYLE_ID)) return;

        var style = document.createElement('style');
        style.id = STYLE_ID;
        style.textContent = [
            '#webapp_monafloat.u5-mobile-tools {',
            '  width: 50px !important;',
            '  display: flex !important;',
            '  flex-direction: column !important;',
            '  align-items: center !important;',
            '  gap: 8px !important;',
            '}',
            '#webapp_monafloat .u5-mobile-tool-wrap {',
            '  width: 44px !important;',
            '  height: 44px !important;',
            '  margin: 0 !important;',
            '  padding: 0 !important;',
            '}',
            '#webapp_monafloat .u5-mobile-tool {',
            '  box-sizing: border-box !important;',
            '  width: 44px !important;',
            '  height: 44px !important;',
            '  margin: 0 !important;',
            '  padding: 0 !important;',
            '  display: flex !important;',
            '  align-items: center !important;',
            '  justify-content: center !important;',
            '  overflow: visible !important;',
            '  border: 1px solid #d7d7d7 !important;',
            '  border-radius: 9px !important;',
            '  background: #fff !important;',
            '  color: #222 !important;',
            '  box-shadow: 0 1px 3px rgba(0,0,0,.12) !important;',
            '  text-decoration: none !important;',
            '  font-size: 0 !important;',
            '  line-height: 1 !important;',
            '  cursor: pointer !important;',
            '  touch-action: manipulation;',
            '  -webkit-tap-highlight-color: transparent;',
            '}',
            '#webapp_monafloat .u5-mobile-tool:hover {',
            '  background: #f4f4f4 !important;',
            '  border-color: #c8c8c8 !important;',
            '}',
            '#webapp_monafloat .u5-mobile-tool:focus-visible {',
            '  outline: 3px solid #1a73e8 !important;',
            '  outline-offset: 2px !important;',
            '}',
            '#webapp_monafloat #webapp_monaclose.u5-mobile-tool {',
            '  background: #e63320 !important;',
            '  border-color: #e63320 !important;',
            '  color: #fff !important;',
            '  margin-bottom: 0 !important;',
            '}',
            '#webapp_monafloat #webapp_monaclose.u5-mobile-tool:hover {',
            '  background: #c92c1c !important;',
            '  border-color: #c92c1c !important;',
            '}',
            '#webapp_monafloat .u5-mobile-tool svg {',
            '  width: 22px;',
            '  height: 22px;',
            '  display: block;',
            '  pointer-events: none;',
            '}',
            '@media (prefers-reduced-motion: reduce) {',
            '  #webapp_monafloat .u5-mobile-tool {',
            '    transition: none !important;',
            '  }',
            '}'
        ].join('\n');

        (document.head || document.documentElement).appendChild(style);
    }

    function makeSvg(type) {
        var svg = document.createElementNS(SVG_NS, 'svg');
        svg.setAttribute('viewBox', '0 0 24 24');
        svg.setAttribute('aria-hidden', 'true');
        svg.setAttribute('focusable', 'false');
        svg.setAttribute('fill', 'none');
        svg.setAttribute('stroke', 'currentColor');
        svg.setAttribute('stroke-width', '2');
        svg.setAttribute('stroke-linecap', 'round');
        svg.setAttribute('stroke-linejoin', 'round');

        function path(d) {
            var p = document.createElementNS(SVG_NS, 'path');
            p.setAttribute('d', d);
            svg.appendChild(p);
        }

        if (type === 'close') {
            path('M6 6l12 12');
            path('M18 6L6 18');
        } else if (type === 'desktop') {
            var rect = document.createElementNS(SVG_NS, 'rect');
            rect.setAttribute('x', '3');
            rect.setAttribute('y', '4');
            rect.setAttribute('width', '18');
            rect.setAttribute('height', '13');
            rect.setAttribute('rx', '2');
            svg.appendChild(rect);
            path('M8 21h8');
            path('M12 17v4');
        } else if (type === 'search') {
            var circle = document.createElementNS(SVG_NS, 'circle');
            circle.setAttribute('cx', '11');
            circle.setAttribute('cy', '11');
            circle.setAttribute('r', '6.5');
            svg.appendChild(circle);
            path('M16 16l4.5 4.5');
        }

        return svg;
    }

    function setIcon(element, type) {
        if (!element || element.getAttribute('data-u5-mobile-icon') === type) return;

        while (element.firstChild) element.removeChild(element.firstChild);
        element.appendChild(makeSvg(type));
        element.setAttribute('data-u5-mobile-icon', type);
    }

    function enhance() {
        var bar = document.getElementById('webapp_monafloat');
        if (!bar) return;

        addStyles();

        var lang = getLanguage();
        var text = labels[lang];
        var close = bar.querySelector('#webapp_monaclose');
        var desktop = bar.querySelector('#webapp_monadesktop');
        var search = bar.querySelector('a[href*="c=_search"]');

        bar.classList.add('u5-mobile-tools');
        bar.setAttribute('role', 'group');
        bar.setAttribute('aria-label', text.group);

        if (close) {
            close.classList.add('u5-mobile-tool');
            close.setAttribute('role', 'button');
            close.setAttribute('tabindex', '0');
            close.setAttribute('aria-label', text.close);
            close.setAttribute('title', text.close);
            close.setAttribute('aria-controls', 'webapp_mona');
            setIcon(close, 'close');
        }

        if (desktop) {
            desktop.classList.add('u5-mobile-tool');
            desktop.setAttribute('role', 'button');
            desktop.setAttribute('tabindex', '0');
            desktop.setAttribute('aria-label', text.desktop);
            desktop.setAttribute('title', text.desktop);
            setIcon(desktop, 'desktop');
        }

        if (search) {
            search.classList.add('u5-mobile-tool');
            search.setAttribute('aria-label', text.search);
            search.setAttribute('title', text.search);
            setIcon(search, 'search');

            if (search.parentElement && search.parentElement !== bar) {
                search.parentElement.classList.add('u5-mobile-tool-wrap');
            }
        }
    }

    function activateButtonFromKeyboard(event) {
        var target = event.target;

        if (!target || !target.matches('#webapp_monafloat #webapp_monaclose, #webapp_monafloat #webapp_monadesktop')) return;
        if (event.key !== 'Enter' && event.key !== ' ') return;

        event.preventDefault();
        target.click();
    }

    function restoreFocusAfterClose(event) {
        var target = event.target;
        if (!target || !target.closest) return;

        var close = target.closest('#webapp_monafloat #webapp_monaclose');
        if (!close) return;

        window.setTimeout(function () {
            var opener = document.getElementById('webapp_monalinka');
            if (opener && typeof opener.focus === 'function') opener.focus();
        }, 0);
    }

    document.addEventListener('keydown', activateButtonFromKeyboard, false);
    document.addEventListener('click', restoreFocusAfterClose, false);

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', enhance, { once: true });
    } else {
        enhance();
    }

    var observer = new MutationObserver(function (mutations) {
        for (var i = 0; i < mutations.length; i++) {
            if (mutations[i].type === 'childList') {
                enhance();
                break;
            }
        }
    });

    observer.observe(document.documentElement, {
        childList: true,
        subtree: true
    });

    // The original code adds the search control at 1.111, 3.333 or 7.777 seconds.
    // After the initial build phase the observer is no longer needed.
    window.setTimeout(function () {
        observer.disconnect();
        enhance();
    }, 10000);
}());
 