/**
 * Hash and url amnipulation
 * @constructor
 */
function URLUtil() {
	 
	var typeToRegExp = new Array();
	typeToRegExp["int"] = "[0-9]+"; 
	typeToRegExp["string"] = "[A-z]+[0-9A-z]+"; 
 
	this.setHashPage = function(page) {
		var hash = window.location.hash;
		if (hash.indexOf("#" + page) == 0) {
			return hash;
		}
		
		if (this.hasHash()) {
			var typeRegExp = typeToRegExp["string"];
			var regExp = new RegExp("#" + typeRegExp);
			hash = hash.replace(regExp, "#" + page);
		} else {
			hash = "#" + page;
		}
		window.location.hash = hash;			
		return hash;
	};
	
	/**
	 * Change the hash to the given state's hash
	 * @param {Object} state
	 */
	this.setState = function(state) {
		if(state != undefined && state != null && state.hash !== undefined) {
			window.location.hash = state.hash;
		}
	};
	
	/**
	 * Set the page hash. For example the call setHash("item", {page: 1}) 
	 * will result in item/page/1 set to the location hash.
	 * @param {String} page
	 * @param {Object} args
	 */
	this.setHash = function(page, args) {
		var hash = "#" + page;
		for (var arg in args) {
			hash += "/" + arg + "/" + args[arg]; 
		}
		window.location.hash = hash;
		return hash;
	};
	
	this.hasHash = function() {
		var hash = window.location.hash;
		return (hash != "") && (hash != "#");
	};
	
	this.hasHashArg = function(argName, argValue) {
		var hash = window.location.hash;
		return this.hasUrlArg(hash, argName, argValue);	
	};
	
	this.setHashArg = function(argName, argType, argValue)  {
		var hash = window.location.hash;
		hash = this.setUrlArg(hash, argName, argType, argValue);
		window.location.hash = hash;
	};
 
	this.setUrlArg = function(url, argName, argType, argValue)  {
		var typeRegExp = typeToRegExp[argType];
		var regExp = new RegExp(argName + '\/' + typeRegExp);
		url = url.replace(regExp, argName + "/" + argValue);
		return url;
	};
	
	this.getUrlArg = function(url, argName, argType) {
		var typeRegExp = typeToRegExp[argType];
		var regExp = new RegExp(argName + '\/' + typeRegExp);
		var match = url.match(regExp);
		if(match != null) {
			match = match[0];
			match = match.replace(argName + "/", "");
			return match;
		}
		return null;
	};
	
	this.hasUrlArg = function(url, argName, argValue) {
		return url.indexOf(argName + "/" + argValue) != -1;
	};
	 
};
