/**
 * @class
 * 
 * A utility for creating nested namespaces
 * 
 * @author Filip Michalowski / Fi Stockholm
 */

Array.prototype.inject = function(v,f)
{
    for ( var i=0; i < this.length; i++)
        v = f(v, this[i]);
    return v;
};

var Namespace = function(){
	var separator = ".";
	
	/** @lends Namespace */
	return {	
		/**
		 * Creates a namespace tree from a dot-separated descriptor string, or 
		 * returns the namespace if it already exists.
		 * 
		 * @param {String} nsString e.g. "fi.lib"
		 * @return {Object} The namespace
		 */
		create: function(nsString){
			return nsString.split(separator).inject(window, function(parent, child){
				if (!parent[child]) {
					parent[child] = {};
				}
				return parent[child];
			});
		}
	};
}();
