﻿
function Uri(s) 
{
	var ss = (typeof s=="undefined") ? window.location : s;
	
	if(typeof Uri.__initialized=="undefined")
	{
		Uri.prototype.toString = function()
		{
			var result = (this.protocol.length) ? this.protocol + "://" : "";
			result += (this.user.length) 
				? this.user + ":" + this.password + "@" + this.host 
				: (this.host.length) ? this.host : "";
			result += (this.port.length) ? ":" + this.port + this.path : this.path;
			result += (this.query.length) 
				? "?" + this.query.replace(/^&/, "")
				: (this.anchor.length) ? "#" + this.anchor : "";
			return result;
		}
		Uri.prototype.resolvePath = function(path)
		{
			var resolvedPath = path;
			if (resolvedPath.length > 0 && resolvedPath.charAt(0) == '~')
			{
				resolvedPath = (typeof gvx_ApplicationPath=="undefined")
					? resolvedPath.substring(1)
					: resolvedPath.replace(((gvx_ApplicationPath.charAt(gvx_ApplicationPath.length-1) == '/')
						? "~/" : "~"),gvx_ApplicationPath);					
			}
			return (resolvedPath.charAt(0) == '/') ? resolvedPath : "/" + resolvedPath;
		}
		Uri.prototype.containsQueryKey = function(key)
		{
			return (typeof this.queryValues[key]!="undefined");
		}
		Uri.prototype.setQueryValue = function(key, value)
		{
			var r = this.containsQueryKey(key);
			if (r)
			{
				this.queryValues[key] = value;
				this.query = "";
				for(var kk in this.queryValues)
				{
					this.query += ("&" + kk + "=" + encodeURIComponent(this.queryValues[kk]));
				}
				return this;
			}
			return this.addQueryValue(key, value);
		}		
		Uri.prototype.removeQueryValue = function(key)
		{
			if (this.containsQueryKey(key))
				this.queryValues[key] = undefined;
			return this;
		}
		Uri.prototype.addQueryValue = function(key, value)
		{
			if (this.query.length) this.query += "&";
			this.query += key + "=" + encodeURIComponent(value);
			this.queryValues[key] = value;
			return this;
		}
		Uri.prototype.addQuery = function(query)
		{
			if (query.length)
			{
				if (this.query.length) this.query += '&';				
				this.query += ((query.charAt(0) == '?' || query.charAt(0) == '&')
						? query.substring(1)
						: query);
				var	o = Uri.helper
					, uri = this;
				((query.charAt(0) == '?')?query.substring(1):query)
					.replace(o.q.splitter, function ($0, $1, $2) {
					if ($1) uri[o.q.name][$1] = decodeURIComponent($2);
					});
			}	
			return this;
		}
		Uri.prototype.copyQueryValue = function(uri, key)
		{
			if (uri.containsQueryKey(key))
				this.setQueryValue(key, uri.queryValues[key]);
			return this;
		}
		Uri.prototype.newWindow = function(surl, secure)
		{			
			window.open(this.toString(), "", "", true);
		}
		Uri.helper = {
			key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"]
			, q: {
				name:   "queryValues",
				splitter: /(?:^|&)([^&=]*)=?([^&]*)/g
				}
			, parser: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
		};
		Uri.__initialized = true;
	}

	var	o = Uri.helper
		, m = o.parser.exec(ss)
		, uri = this
		, i = o.key.length;

	while (i--) this[o.key[i]] = m[i] || "";

	this[o.q.name] = {};	
	this[o.key[12]].replace(o.q.splitter, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = decodeURIComponent($2);
	});

	return this;
}

var CurrentUrl = new Uri(window.location);

Uri.resolveUrl = function(surl, secure)
{
	var result = new Uri(surl);
	if (result.authority == "~")
	{
		result.protocol = (secure) ? "https" : CurrentUrl.protocol;
		result.user = CurrentUrl.user;
		result.password = CurrentUrl.password;
		result.host = CurrentUrl.host;
		result.port = CurrentUrl.port;
		result.path = CurrentUrl.resolvePath("~" + result.path);		
		result.authority = CurrentUrl.authority;
	}
	return result;
}
Uri.follow = function(surl, secure)
{
	window.location = Uri.resolveUrl(surl, secure);
}
Uri.newWindow = function(surl, secure)
{
  Uri.resolveUrl(surl, secure).newWindow();	
}

