var MPDebuggable = {
	install: function(params) {
		// when only a string is provided, assume that's the name of the target
		if (typeof params == "string") {
			params = {
				into: window[params],
				as: params
			};
		}
		
		// when into isn't defined, but as is provided, use as to determine target
		if (typeof params.into == "undefined" && typeof params.as == "string") {
			params.into = window[params.as];
		}
		
		// extend functionality into the target
		$.extend(params.into, {
			debugEnabled: false,
			isProduction: MPDebuggable.isProduction(),
			
			enableDebug: function(message) {
				this.debugEnabled = true;
				if (typeof message == "string") {
					this.trace(message);
				}
			},
			
			trace: function(message) {
				if (this.debugEnabled && !this.isProduction) {
					if (typeof console == "undefined" || typeof console.log == "undefined") {
						console = {log: function() {}};
					}
					if (typeof message == "object") {
						console.log("----- " + this.traceAs + " -----");
						console.log(message);
						console.log("-----");
					} else {
						console.log(this.traceAs + ": " + message);
					}
				}
			}
		});
		
		// define how to trace
		if (typeof params.as == "string") {
			params.into.traceAs = params.as;
		}
		
		// enable on install
		if (typeof params.enable == "boolean" && params.enable) {
			params.into.enableDebug("Debug enabled on install.");
		}
	},
	
	isProduction: function() {
		var isProduction = true;
		
		// parse out the host name and reverse it
		var reversedHost = location.host.split(".").reverse();
		var tld = reversedHost[0];
		var sld = reversedHost[1];
		var sub = reversedHost[2];
		
		// local or localhost in tld is not production
		if (tld == "local" || tld == "localhost") {
			isProduction = false;
		}
		
		// int at the beginning of the sld is not production
		if (typeof sub == "string" && sub.substr(0,3) == "int") {
			isProduction = false;
		}
		
		return isProduction;
	}
}
