var _historyRefs = [];
//
var isIE = /MSIE/.test(navigator.userAgent);
var isWindows = /Windows/.test(navigator.userAgent);
var isFirefox = /Firefox/.test(navigator.userAgent);
var isNetscape = /Netscape/.test(navigator.userAgent);
var isSafari = /Safari/.test(navigator.userAgent);
var isOpera = /Opera/.test(navigator.userAgent);
var historyEnabled = true;
//
// if(!isIE && !isFirefox && !isNetscape && (!isWindows && isIE))
if(!isIE && !isNetscape && (!isWindows && isIE) || isFirefox){
	historyEnabled = false;
}
//

var _pollInterval = 100;
//
if (!window.flashHistory) new function() {
	window.flashHistory = this;
	// private variables
	
	var _listeners = []; // there has been much debate over whether this is appropriate as an array on comp.lang.javascript :-)
	var _currentHash = window.location.hash.substring(1);
	// do constructor type stuff here
	// :TODO: add alternative hash update detection methods if one is available (anybody?)
	// :NOTE: this method doesn't work to detect changes in IE 5.5 or 6.0 when the back button is 
	// pressed (location and location.hash are not updated - even when an anchor is added like in IE 5.0)
	if (setInterval)
		var _intervalID = setInterval(_CheckHash, _pollInterval);

	// private methods
	function _GetHash() {
		return window.location.hash.substring(1);
	};
	function _SetHash($newHash) {
		_currentHash = $newHash;
		window.location.hash = $newHash;
	};
	function _CheckHash() {
		var $newHash = _GetHash();
		//
		
		for (var i = _listeners.length; -1 < --i;){
			_listeners[i].historyCheck($newHash);
		}
		//
		if (_currentHash != $newHash) {
			_currentHash = $newHash;
			_NotifyListeners($newHash);
		}
	};
	function _NotifyListeners($newHash) {
		for (var i = _listeners.length; -1 < --i;){
			_listeners[i].historyUpdate($newHash);
		}
	};
	//
	this.CheckUpdate = _CheckHash
	// privileged methods
	this.AddListener = function($obj) {
		// consider throwing exceptions here (can't have NS4 compat then)
		if (!$obj.historyUpdate) return false; // add an exception here
		// check if this is already in here
		for (var i = _listeners.length; -1 < --i;)
			if (_listeners[i] == $obj)
				return false; // add an exception here?
		_listeners.push($obj);
		// do init
		if ($obj.historyLoad)
			$obj.historyLoad(_currentHash);
		return true;
	};
	this.RemoveListener = function($obj) {
		for (var i = _listeners.length; -1 < --i;) {
			if (_listeners[i] == $obj) {
				_listeners.splice(i,1);
				break;
			}
		}
	};
	
	this.AddBookmark = function($newHash) { // adds history and bookmark hash
		_currentHash = $newHash;
		_SetHash($newHash);
		// having this occur here makes _NotifyListeners fire twice due to the setInterval
		// _NotifyListeners($newHash);
	};

	/***************************
	* Browser specific patches *
	***************************/
	// do browser sniffing, fixes bugs that can't be detected through feature sniffing
	if ( isIE ) {
		// overwrite the old functions (gotta love javascript :-) )
		// IE 5.5 and 6.0 (I'm going to assume IE 7.x too - will test August 3) need the iframe method, since adding a hash makes no history
		if (navigator.appVersion.match(/MSIE (\d\.\d+)/)[1] >= 5.5 ) {
			// setup history IFrame
			
			var $historyFrame = false;
			this.AddBookmark = function($newHash) { // adds history and bookmark hash
				_currentHash = $newHash;
				_CreateHistoryHTML($newHash);
				_historyRefs[history.length] = _currentHash;
			};

			this._NotifyListeners = function($newHash) {
				// the first time this is called, it shouldn't notify, so it instead replaces itself with a version that will notify
				this._NotifyListeners = function($newHash) {
					_currentHash = $newHash;
					_SetHash($newHash);
					_NotifyListeners($newHash);
				};
			};
			
			function _CreateHistoryHTML($newHash) {
				_SetUpHistoryFrame(true);
				$historyFrame.document.open('text/html');
				$historyFrame.document.write('<html><head></head><body onload="window.parent.flashHistory._NotifyListeners(\''+$newHash+'\');"></body></html>');
				$historyFrame.document.close();
			};
			// set up the History Frame, for AddHistory method
			function _SetUpHistoryFrame(safeGaurd) {
				if (!window.frames['historyFrame']) {
					$historyFrame = document.createElement("iframe");
					$historyFrame.name = 'historyFrame';
					$historyFrame.id = 'historyFrame';
					$historyFrame.style.display = 'none';
					document.body.appendChild($historyFrame);
				}
				// reset the reference to the frame using the frames array - for some reason 
				// the other reference can't be used with document.open
				$historyFrame = window.frames["historyFrame"];
				// create root frame for the furthest back state
				if(!safeGaurd) {
					_CreateHistoryHTML(_currentHash);
				}
			};
			
			addToOnLoad(_SetUpHistoryFrame);
			//_SetUpHistoryFrame();
			
		} else { // IE 5.0 needs an anchor before it will take a new hash value
		
			this.AddBookmark = function($newHash) { // adds history and bookmark hash
				_currentHash = $newHash;
				_CreateAnchor($newHash);
				_SetHash($newHash);
				_NotifyListeners($newHash);
			};
			function _CreateAnchor($newHash) {
				var $anchor = document.createElement('a');
				$anchor.setAttribute('name',$newHash);
				if (/MSIE/.test(navigator.userAgent)) $anchor = document.createElement('<a name="'+$newHash+'"></a>');
				$anchor.style.display = 'none';
				$anchor.innerHTML = $newHash;
				document.body.appendChild($anchor);
			};
		}
		
	}
};
//

// default history listener for HH sites

	if(typeof hothouse=="undefined"){var hothouse=new Object();}
		if(typeof hothouse.util=="undefined"){hothouse.util=new Object();}
		if(typeof hothouse.util.HistoryListener=="undefined"){hothouse.util.HistoryListener=new Object();
	}
	
	hothouse.util.HistoryListener = function () {
		
		this.historyUpdate = function(newHash) {
			if(newHash == '') return;
			var flashMovie = document.getElementById(swfId);
   			flashMovie.changeFromHistory(newHash);
		};
		
		this.historyLoad = function(initHash) {
			// called when page is first loaded
			var flashMovie = document.getElementById(swfId);
   			flashMovie.historyInit(initHash); 
			if(hashData == undefined) return;
		};

		this.historyCheck = function(checkHash) {
			// called every 100 miliseconds by flashHistory object if the browser isn't IE
		};
	};

HistoryListener = hothouse.util.HistoryListener;

setupHistory = function() {
	if((isIE || isNetscape || isFirefox)){
		window.flashHistory.AddListener(new HistoryListener());
	}
}
function updateHistory(pageLevels) {
	//alert(pageLevels);
	window.flashHistory.AddBookmark(pageLevels) ;
}


	
	




