epiton.context={};

epiton.context.Browser=function(){
	var _private={
		uriParts:{},
		initUriParts:function(){
			epiton.classloader.require("epiton.context.Browser.uriParts");
			_private.uriParts=this.parseUri(document.location);
			epiton.classloader.setRequireLoaded("epiton.context.Browser.uriParts");
		},
		parseOptions : {
			strictMode: false,
			key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
			q:   {
				name:   "queryKey",
				parser: /(?:^|&)([^&=]*)=?([^&]*)/g
			},
			parser: {
				strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
				loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
			}
		},
		isIp:function(str){
			var p=str.split(".");
			if(p.length==4){
				var i=0;
				for(i=0;i<p.length;i++){
					if(isNaN(parseInt(p[i]))){
						return false;
					}
				}
				return true;
			}else{
				return false;
			}
		},
		parseUri:function(string){
			var	o   = _private.parseOptions,
				m   = o.parser[o.strictMode ? "strict" : "loose"].exec(string),
				uri = {},
				i   = 14;
		
			while (i--) uri[o.key[i]] = m[i] || "";
		
			uri[o.q.name] = {};
			uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
				if ($1) uri[o.q.name][$1] = $2;
			});
			
			//parse base SLD
			var h=uri["host"];
			if(h&&!_private.isIp(h)){
				var dp=h.split(".");
				if(dp.length>2){
					h=dp[dp.length-2]+"."+dp[dp.length-1];
					hp=2;
					if(dp[dp.length-2]=="co"){
						h=dp[dp.length-3]+"."+h;
						hp++;
					}
					if(hp<dp.length){
						h="."+h;
					}
				}
			}
			uri["csld"]=h;
			return uri;
		}
	}
	var _public={
		getCookieSld:function(){
			return _private.uriParts["csld"];
		},
		getHost:function(){
			return _private.uriParts["host"];
		},
		parseUri:function(string){
			return _private.parseUri(string);
		}
	}
	
	_private.initUriParts();
	return _public;
}();
epiton.classloader.setLoaded("context");
