/**

TODO: Implementation is incomplete
Currently supports the most typical URL type needs

**/



function Url(argUrl)
{

	if (!argUrl)
	{
		var startingUrl = self.location.href;
	}
	else
	{
		var startingUrl = argUrl;
	}

	var url = new Object();

	var protocol = "";
	var host = "";
	var port = -1;
	var path = "";

	var queryString = "";
	var anchor = "";

	// protocol and host
	var protocolIndex = startingUrl.indexOf('://');

	if (protocolIndex >= 0)
	{
		protocol = startingUrl.substring(0, protocolIndex).toLowerCase();
		host = startingUrl.substring(protocolIndex + 3);
		if(host.indexOf('/') >= 0)
		{
			host = host.substring(0, host.indexOf('/'));
		}

		// port
		var portIndex = host.indexOf(':');

		if (portIndex >= 0)
		{
			port = host.substring(portIndex);
			port = port.replace(":", "");
			host = host.substring(0, portIndex);
		}


		// querystring and anchor
		queryString = (startingUrl.indexOf('?') >= 0) ? startingUrl.substring(startingUrl.indexOf('?') + 1) : '';


		if(queryString.indexOf('#') >= 0)
		{
			queryString = queryString.substring(0, queryString.indexOf('#'));
			anchor = queryString.substring(queryString.indexOf('#') + 1, queryString.length);

		}


		var file = startingUrl.substring(protocolIndex + 3);
		file = file.substring(file.indexOf('/'));

	}
	else
	{
		file = startingUrl;
	}


	if (file.indexOf('?') >= 0) file = file.substring(0, file.indexOf('?'));

	var hashIndex = startingUrl.indexOf('#');

	if (hashIndex >= 0)
	{
		file = file.substring(0,hashIndex);
		anchor = startingUrl.substring(startingUrl.indexOf('#') + 1);
	}

	path = file;







	url.protocol = protocol;
	url.host = host;
	url.port = port;
	url.path = path;


	url.queryString = queryString;

	// convert our queryString into an object
	url.queryString = _getQueryStringNameValuePairArray(queryString);


	url.anchor = anchor;




	function getUrl()
	{

		var build = "";

		if (url.protocol != "")
		{
			build = url.protocol + "://" + url.host;
		}

		if (url.port != -1)
		{
			build += ":" + url.port;
		}

		if (url.path.substring(0,1) != "/")
		{
			build += "/" + url.path;
		}
		else
		{
		build += url.path;
		}

		if (getQueryString() != "")
		{
			build += getQueryString();
		}

		if (url.anchor != "")
		{
			build += "#" + url.anchor;
		}

		return build;

	}


	function setAnchor(anchor)
	{
		url.anchor = anchor;
	}

	function clearAnchor()
	{
		url.anchor = "";
	}


	function getPath()
	{
		return url.path;
	}


	function setPath(path)
	{
		url.path = path;
	}

	function setProtocol(argProtocol)
	{
		url.protocol = argProtocol;
	}

	function clearQueryStringAndAnchor()
	{
		clearQueryString();
		clearAnchor();
	}


	function clearQueryString()
	{
		url.queryString = new Array();
	}


	function addToQueryString(name, value)
	{
		url.queryString[name] = value;
	}

	function removeFromQueryString(name)
	{
		delete url.queryString[name];
	}



	function getQueryString()
	{

		var build = "";

		for (var key in url.queryString)
		{
			// needed for goofy ie
			if (key != "" && key != "indexOf")
			{

				if (build == "")
				{
					build += "?";
				}

				if (build != "")
				{
					build += "&";
				}

				build += key + "=" + url.queryString[key];
			}

		}

		return build;

	}


	function _getQueryStringNameValuePairArray(queryString)
	{
		var build = new Array();

		var split = queryString.split("&");

		for (var i = 0; i < split.length; i++)
		{

			var nvp = split[i];
			var nvpSplit = nvp.split("=");

			var name = nvpSplit[0];
			var value = nvpSplit[1];

			build[name] = value;

		}

		return build;

	}


	this.getUrl = getUrl;

	this.setPath = setPath;
	this.getPath = getPath;

	this.setProtocol = setProtocol;

	this.getQueryString = getQueryString;
	this.addToQueryString = addToQueryString;
	this.removeFromQueryString = removeFromQueryString;

	this.clearQueryStringAndAnchor = clearQueryStringAndAnchor;
	this.clearQueryString = clearQueryString;


	this.setAnchor = setAnchor;
	this.clearAnchor = clearAnchor;



}
