(function(){
    
var reboot = function(){
    if(window.ggbRebooted){
        return;
    }
    window.ggbRebooted=true;
    var applets=document.getElementsByTagName("applet");
    for (var i = applets.length-1;i>=0;i--) {
        var applet = applets[i];
        function attr(a) {
            return applet.getAttribute && applet.getAttribute(a);
        }

        if (attr("code").match(/geogebra/)) {
            var container = document.createElement("div");
            var article = document.createElement("article");
            article.className = "geogebraweb";
            for (var k =0; k<2; k++) {
                var attrName = ['width','height'][k];
                var cssName = ['minWidth','minHeight'][k];
                var attrVal = parseInt(attr(attrName));
                if(parseInt(attr(attrName)) > 0){
                    article.setAttribute('data-param-'+attrName, attrVal);
                    container.style[cssName] = attrVal+"px";
                }
            }
            var params = applet.getElementsByTagName("param");
            function normalize(fn) {
	        var path = location.protocol + "//"+location.host+location.pathname;
                var val = path.substring(0,path.lastIndexOf('/')+1)+fn;
                window.console && console.log(val);
                return val;
            }
            if (attr("fileName")) {
                article.setAttribute('data-param-filename', normalize(attr("fileName")));
            }
            for (j in params) {
                var val= params[j].value;
                if (params[j].name && params[j].name.toLowerCase() == "filename") {
                    val = normalize(val);
                }
                article.setAttribute('data-param-'+params[j].name, val);
            }
            container.appendChild(article);
            applet.parentNode.replaceChild(container, applet);
        }
    }
    if (window.jQuery) {
        window.jQuery.support = window.jQuery.support || {};
    }
    var script = document.createElement("script");
    script.src = "https://cdn.geogebra.org/apps/latest/web3d/web3d.nocache.js";
    document.head.appendChild(script);
}

if (document.readyState === "complete" || document.readyState === "interactive") {
    reboot();
}
else if (window.addEventListener) {
    window.addEventListener('load', reboot, false);
} else {
    window.attachEvent('onload', reboot);
}

})();
