﻿
// load a js file on the fly
function loadJSFile(scriptPath, callback) {
    var scriptNode = document.createElement('script');
    scriptNode.type = 'text/javascript';
    scriptNode.src = scriptPath;

    if (callback != null) {
        scriptNode.onreadystatechange = function () {
            if (!scriptNode.callbackHasRun)
                if(this.readyState == "loaded" || this.readyState == "complete") {
                    scriptNode.callbackHasRun = true;
                    callback();
                }
        };
        scriptNode.onload = function () {
            if (!scriptNode.callbackHasRun) {
                scriptNode.callbackHasRun = true;
                callback();
            }
        };
    }
    
    var headNode = document.getElementsByTagName('HEAD');
    if (headNode[0] != null)
        headNode[0].appendChild(scriptNode);
}

// loads a css file on the fly
function loadCSSFile(filePath) {
    var fileref=document.createElement("link")
    fileref.setAttribute("rel", "stylesheet")
    fileref.setAttribute("type", "text/css")
    fileref.setAttribute("href", filePath)

    var headNode = document.getElementsByTagName('HEAD');
    if (headNode[0] != null)
        headNode[0].appendChild(fileref);
}

// load the icms
function icmsLoad(contentsCss,bundleJs) {

    if (this.state)
        return; // already loaded

    this.state = "loading";
    document.getElementById("icmsLoader").style.display = 'table';

    loadCSSFile("/icms/icms.css");
    loadJSFile("/icms/jquery-1.6.2.min.js", function () {
        loadJSFile(bundleJs, function () {
            $(function () { $.fn.icms.contentsCss = contentsCss });
            this.state = "loaded";
        });
        loadJSFile("/ckeditor/ckeditor.js", function () {
            loadJSFile("/ckeditor/adapters/jquery.js");
        });
    });
}


 
