/** Loader for the MyUI library. */

var MYUI_JS = "myui.js";
var MYUI_MODULES = { 
    "autosuggest" : ["core", "autosuggest.js", "autosuggest.css"],
    "cloud" : ["core", "cloud.js"],
    "core" : ["util.js"], 
    "date" : ["date.js"],
    "daySelector" : ["core", "date", "daySelector.js", "daySelector.css"],
    "effects" : ["core", "effect.js"],
    "frames" : ["core", "frame.js", "frame.css"],
    "gestures" : ["core", "gesture.js", "gesture.css"],
    "log4js" : ["log4js.js"],
    "popup" : ["core", "popup.js"],
    "progressbar" : ["core", "progressbar.js", "progressbar.css"],
    "slideshow" : ["core", "effects", "slideshow.js"],
    "tabletree" : ["core", "tabletree.js", "tabletree.css"],
    "validation" : ["core", "date", "fields.js", "fields.css"] 
    };
    
document.insertInHeadOrAfter = function(newChild, refChild) {
    if (refChild == null)
        this.getElementsByTagName("HEAD").item(0).appendChild(newChild);
    else 
        if (refChild.nextSibling != null)
            refChild.parentNode.insertBefore(newChild, refChild.nextSibling);
        else
            refChild.parentNode.appendChild(newChild);    
}

/** Adds the given script to the window containing the document to be reviewed */
document.createScript = function(scriptPath, language) {
    var s = this.createElement("SCRIPT");
    if (language == null)
        language = "javascript";
    s.setAttribute("language", language);
    s.setAttribute("src", scriptPath);
	  //s.setAttribute("defer", "false");
}

function MyUI_loadResource(path, resource, afterElem) {
    if (document.myuiLoaded == null) {
        document.myuiLoaded = new Object();
    }
    if (document.myuiLoaded[resource] != true) {
        if (resource.indexOf(".js") == resource.length - 3) {
            // load script
            try {
                document.write("<script src='" + path + resource + "'></script>");
            } catch (exc) {
                document.insertInHeadOrAfter(document.createScript(path + resource), afterElem);
            }            
        } else if (resource.indexOf(".css") == resource.length - 4) {
            // load stylesheet
            try {
                document.write("<link rel='stylesheet' type='text/css' href='" + path + resource + "'/>");
            } catch (exc) {
                document.insertInHeadOrAfter(dcoument.createStyleSheet(path + resource), afterElem);
            }
        } else {
            for (var i = 0; i < MYUI_MODULES[resource].length; i++) {
                MyUI_loadResource(path, MYUI_MODULES[resource][i], afterElem);
            }
        }
        document.myuiLoaded[resource] = true;
    }
}


function MyUI_loadModules(path, modules, afterElem) {
    if (modules == null) {
        for (var m in MYUI_MODULES) {
            MyUI_loadResource(path, m, afterElem);
        }
    } else {
        var mlist = modules.split(",");
        for (var i = 0; i < mlist.length; i++) {
            MyUI_loadResource(path, mlist[i].replace( /\s*$/, "" ).replace( /^\s*/, "" ), afterElem);
        }
    }
}

function MyUI_load() {
    

    var scripts = document.getElementsByTagName("SCRIPT");
    var x = MYUI_JS.length;
    for (var i = 0, n = scripts.length; i < n; i++) {
        var index = scripts[i].src.lastIndexOf(MYUI_JS);
        if (index == scripts[i].src.length - x 
                || (scripts[i].src.length >= (index + x - 1) && scripts[i].src.charAt(index + x) == "?")) {
            var myuiPath = scripts[i].src.substring(0, index);
            var modules = null;
            if (scripts[i].src.length >= (index + x - 1) && scripts[i].src.charAt(index + x) == "?")
                modules = scripts[i].src.substring(index + x + 9);
            
            // load the behaviors
            if (document.all) {
                try {
                    document.write("<!--[if lte IE 7]><style>* { behavior: url(" + myuiPath 
                            + "cb_proto.htc) url(" + myuiPath + "cb_prop.htc);}</style><![endif]-->"
                            + "<!--[if gte IE 8]><style>* { behavior: url(" + myuiPath 
                            + "cb_prop_ie8.htc);}</style><![endif]-->"
                            );
                } catch (exc) {
                    var styleElem = document.createElement("STYLE");
                    styleElem.innerHTML = "* { behavior: url(" + myuiPath 
                            + "cb_prop.htc) url(" + myuiPath + "cb_proto.htc);}";
                    document.insertInHeadOrAfter(styleElem, scripts[i]);
                }
            }
            
            // load the modules
            MyUI_loadModules(myuiPath, modules, scripts[i]);
        }
    }
}

MyUI_load();

