


var Base64 = { _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=", encode: function (e) { var t = ""; var n, r, i, s, o, u, a; var f = 0; e = Base64._utf8_encode(e); while (f < e.length) { n = e.charCodeAt(f++); r = e.charCodeAt(f++); i = e.charCodeAt(f++); s = n >> 2; o = (n & 3) << 4 | r >> 4; u = (r & 15) << 2 | i >> 6; a = i & 63; if (isNaN(r)) { u = a = 64 } else if (isNaN(i)) { a = 64 } t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a) } return t }, decode: function (e) { var t = ""; var n, r, i; var s, o, u, a; var f = 0; e = e.replace(/[^A-Za-z0-9\+\/\=]/g, ""); while (f < e.length) { s = this._keyStr.indexOf(e.charAt(f++)); o = this._keyStr.indexOf(e.charAt(f++)); u = this._keyStr.indexOf(e.charAt(f++)); a = this._keyStr.indexOf(e.charAt(f++)); n = s << 2 | o >> 4; r = (o & 15) << 4 | u >> 2; i = (u & 3) << 6 | a; t = t + String.fromCharCode(n); if (u != 64) { t = t + String.fromCharCode(r) } if (a != 64) { t = t + String.fromCharCode(i) } } t = Base64._utf8_decode(t); return t }, _utf8_encode: function (e) { e = e.replace(/\r\n/g, "\n"); var t = ""; for (var n = 0; n < e.length; n++) { var r = e.charCodeAt(n); if (r < 128) { t += String.fromCharCode(r) } else if (r > 127 && r < 2048) { t += String.fromCharCode(r >> 6 | 192); t += String.fromCharCode(r & 63 | 128) } else { t += String.fromCharCode(r >> 12 | 224); t += String.fromCharCode(r >> 6 & 63 | 128); t += String.fromCharCode(r & 63 | 128) } } return t }, _utf8_decode: function (e) { var t = ""; var n = 0; var r = c1 = c2 = 0; while (n < e.length) { r = e.charCodeAt(n); if (r < 128) { t += String.fromCharCode(r); n++ } else if (r > 191 && r < 224) { c2 = e.charCodeAt(n + 1); t += String.fromCharCode((r & 31) << 6 | c2 & 63); n += 2 } else { c2 = e.charCodeAt(n + 1); c3 = e.charCodeAt(n + 2); t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63); n += 3 } } return t } };
String.prototype.replaceAll = function(search, replacement) {
    var target = this;
    return target.split(search).join(replacement);
};

if (!window.config) {
    window.config = {};
}
if (!config.initURL) {
    config.initURL = "/Home/KursListe";
}
if (!config.width) {
    config.width = "100%";
}
if (!config.maxHeight) {
    config.maxHeight = function() {
        return window.innerHeight;
    };
}
if (config.shrinkOnUnload === undefined) {
    config.shrinkOnUnload = true;
}
if (config.breakpointKalendar === undefined) {
    config.breakpointKalendar = 380;
}
if (config.ignoreHash === undefined) {
    config.ignoreHash = false;
}

function setIframeHeightCO(id, ht) {
    var ifrm = document.getElementById(id);
    if (ifrm !== null) {
        ifrm.style.height = Math.max(100, ht) + "px";
    }    
}

function loadCurrentSite() {
    businessGoTo(decodeURIComponent(window.location.hash).replace(/#/g, ""));
}

function businessGoTo(path) {
    pluginDontScroll = true;
    if (path.indexOf("/Home/") === -1) {
        path = config.initURL;
    }
    (document.getElementById('aktivKONZEPTE_Plugin_Frame') || {}).src = baseURL + "/LANG-" + configNav.lang + path;
    var frame = document.getElementById('aktivKONZEPTE_Plugin_Frame');
    if (frame !== null) frame.contentWindow.postMessage(JSON.stringify({ isReloading: true }), '*');
}

function popStateHandler(event) {
    loadCurrentSite();
}

function initPlugin(callback) {
    if (!config.show) {
        config.show = [ ];
    }

    var old = document.querySelector('#aktivKONZEPTE_Plugin');
    if (old !== null && old.parentNode != null) {
        old.parentNode.removeChild(old);
    }
    old = document.querySelector('#aktivKONZEPTE_Plugin_Frame');
    if (old !== null && old.parentNode != null) {
        old.parentNode.removeChild(old);
    }
    var me = document.querySelector('.ui-page-active script[src$="/Home/Plugin"]');
    if (me === null) {
        me = document.querySelector('script[src$="/Home/Plugin"]');
    }

    config.baseURL = me.getAttribute("src").substr(0, me.getAttribute("src").indexOf("/Home/Plugin"));
    config.baseHOST = config.baseURL.replace(/^(.*\/\/[^\/?#]*).*$/,"$1");
    var meDiv = document.createElement('div');
    meDiv.id = "aktivKONZEPTE_Plugin";
    me.parentNode.insertBefore(meDiv, me);

    var ie = detectIE();
    if (ie !== false && ie < 11) {
        meDiv.innerHTML = configNav.browserfail;
        return;
    }

    meDiv.innerHTML = '<iframe id="aktivKONZEPTE_Plugin_Frame" scrolling="no" src="" border="0" style="border: 0px solid black; height: 200px; min-width: ' + config.width + '; max-width: ' + config.width + '; width: 1px; display: block;"></iframe>';
    var fontSize = window.getComputedStyle(meDiv,null).getPropertyValue("font-size");
    var fontFamily = window.getComputedStyle(meDiv,null).getPropertyValue("font-family").replace(/"/g, "'");
    var fontColor = window.getComputedStyle(meDiv,null).getPropertyValue("color");
    var frame = document.getElementById('aktivKONZEPTE_Plugin_Frame');
    var base64img = Base64.encode('<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" style="font-family: ' 
                    + fontFamily.replaceAll('" ', "' ") + '; font-size: ' + fontSize + '; fill: ' + fontColor + ';"><text x="50%" y="50%" text-anchor="middle">'
                    + configNav.bittewarten + '</text><text x="50%" y="50%" text-anchor="middle" dy="1em">' + configNav.datenwerdengeladen
                    + '</text></svg>').replaceAll("-", "+").replaceAll("_", "/");
                frame.style.background = "url('data:image/svg+xml;charset=utf-8;base64," + base64img + "')";

    function runOnce(e) {
        if (typeof(callback) === "function") {
            if (e.data === '{ "ready": true }') {
                callback();
                window.removeEventListener('message', runOnce);
            }
        } else {
            window.removeEventListener('message', runOnce);
        }
    }    
    window.removeEventListener('message', runOnce);
    window.addEventListener('message', runOnce, false);

    var css = document.getElementById('aktivKONZEPTE_Plugin_CSS');
    if (css !== null) {
        if (css.getAttribute("src") !== null) {
            config.CSSurl = absolutePath(css.getAttribute("src"));
        }
        config.CSS = css.textContent;
    }

    var baseURL = config.baseURL;

    window.removeEventListener('popstate', popStateHandler);
    window.addEventListener('popstate', popStateHandler, false);

    loadCurrentSite();

    window.removeEventListener("orientationchange", onPluginResize);
    window.addEventListener("orientationchange", onPluginResize, false);
    window.removeEventListener("resize", onPluginResize);
    window.addEventListener("resize", onPluginResize, false);

    function destroyPlugin() {
        window.removeEventListener("orientationchange", onPluginResize);
        window.removeEventListener("resize", onPluginResize);
        window.removeEventListener('popstate', popStateHandler);
        window.removeEventListener('message', runOnce);
    }
}

function onPluginResize() {
    var height = config.maxHeight();
    var frame = document.getElementById('aktivKONZEPTE_Plugin_Frame');
    if (frame !== null) frame.contentWindow.postMessage(JSON.stringify({ height: height }), '*');
}

if (window.navLoaded === true) {
    initPlugin();
}

function destroyOnlinePoint() {
    if (typeof(window.destroyPluginNavigation) === "function") window.destroyPluginNavigation();
    if (typeof(window.destroyPlugin) === "function") window.destroyPlugin();
}