if(typeof(window.URLPARSER_DEFINED) == 'undefined') {
	window.URLPARSER_DEFINED = true;
	
	URLParser = function () {};
	
	URLParser.parse = function(url) {
		this.url = url;
		
		var result = this.url.match(this.regexp);
		
		this.protocol = result[1];
		this.host = result[2];
		this.port = result[4];
		this.path = result[5];
		this.query = result[6];
		this.baseurl = this.getBaseURL();
		this.parseQueryParams();
		return this;
	};
	
	URLParser.parseQueryParams = function() {
		if(typeof(this.query) == 'undefined' || this.query == '') {
			return false;
		}
		
		var paramPairs = this.query.split("&");
		if(paramPairs.length > 0) {
			this.queryParams = new Object();
			for(var i = 0;i < paramPairs.length;i++) {
				var paramPair = paramPairs[i].split("=");
				this.queryParams[paramPair[0]] = paramPair[1];
			}
		}
	};		
	
	URLParser.getBaseURL = function() {
		baseUrl = this.protocol + "://" + this.host;
		if(this.port) {
			baseUrl = baseUrl + ":" + this.port;
		}
		if(this.path) {
			baseUrl = baseUrl + "/" + this.path;
		}
		return baseUrl;
	};
	
	URLParser.getURL = function() {
		var newUrl = this.getBaseURL();
		var queryString = '';
		for (var key in this.queryParams) {
			if(queryString != '') queryString += '&';
			queryString += key + '=' + this.queryParams[key];
		}
		return newUrl + '?' + queryString;
	};
	
	URLParser.setQueryParam  = function(key,value) {
		this.queryParams[key] = value;
	};

	URLParser.getQueryParams = function() {
		return this.queryParams;
	};

	URLParser.prototype.parse = URLParser.parse;
	URLParser.prototype.parseQueryParams = URLParser.parseQueryParams;
	URLParser.prototype.getQueryParams = URLParser.getQueryParams;
	URLParser.prototype.setQueryParam = URLParser.setQueryParam;
	URLParser.prototype.getBaseURL = URLParser.getBaseURL;
	URLParser.prototype.getURL = URLParser.getURL;
	URLParser.prototype.regexp = /(https?):\/\/([a-zA-Z0-9_\-\.]+)(:([0-9]+))?\/?([a-zA-Z0-9_\.]+)?\??(.*)?/;
}
