//test
JSLoader = function (options) {
    this.debugMode = options.debug;
    this.jsPath = options.jsPath;
    this.pluginsPath = options.pluginsPath;
    this.loadedScripts = null;
    this.loadedCSS = null;

    var collectScripts = function () {
        if (this.loadedScripts == null) {
            this.loadedScripts = {};
            var scriptElements = document.getElementsByTagName('script');
            for (var scriptId = 0; scriptId < scriptElements.length; scriptId++) {
                var scriptSrc = scriptElements[scriptId].src;
                if (typeof (scriptSrc) != "string") {
                    continue;
                }
                var scriptFileName = scriptSrc.substring(scriptSrc.lastIndexOf("/") + 1);
                if (typeof (this.loadedScripts[scriptFileName.toLowerCase()]) == "undefined") {
                    this.loadedScripts[scriptFileName.toLowerCase()] = scriptSrc;
                }
            }
        }
        if (this.loadedCSS == null) {
            this.loadedCSS = {};
            var linkElements = document.getElementsByTagName('link');
            for (var i = 0; i < linkElements.length; i++) {
                var linkSrc = linkElements[i].href;
                if (typeof (linkSrc) != "string") {
                    continue;
                }
                var cssFileName = linkSrc.substring(linkSrc.lastIndexOf("/") + 1);
                if (typeof (this.loadedCSS[cssFileName]) == "undefined") {
                    this.loadedCSS[cssFileName] = linkSrc;
                }
            }
        }
    };

    var preparePath = function (jsPath, jsFile) {
        var ret = jsPath;
        if (ret!="" && ret.lastIndexOf("/") != ret.length - 1) {
            ret += "/";
        }
        return ret + prepareJsName(jsFile);
    };

    var prepareJsName = function (jsFile) {
        if (this.debugMode) {
            return jsFile.replace(".js", ".min.js");
        }
        return jsFile;
    };

    var createScript = function (jsPath, isasync) {
		$.ajax({async:isasync, url: jsPath, dataType: 'script'});
    };

    var createCSS = function (cssPath) {
        var head = document.getElementsByTagName("head")[0] || document.documentElement;
        var link = document.createElement("link");
        link.rel = "stylesheet";
        link.type = "text/css";
        link.href = cssPath;
        head.appendChild(link);
    };

    var endsWith = function (str, end) {
        return str.length >= end.length && str.substr(str.length - end.length).toLowerCase() == end.toLowerCase();
    };

    var loadFiles = function (jsPath, files, issync) {
        collectScripts();
        for (var i = 0; i < files.length; i++) {
            var loadedFile = files[i];
            if (endsWith(loadedFile, ".css")) {
                var cssFileName = loadedFile.substring(loadedFile.lastIndexOf("/") + 1);
                if (typeof (this.loadedCSS[cssFileName.toLowerCase()]) == "undefined") {
                    this.loadedCSS[cssFileName.toLowerCase()] = loadedFile;
                    createCSS(preparePath(jsPath, loadedFile));
                }
            }
            else if (endsWith(loadedFile, ".js")) {
                var jsFileName = loadedFile.substring(loadedFile.lastIndexOf("/") + 1);
                if (typeof (this.loadedScripts[jsFileName.toLowerCase()]) == "undefined") {
                    this.loadedScripts[jsFileName.toLowerCase()] = loadedFile;
                    createScript(preparePath(jsPath, loadedFile),issync);
                }
            }
        }
    };

    this.loadJS = function () {
        loadFiles(this.jsPath, arguments, true);
    };

    this.loadSyncJS = function () {
        loadFiles(this.jsPath, arguments, false);
    };

    this.loadJSPlugin = function () {
        loadFiles(this.pluginsPath, arguments, true);
    };
	
    this.loadSyncJSPlugin = function () {
        loadFiles(this.pluginsPath, arguments, false);
    };
};
