var DefaultAgent = Class.create({
	listener: null,
	currentLocation: null,
	initialize: function() {
		this.listener = Prototype.emptyFunction;
		this._setup();
	},
	add: function(url) {
		window.location.hash = url;
		this.currentLocation = window.location.hash;
	},
	addListener: function(listener) {
		this.listener = listener;
		this.currentLocation = "";
		this._check();
	},
	_setup: function() {
		this.currentLocation = "";
		// setup location observer
		window.setTimeout(function() {
			this._check();
		}.bind(this), 100);
	},
	_check: function() {
		if (window.location.hash != this.currentLocation) {
			this.currentLocation = window.location.hash;
			this.listener(this.currentLocation.replace(/#/, ""));
		}
		window.setTimeout(function() {
			this._check();
		}.bind(this), 100);
	}
});
var IEAgent = Class.create(DefaultAgent, {
	historyFrame: null,
	initialize: function($super) {
		$super();
	},
	add: function($super, url) {
		var doc = this.historyFrame.contentWindow.document;
		doc.open("javascript:'<html></html>'");
		doc.write("<html><head><title>" + document.title + "</title><scri" + "pt type=\"text/javascript\">parent.window.dhtmlHistory.update('" + url + "');</scri" + "pt></head><body></body></html>");
		doc.close();
		$super(url);
	},
	titlePatch: function() {
		this.historyFrame.contentWindow.document.title = document.title;
	},
	update: function(url) {
		/*this option below gives the numeric sign when is setting the window location hash property*/
		/*window.location.hash = url;*/
		if (url != '' || window.location.hash.length > 1){
		window.location.hash = url;
			/*alert("on update funcion url: " + url + " , hash: " +  window.location.hash + " hash length: " + window.location.hash.length + " url length: " + url.length);*/
			this.removeLastHashSign();
		}
		this._check();
	},
	_setup: function() {
		var frame = "<iframe src=\"/blank.htm" + window.location.hash + "\" style=\"display: none;\" id=\"_history\"></iframe>";
		document.write(frame);
		this.historyFrame = $("_history");
	},
	_check: function() {
		if (window.location.hash != this.currentLocation) {
			this.currentLocation = window.location.hash;
			this.listener(this.currentLocation.replace(/#/, ""));
		}
	},
	removeLastHashSign: function(){
	}
});
if (Prototype.Browser.IE) {
	window.dhtmlHistory = new IEAgent();
} else if (Prototype.Browser.Gecko) {
	window.dhtmlHistory = new DefaultAgent();
} else if (Prototype.Browser.WebKit) {
	window.dhtmlHistory = new DefaultAgent();
} else if (Prototype.Browser.Opera) {
	window.dhtmlHistory = new DefaultAgent();
} else {
	// not supported browser
}

