/**
 * require is used for on demand loading of JavaScript
 *
 * require r1 // 2008.02.05 // jQuery 1.3.2
 *  // basic usage $.require("comp1.js");
 *
 * version: 1.0 (2009/12/01)
 *
 * Copyright (c) 2008 Manish Shanker
 * Copyright (c) 2009 Ferenc Radius, ferenz1@gmail.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @return The jQuery object
 * @author Manish Shanker
 * @author Ferenc Radius
 */
(function($) {

    /**
     * Css files will be added as link tag
     *
     * @param file
     */
    function loadCss(file){
        var link = document.createElement('link');
        link.rel = "stylesheet";
        link.type = "text/css";
        link.href = $.cssPath + file;
        $(link).insertAfter("head > link[rel='stylesheet']");
    }

    /**
     * The require plugin
     * @param fileList  string array or string holding the js/css file names to load
     * @param params    object holding parameter like browser, callback, cache
     */
    $.require = function(fileList, params) {

        var executed = false;
        var params   = params || {};
        var browser  = params.browser !== false;
        var cache    = !!$.requireCache;
        var cBack    = params.callBack || function() {};

        $.require.loadedLib = $.require.loadedLib || {};
        fileList =(typeof fileList === "string") ? [fileList] : fileList;

        if (!browser) {
            return $;
        }

        if(typeof params.cache == "boolean") {
            cache = params.cache;
        }

        if (!$.scriptPath) {
            throw Error('$.scriptPath not set!');
        }

        for( var i = 0; i < fileList.length; i++) {
            var file = fileList[i];

            if (!$.require.loadedLib[file]) {

                if(file && file.match(/\.css$/) != null) {
                    loadCss(file);
                    cBack.call(this);
                } else {
                //alert($.scriptPath + file);
                    $.ajax({
                        type : "GET",
                        url : $.scriptPath + file,
                        success: function() {
                            executed = true;
                            cBack.call(this);
                        },
                        dataType : "script",
                        cache : cache,
                        async : false
                    });
                }
                $.require.loadedLib[file] = true;
            }
        }

        if(!executed) {
            cBack.call(this, true);
        }
        return $;
    };
})(jQuery);
