// Copyright(c)2005 Daniel Pupius - http://pupius.co.uk/
var StateMonitor = {
	_state: "",
	_laststate: "",
	_framename: "",
	_framesrc: "state-monitor.php",
	

	init: function() {

		// So far only IE needs the Iframe for state, so return if not IE
		if(!document.all) {			
			// Start monitor
			setInterval("StateMonitor.monitor()",10);
			return;
		}

		var frame = document.createElement("iframe");
		this._framename = "frame" + new Date().valueOf();
		frame.id = this._framename ;
		frame.name = this._framename;
		frame.style.position = "absolute";
		frame.style.left = "-100px";
		frame.style.top = "-100px";
		frame.style.width = "10px";
		frame.style.height = "10px";
		document.getElementsByTagName("body")[0].appendChild(frame);

		if(location.href.indexOf("#") > -1) {
			var s = location.href.substr(location.href.indexOf("#")+1,location.href.length);
			this.recoverState(s);
		}
	},

	getState: function() {
		return this._state;
	},

	getLastState: function() {
		return this._laststate;
	},

	setState: function(s) {
		if(!document.all) {
			var curl = location.href;
			window.location = curl.substr(0,curl.indexOf("#")>=0?curl.indexOf("#"):curl.length) + '#' + s;
			return false;		
		}

		var frame = null;
		if(document.getElementById(this._framename).contentDocument) frame = document.getElementById(this._framename).contentDocument;
		else frame = document.frames[this._framename].document;

		frame.location = (this._framesrc + '?s='+s);
		return false;
	},

	recoverState: function(s) {
		this._laststate = this._state;
		this._state = s;
		var curl = location.href;
		window.location = curl.substr(0,curl.indexOf("#")>=0?curl.indexOf("#"):curl.length) + '#' + s;
		StateMonitor.onstatechange();
	},

	monitor: function () {
		if(location.href.indexOf("#") > -1) {
			var s = location.href.substr(location.href.indexOf("#")+1,location.href.length);
			if(s != this._state) {
				this._laststate = this._state;
				this._state = s;
				this.onstatechange();
			}
		}
	},

	onstatechange: function() {	}
}