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();

			if (this.ie && this.ieSupportBack) {

				this.writeFrame(document.location.hash);

			}

		}

	},

	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;

	},

	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 () {



		var parts = this.hash.substring(1).split("?");

		var op = parts[0];
		
		var params = parts[1];
			params += "&display=1";

		if (op == "") op = 'index';
		
		if(op == "index")
		{
			startSwap();	
		}
		else if(parts[1] != "swap=true")
		{
			noClick = false;	
		}
		
		$.ajax({

			type: "POST",

			url: "content/" + op + ".php?" + params,

			cache: false,

			dataType: "text",

			data: params,

			success: function(responseText){
				
				var rawdata = responseText.split("|"); //If there aren't any |'s in the string, it will just grab the entire string and put it into items[0]
				for ( var i = 0; i < rawdata.length; i++ ) { //Loop through the rawdata[] array
					var item = (rawdata[i]).split("=>");	//Split each item into id=>value where id is item[0] and the value is item[1]
					if (item[0] != "") {	//If it is a valid HTML id
						if (item[1].substr(0,3) == "%V%" ) { //When sending a response, if you want to change the .value of an item (rather than .innerHTML), preface your response with %V%
							document.getElementById(item[0]).value = item[1].substring(3);  //Set the value property of the given HTML item to the value of item[1]
							//Element.writeAttribute(item[0], 'value', item[1].substring(3));						
						}
						else if(item[1].substr(0,6) == "%eval%")
						{
							eval(item[1].substr(6));	
						}
						else {
							try
							{
							document.getElementById(item[0]).innerHTML = item[1]; //Set the innerHTML property of the given HTML item to the value of item[1]
							//Element.update(item[0], item[1]);
							}
							catch (err)
							{
								
							}
						}
						//item[0] is the id of the HTML element, item[1] is the value of that HTML element
					}
				}

			},

			error: function (XMLHttpRequest, textStatus, errorThrown) {

				//alert("Error" + errorThrown); //Dont really want users seeing this

			}

		});



	}

};
