/*******************************************************************
 * Loads the various generally needed JS files and initializes them
 * for this site.  This file allows us to configure the files to be
 * loaded specifically to the environment/site.
 *
 * This should be adjusted for each unique environment or site.
 * 
 * JavaScript File Dependencies:
 *   none
 * 
 * @Author Pat B
 * @version 0.1
 *******************************************************************/

// Initialize the cfc library for this environment
if (typeof cfc == "undefined") {
    cfc = new Object();
}

/**
 * This is an object that is used to initialize the site
 * for the CFC library components.
 */
cfc.init = (function(objSpec) {

    /****************************************
     * PRIVATE Members -- Dependencies
     ****************************************/
    var jsFilesToLoad = objSpec.jsFilesToLoad;
	var strgRegExlocalSiteDomains = objSpec.strgRegExlocalSiteDomains;
    
    /****************************************
     * PUBLIC Members
     ****************************************/
    var publicApi = {};
    
    publicApi.localHost = window.location.hostname;
    publicApi.jsFilesToLoad = jsFilesToLoad;
    
    // Returns a string that represents a regEx (from publicApi.localSiteDomains) that can be used to identify local site links
    publicApi.getLocalDomainRegEx = (function() {
        var strgRegExSiteLocalDomain; // for debugging
        var hostname = window.location.hostname;
        var temp = strgRegExlocalSiteDomains;
        for (var prop in temp) {
            if (temp[prop]) {
                if (hostname.toLowerCase().indexOf(prop) >= 0) {
                    strgRegExSiteLocalDomain = temp[prop];
                    break;
                }
            }
        }
        return strgRegExSiteLocalDomain
    });
	
	return publicApi;
    
    /*
     * Optional literal objSpec parameter used to set all necessary intenral parameters.
     * All dependencies should be provided through this objSpec parameter.
     */
})({ 
	jsFilesToLoad: [["jquery.min.js", "//ajax.googleapis.com/ajax/libs/jquery/1.3.2/"], ["cfc.js", "/etc/designs/cfc-lib/cfc-common/js/"], ["cfc.util.js", "/etc/designs/cfc-lib/cfc-common/js/"]],
	strgRegExlocalSiteDomains: {
        nrucfc: "\\.(nrucfc)\\.(coop|org)$",
        ncsc: "\\.(ncsc)\\.(coop|ws)$",
        rtfc: "\\.(rtfc)\\.(coop)$",
        statstest: "\\.(statstest)\\.(com)$" // used for local testing
    }
});

/* Import external JS files loading in a private scope. I had to do it this way 
 * because I haven't loaded the cfc library components that contain the loadScript method
 */
(function() {

    var loadScript = (function(objSpec) {
        document.write("<script src='" + objSpec.path + objSpec.file + "'><\/script>")
    });
    
    /********* load scripts *********/
    // Loop through the array of files to load
    for (var i = 0; i < cfc.init.jsFilesToLoad.length; i++) {
        var file = cfc.init.jsFilesToLoad[i][0]; // for debugging
        var path = cfc.init.jsFilesToLoad[i][1]; // for debugging
        loadScript({
            file: cfc.init.jsFilesToLoad[i][0],
            path: cfc.init.jsFilesToLoad[i][1]
        });
    }
})();


