(function(){
	var rootNamespace = function(str,clean){
	 var use = function (ns) {   
	  if (typeof ns !== 'string' || ns.length === 0) {
	   throw new Error('Invalid or empty namespace');
	  }
	  
	  var root = window[str];
	  ns = ns.split('.');
	  for (var i = (ns[0] === str ? 1 : 0); i < ns.length; i++) {
	   if (typeof root[ns[i]] === 'undefined') {
	    root[ns[i]] = {};
	   }
	   root = root[ns[i]];
	  }
	  return root;
	 };
	 
	 var applyObj=function(base,over,write){
	  for(var x in over){
	   if( write || !(base[x]) ){
	    base[x]=over[x];		  
	   }else{
		   throw new Error('Cannot overwrite '+ x +' in safe mode. It already exists in the namespace.');
	   }
	  }
	 };
	 
	 var resolveInterface=function(nsStr,fn,overWrite){
	  var interface={};
	  if(typeof nsStr === 'string'){
	   interface.ns=nsStr;
	   interface.fn=fn;
	   interface.over=overWrite;
	  }else if(typeof nsStr==='function'){
	   interface.ns=undefined;
	   interface.fn=nsStr;
	   interface.over=fn;		 
	  }
	  return interface;	 
	 };
	 
	 
	 var exec=function(nsStr,fn,overWrite){
	  var x=resolveInterface(nsStr,fn,overWrite);
	  var ns,fnResult;
	  if(x.fn){
	   fnResult=x.fn();
	  }
	  if(x.ns){
	   ns=use(x.ns);
	  }
	  if(x.fn && x.ns){
	   applyObj(ns,fnResult,x.over);
	  }
	  return (x.ns)?ns:fnResult;
	 };
	 
	 if(!window[str] || clean){
	  window[str]=exec;
	  window[str].Namespace={define:use};
	 }else{
	  throw new Error('this namespace already existed. Cannot Overwrite');
	 }
	};
	rootNamespace('BN');
})();


BN('Environment.Host', function(){
	var resolveDomain=function(str){
		if(str==='COMMUNITY'){
			str='my';
		}else if (str === 'NETORDERPATH'){
			str='cart4';
		}
		var res;
		var host=window.location.host;
		var myHost;
		if(host.substr(0,2)==='pl' ||  host.substr(0,2)==='hp' || host.substr(0,4)==='mypl' || host.substr(0,4)==='myhp'){
			myHost=(host.substr(0,2)==='my')?host.substr(2,4):host.substr(0,2);
			if(str==='my'){
				str+=myHost;
			}else if(str==='www'){
				str=myHost;
			}else{
				str=myHost+str;
			}
		}
		return str+'.barnesandnoble.com';
	};

	return {
		getMappedDomain: resolveDomain
	}
});


	