/* unFocus.History, version2.0 (beta 4) (2009/07/09)
Copyright: 2005-2009, Kevin Newman (http://www.unfocus.com/)
License: MIT - license.txt */
if (!window.unFocus) var unFocus = {};
unFocus.EventManager = function () {
	this._listeners = {};
	for (var i = 0; i < arguments.length; i++) {
		this._listeners[arguments[i]] = []
	}
};
unFocus.EventManager.prototype = {
	addEventListener: function (a, b) {
		for (var i = 0; i < this._listeners[a].length; i++) if (this._listeners[a][i] == b) return;
		this._listeners[a].push(b)
	},
	removeEventListener: function (a, b) {
		for (var i = 0; i < this._listeners[a].length; i++) {
			if (this._listeners[a][i] == b) {
				this._listeners.splice(i, 1);
				return
			}
		}
	},
	notifyListeners: function (a, b) {
		for (var i = 0; i < this._listeners[a].length; i++) this._listeners[a][i](b)
	}
};
unFocus.History = (function () {
	function Keeper() {
		var c = this,
		_pollInterval = 200,
		_intervalID, _currentHash;
		var d = function () {
			return location.hash.substring(1)
		};
		_currentHash = d();
		var e = function (a) {
			window.location.hash = a
		};
		function _watchHash() {
			var a = d();
			if (_currentHash != a) {
				_currentHash = a;
				c.notifyListeners("historyChange", a)
			}
		}
		if (setInterval) _intervalID = setInterval(_watchHash, _pollInterval);
		c.getCurrent = function () {
			return _currentHash
		};
		c.addHistory = function addHistory(a) {
			if (_currentHash != a) {
				_currentHash = a;
				e(a);
				c.notifyListeners("historyChange", a)
			}
			return true
		};
		if (history.navigationMode) history.navigationMode = 'compatible';
		if (/WebKit\/\d+/.test(navigator.appVersion) && navigator.appVersion.match(/WebKit\/(\d+)/)[1] < 420) {
			var f = history.length,
			_historyStates = {},
			_form, _recentlyAdded = false;
			function _createSafariSetHashForm() {
				_form = document.createElement("form");
				_form.id = "unFocusHistoryForm";
				_form.method = "get";
				document.body.insertBefore(_form, document.body.firstChild)
			}
			e = function (a) {
				_historyStates[f] = a;
				_form.action = "#" + d();
				_form.submit()
			};
			d = function () {
				return _historyStates[f]
			};
			_historyStates[f] = _currentHash;
			function addHistorySafari(a) {
				if (_currentHash != a) {
					_currentHash = a;
					f = history.length + 1;
					_recentlyAdded = true;
					e(a);
					c.notifyListeners("historyChange", a);
					_recentlyAdded = false
				}
				return true
			}
			c.addHistory = function (a) {
				_createSafariSetHashForm();
				c.addHistory = addHistorySafari;
				return c.addHistory(a)
			};
			function _watchHistoryLength() {
				if (!_recentlyAdded) {
					var a = history.length;
					if (a != f) {
						f = a;
						var b = d();
						if (_currentHash != b) {
							_currentHash = b;
							c.notifyListeners("historyChange", b)
						}
					}
				}
			};
			clearInterval(_intervalID);
			_intervalID = setInterval(_watchHistoryLength, _pollInterval)
		} else if (
		/*@cc_on!@*/
		0 && navigator.userAgent.match(/MSIE (\d+\.\d+)/)[1] >= 5.5) {
			if (document.documentMode && document.documentMode >= 8) return;
			var g, _historyFrameRef;
			function _createHistoryFrame() {
				var a = "unFocusHistoryFrame";
				g = document.createElement("iframe");
				g.setAttribute("name", a);
				g.setAttribute("id", a);
				g.setAttribute("src", 'javascript:;');
				g.style.position = "absolute";
				g.style.top = "-900px";
				document.body.insertBefore(g, document.body.firstChild);
				_historyFrameRef = frames[a];
				_createHistoryHTML(_currentHash, true)
			}
			function _createHistoryHTML(a) {
				with(_historyFrameRef.document) {
					open("text/html");
					write("<html><head></head><body onl", 'oad="parent.unFocus.History._updateFromHistory(\'' + a + '\');">', a + "</body></html>");
					close()
				}
			}
			function updateFromHistory(a) {
				_currentHash = a;
				c.notifyListeners("historyChange", a)
			}
			c._updateFromHistory = function () {
				c._updateFromHistory = updateFromHistory
			};
			function addHistoryIE(a) {
				if (_currentHash != a) {
					_currentHash = a;
					_createHistoryHTML(a)
				}
				return true
			};
			c.addHistory = function (a) {
				_createHistoryFrame();
				c.addHistory = addHistoryIE;
				return c.addHistory(a)
			};
			c.addEventListener("historyChange", function (a) {
				e(a)
			})
		}
	}
	Keeper.prototype = new unFocus.EventManager("historyChange");
	return new Keeper()
})();
