window.vmf = function(){

    // The VMF framework default settings
    var settings = {
        jQueryNoConflict: true,
        mapping: null,
        mappingFile: '//www.vmware.com/files/include/vmf/config/mapping.js'
    };
    
    var _init = false; // private flag to identify if VMF has been initialized.
    /**
     * Private function for loading external JS file statically.
     * @param {String} url The URL of the JS file
     */
    var loadJs = function(url){
        document.write('<script src="' + url + '" type="text/javascript"><\/script>');
    }
    
    /**
     * Private function for initializing the VMF framework
     */
    var initVmf = function(callback){
    
        // Call the jQuery noConflict function to release its $ variable.
        window.$j = settings.jQueryNoConflict ? jQuery.noConflict() : jQuery;
        
        // Read the file mapping file
        if (_init == false) {
            jQuery.xLazyLoader({
                js: settings.mappingFile,
                success: callback
            });
        }
        // Set the init flag to true after intilization
        _init = true;
        
    }
    
    // If jQuery has not been loaded yet, load it
    if (!window.jQuery) 
        loadJs("//www.vmware.com/files/include/vmf/plugin/jquery.1.3.2.js");
    
    // load all the required plugins    
    loadJs("//www.vmware.com/files/include/vmf/plugin/jquery.xLazyLoader.js");
    
    // load in the interface
    loadJs("//www.vmware.com/files/include/vmf/config/interface.js");
    
    return {
        /**
         * Public Function: loading the module and execute the VMF code
         */
        load: function(module, callback){
            // function for loading all resources, and execute the call back function (if specified)
            var loadFilesAjax = function(){
                var resources = settings.mapping[module];
                if (callback) {
                    jQuery.extend(resources, {
                        success: callback
                    });
                }
                jQuery.xLazyLoader(resources);
            };
            // make sure that the VMF framework is initiated before loading the resources. 
            _init ? loadFilesAjax : initVmf(loadFilesAjax);
            
        },
        /**
         * Public Function: configure the VMF framework
         * @param {Object} c The configuration object
         */
        config: function(c){
            jQuery.extend(settings, c);
        }
    }
}();
