

var hashListener = {
	ie:		/MSIE/.test(navigator.userAgent),
	ieSupportBack:	true,
	hash:	document.location.hash,
	check:	function () {
		var h = document.location.hash;
		if (h != this.hash) {
			this.hash = h;
			this.onHashChanged();
		}
	},
	init:	function () {

		// for IE we need the iframe state trick
		if (this.ie && this.ieSupportBack) {
			var frame = document.createElement("iframe");
			frame.id = "state-frame";
			frame.style.display = "none";
			document.body.appendChild(frame);
			this.writeFrame("");
		}

		var self = this;

		// IE
		if ("onpropertychange" in document && "attachEvent" in document) {
			document.attachEvent("onpropertychange", function () {
				if (event.propertyName == "location") {
					self.check();
				}
			});
		}
		// poll for changes of the hash
		window.setInterval(function () { self.check() }, 50);
	},
	setHash: function (s) {
		// Mozilla always adds an entry to the history
		if (this.ie && this.ieSupportBack) {
			this.writeFrame(s);
		}
		document.location.hash = s;
	},

	setVar: function (var_name,var_value) {
	
		var ret_array = new Array();
		var ret_string="";
		var str = document.location.hash;
		
		var str_1 = str.split("?");
		//Här failar det
		if(str_1[1])
		{	var str_2 = str_1[1].split("&"); }
		else { str_2 = new Array(var_name+'='+var_value); }
		
		var is_in_hash = 0;
		
		for(var i = 0; i < str_2.length; i++)
		{
			var tmp = str_2[i].split("=");
			
			if(tmp[0] == var_name)
			{
				tmp[1] = var_value;
				ret_array.push(tmp[0]+"="+tmp[1]);
				is_in_hash=1;
			}
			else
			{
				ret_array.push(str_2[i]);	
			}
		}
		
		if(is_in_hash == 0)
		{
			hashListener.setHash(document.location.hash+"&"+var_name+"="+var_value);
		}
		else
		{
			for(var i = 0; i < ret_array.length; i++)
			{
				if(i == ret_array.length-1)
				{
					ret_string = ret_string+ret_array[i];
				}
				else
				{
					ret_string = ret_string+ret_array[i]+"&";
				}
			}		
			hashListener.setHash(str_1[0]+"?"+ret_string);
		}
	},

	getHash: function () {
	return document.location.hash;
	},
	writeFrame:	function (s) {
		var f = document.getElementById("state-frame");
		var d = f.contentDocument || f.contentWindow.document;
		d.open();
		d.write("<script>window._hash = '" + s + "'; window.onload = parent.hashListener.syncHash;<\/script>");
		d.close();
	},
	syncHash:	function () {
		var s = this._hash;
		if (s != document.location.hash) {
			document.location.hash = s;
		}
	},
	onHashChanged:	function () {}
};
hashListener.init();

