﻿/*
 * Copyright ClosingCorp Inc.
 */
String.prototype.endsWith = function(str) {
	return (this.match(str+"$")==str)
}
String.prototype.startsWith = function(str) {
	return (this.match("^"+str)==str)
}
String.prototype.trim = function() {
	return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))
}

if (typeof CC == "undefined" || !CC) {
	var CC = {};
}

CC.options = {};
CC.options.HTTP_DOMAIN = "http://www.closing.com";
CC.options.JS_PATH = CC.options.HTTP_DOMAIN + "/partner/assets/js/cc";

CC.namespace = function() {
    var a=arguments, o=null, i, j, d;
    for (i=0; i<a.length; i=i+1) {
        d=(""+a[i]).split(".");
        o=CC;

        // CC is implied, ignore if included
        for (j=(d[0] == "CC") ? 1 : 0; j<d.length; j=j+1) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }

    return o;
};

//// CC.lang
CC.namespace("lang");
CC.lang.merge = function(obj1, obj2) {
	if (obj1 != null && obj2 != null) {
		for (attr in obj2) {
			obj1[attr] = obj2[attr];
		}
	}
}

// jsFiles => ["cc.util.uriParser", "cc.widget.calculator575x590"]
// or jsFiles => "cc.util.uriParser"
CC.load = function(uris, onSuccess, onFailure) {
	var files = (typeof uris == "string") ? [uris] : uris;

	var loader = new YAHOO.util.YUILoader();
	for (i = 0; i < files.length; i++) {
		var file = files[i];

		var fileFullPath = file;
		if (file.startsWith("cc.")) {
			fileFullPath = CC.options.JS_PATH + "/" + file;
		}

		var fileType = file.endsWith(".css") ? "css" : "js";
		loader.addModule({
			name: file, //module name; must be unique
			type: fileType, //can be "js" or "css"
		    fullpath: fileFullPath,
		    varName: "JSON" // a variable that will be available when the script is loaded.  Needed
		                     // in order to act on the script immediately in Safari 2.x and below.
		});
	}
	loader.onSuccess = onSuccess;
	loader.onFailure = onFailure;
	loader.require(files);
	loader.insert();
};

// Autoload a JS file
if (typeof ccAutoloadJsOptions != "undefined") {
	CC.load(ccAutoloadJsOptions.file, ccAutoloadJsOptions.onSuccess, ccAutoloadJsOptions.onFailure);
}