﻿/**
* require is used for on demand loading of JavaScript
* 
* require r1 // 2008.02.05 // jQuery 1.2.2
* 
* // basic usage (just like .accordion) 
* $.require("comp1.js");
* 

* @param  jsFiles string array or string holding the js file names to load
* @param  params object holding parameter like browserType, callback, cache
* @return The jQuery object
* @author Manish Shanker
*/

(function($){
    $.require = function(jsFiles, params) {

        var params = params || {};
        var bType = params.browserType===false?false:true;

        if (!bType){
            return $;
        }

        var cBack = params.callBack || function(){};
        var eCache = params.cache===false?false:true;

        if (!$.require.loadedLib) $.require.loadedLib = {};

        if ( !$.scriptPath ) {
            var path = $('script').attr('src');
            $.scriptPath = path.replace(/[\w+\.+]{1,}.js$/, '');
        }
        if ( !$.cssPath ) {
            var path = $('link').attr('href');
            $.cssPath = path.replace(/\w+\.css$/, '');
        }
        
        if (typeof jsFiles === "string") {
            jsFiles = new Array(jsFiles);
        }
        for (var n=0; n< jsFiles.length; n++) {
            if (!$.require.loadedLib[jsFiles[n]]) {
            	var info = fileinfo(jsFiles[n]);
            	if(info.tag == 'script'){
            		$.ajax({
                    type: "GET",
                    url: $.scriptPath + jsFiles[n],
                    success: cBack,
                    dataType: "script",
                    cache: eCache,
                    async: false
                });
            	}else if(info.tag == 'link'){
            		var headerTag = document.getElementsByTagName('head')[0];
            		var styleTag = document.createElement(info.tag);
								styleTag.setAttribute('type', 'text/css');
								styleTag.setAttribute('rel', 'stylesheet');
								styleTag.setAttribute('href', $.cssPath + jsFiles[n]);
								headerTag.appendChild(styleTag);
            	}
              $.require.loadedLib[jsFiles[n]] = true;
            }
        }
        //console.dir($.require.loadedLib);

        return $;
    };
})(jQuery);

//pass a file name and return a array with file name and extension function 
function fileinfo(data){
    data = data.replace(/^\s|\s$/g, "");
    var m;
    if (/\.\w+$/.test(data)) {
        m = data.match(/([^\/\\]+)\.(\w+)$/);
        if (m) {
            if (m[2] == 'js') {
                return {
                    filename: m[1],
                    ext: m[2],
                    tag: 'script'
                };
            }
            else
            if (m[2] == 'css') {
                return {
                    filename: m[1],
                    ext: m[2],
                    tag: 'link'
                };
            }
            else {
                return {
                    filename: m[1],
                    ext: m[2],
                    tag: null
                };
            }
        }
        else {
            return {
                filename: null,
                ext: null
            };
        }
    } else {
        m = data.match(/([^\/\\]+)$/);
        if (m) {
            return {
                filename: m[1],
                ext: null,
                tag: null
            };
        }
        else {
            return {
                filename: null,
                ext: null,
                tag: null
            };
        }
    }
}
