/**
 * 1. Add to head:
 *      <script type="text/javascript"
 *          src="include.js?somepackage.SomeModule1,somepackage.SomeModule2">
 *      </script>
 * 2. And anywhere in your js files:
 *      include("somepackage.SomeOtherModule");
 *
 * Modules are loaded from [package]/[subpackage]/[ModuleName].js files.
 * http://modularjs.googlecode.com
 */
var MODULARJS_SET_GLOBAL_VAR_NAME = "__modularjs_global_test__";
var MODULARJS_SET_GLOBAL_VAR = "var " + MODULARJS_SET_GLOBAL_VAR_NAME +  " = true;";
var modularjs = {
    basePath: null,
    loaded: {},
    loading: {},	
    init: function() {	
		modularjs.xhr = 	(typeof XMLHttpRequest != "undefined")	? new XMLHttpRequest() 
					:	(typeof ActiveXObject != "undefined")	? new ActiveXObject("Microsoft.XMLHTTP")
					:	false ;
        modularjs.head = document.getElementsByTagName("head")[0];
        var scripts = modularjs.head.getElementsByTagName("script");
        modularjs.setEvalFunction();
        for ( var i = 0; i < scripts.length; i++) {
            var src = scripts[i].src;
            if (src.match(/.*include\.js.*/)) {
                var parts = src.split(/\?/);
                modularjs.basePath = parts[0].replace(/include\.js.*/, '');
                if (parts.length > 1) {
                    parts = parts[1].split(",");
                    for ( var j = 0; j < parts.length; j++) {
                        modularjs.include(parts[j]);
                    }
                }
            }
        }
    },
    setGlobal: function(evalFunction) {
        if (!!window[MODULARJS_SET_GLOBAL_VAR_NAME]) {
            try {
                delete window[MODULARJS_SET_GLOBAL_VAR_NAME];
            } catch (e) {
                window[MODULARJS_SET_GLOBAL_VAR_NAME] = undefined;
            }
        }
        evalFunction(MODULARJS_SET_GLOBAL_VAR);
        if (!!window[MODULARJS_SET_GLOBAL_VAR_NAME]) {
            modularjs.eval = evalFunction;
            return true;
        } else {
            return false;
        }
    },
    execScript: function(contents) {
        if (window.execScript) { window.execScript(contents);      }
    },
    withWindowEval: function(contents) {
        with (window) {            window.eval(contents);       }
    },
    insertScript: function(contents) {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.text = contents;
        modularjs.head.appendChild(script);
        modularjs.head.removeChild(script);
    },
    setEvalFunction: function() {
        if (modularjs.setGlobal(modularjs.execScript)) {            return;      }
        if (modularjs.setGlobal(modularjs.withWindowEval)) {  return;      }
        if (modularjs.setGlobal(modularjs.insertScript)) {          return;      }
        throw new Error("Cannot determine a good eval function");
    },
    include: function(module) {
        if (!module) {       throw new Error("No module name defined");       }
        if (modularjs.loaded[module]) {            return true;        }
        if (modularjs.loading[module]) {           throw new Error("Possible recursive import: " + module);        }
        modularjs.loading[module] = true;
        var contents = modularjs.getContents(module);
       // try {
            modularjs.eval(contents);
            modularjs.loaded[module] = true;
        /*} catch (e) {
            if (typeof console != "undefined") {
                console.error("Error importing module", module, e);
            }
        }*/
        modularjs.loading[module] = false;
        return !!modularjs.loaded[module];
    },
    getContents: function(module) {
        var contents = null;
        var filename = null;
        var build = module.replace(/\//g, ".");
        filename = build + ".build.compressed.js";
        if (contents = modularjs.getFileContents(filename)) {            return contents + "\r\n//@ sourceURL=" + filename;        } 
        filename = build + ".min.js";
        if (contents = modularjs.getFileContents(filename)) {            return contents + "\r\n//@ sourceURL=" + filename;        }
        filename = module.replace(/\./g, "/") ; + ".js";
        if (contents = modularjs.getFileContents(filename)) {            return contents + "\r\n//@ sourceURL=" + filename;        }
        throw new Error("Module " + module + " not found.");
    },
    getFileContents: function(filename) {
        try {
            modularjs.xhr.open("get", modularjs.basePath + filename, false);
            modularjs.xhr.send(null);
            if (modularjs.xhr.status == 0 || modularjs.xhr.status == 200) {
                return modularjs.xhr.responseText;
            }
        } catch (e) {       }
        return "";
    }
};
modularjsMOD = modularjs
modularjsMOD.getContents =  function(module) {
        var contents = null;         var filename = null;		
        filename = module.replace(/\.js$/,'') + '.js?'+Math.random() ;
        if (contents = modularjs.getFileContents(filename)) {            return contents + "\r\n//@ sourceURL=" + filename;        }
        throw new Error("Module " + module + " not found.");
}
var include = modularjsMOD.include;
if (typeof __build__ == "undefined") {    modularjs.init();}
