/*
	unFocus.Utilities.EventManager, version 1.0b (beta) (2005/12/16)
	Copyright: 2005, Kevin Newman (http://www.unfocus.com/Projects/)
	License: http://creativecommons.org/licenses/LGPL/2.1/
*/

// make sure faux-namespace is available before adding to it
if (typeof unFocus == 'undefined') var unFocus = {};
if (!unFocus.Utilities) unFocus.Utilities = {};

/* Class EventManager
	Provides the interface and functionality to a Subscriber/Subscriber Pattern interface for
	classes to easily inherit or use. */
unFocus.Utilities.EventManager = function(arg) {
	this._listeners = {};
	for (var i = arguments.length; -1 < --i;) {
		this._listeners[arguments[i]] = [];
	}
};

/* Method: addEventListener
	A public method that adds an event listener to be called when the hash changes. */
unFocus.Utilities.EventManager.prototype.addEventListener = function($type, $listener) {
	// check that listener is not in list
	for (var i = this._listeners[$type].length; -1 < --i;)
		if (this._listeners[$type][i] == $listener) return;
	// add listener to appropriate list
	this._listeners[$type].push($listener);
};

/* Method: removeListener
	A public method that removes an event listener. */
unFocus.Utilities.EventManager.prototype.removeEventListener = function($type, $listener) {
	// search for the listener method
	for (var i = this._listeners[$type].length; -1 < --i;) {
		if (this._listeners[$type][i] == $listener) {
			this._listeners.splice(i,1);
			break;
		}
	}
};

/* Method: notifyListeners
	Notifies listeners when an event accurs. */
unFocus.Utilities.EventManager.prototype.notifyListeners = function($type, $data) {
	for (var i = this._listeners[$type].length; -1 < --i;)
		this._listeners[$type][i]($data);
};




/*
	HistoryKeeper, version 1.8 (alpha) (2005/12/16)
	Copyright: 2005, Kevin Newman (http://www.unfocus.com/Projects/)
	License: http://creativecommons.org/licenses/LGPL/2.1/
*/
if (typeof unFocus == 'undefined') var unFocus = {};
if (!unFocus.History) unFocus.History = {};

/*
   Class: unFocus.History.Keeper
   A singleton with subscriber interface (<unFocus.Utilities.EventManager>) that keeps a history and provides deep links for Flash and AJAX apps
*/
unFocus.History.Keeper = (function () {

var Keeper = function() {
	// bool: initialize - whether or not the class has been initialized
	var _self = this,
		// IFrame references
		_historyFrameObj, _historyFrameRef,
		// set the poll interval here.
		_pollInterval = 200, _intervalID,
		// get the initial Hash state
		_currentHash = _getHash();

	/*
	method: _getHash
		A private method that gets the Hash from the location.hash property.
	 
	returns:
		a string containing the current hash from the url
	*/
	function _getHash() {
		return location.hash.substring(1);
	}
	/*
	method: _setHash
		A private method that sets the Hash on the location string (the current url).
	*/
	function _setHash($newHash) {
		window.location.hash = $newHash;
	}
	
	/*
	method: _watchHash
		A private method that is called every n miliseconds (<_pollInterval>) to check if the hash has changed.
		This is the primary Hash change detection method for most browsers. It doesn't work to detect the hash
		change in IE 5.5+ or various other browsers. Workarounds like the iframe method are used for those 
		browsers (IE 5.0 will use an anchor creation hack).
	*/
	function _watchHash() {
		var $newHash = _getHash();
		if (_currentHash != $newHash) {
			_currentHash = $newHash;
			_self.notifyListeners('historyChange', $newHash);
		}
	}
	// set the interval
	if (setInterval) _intervalID = setInterval(_watchHash, _pollInterval);

	/*
	method: _updateFromHistory
		A private method that is meant to be called only from HistoryFrame.html.
		It is not meant to be used by an end user even though it is accessable as public.
	*/
	_self._updateFromHistory = function($hash) {
		_currentHash = $hash;
		_self.notifyListeners('historyChange', $hash);
	};
	
	/*
	method: getCurrent
		A public method to retrieve the current history string
	
	returns:
		The current History Hash
	*/
	_self.getCurrentHistory = function() {
		return _currentHash;
	};
	
	/*
	_self.setHistoryFrame = function($frameName) {
		if (!$frameName)
			$frameName = 'unFocusHistoryFrame';
		// _self requires the iframe to have a name attribute, in addition to the id attribute
		// http://www.quirksmode.org/js/iframe.html
		if (!window.frames[$frameName])
			_createHistoryFrame($frameName);
		// 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[$frameName];
	};*/
	
	/* Method: _createAnchor
		Various browsers may need an achor to be present in the dom for the hash to actually be set,
		so we add one every time a history entry is made.
	*/
	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.position = 'absolute';
		$anchor.style.left = getScrollX()+'px';
		$anchor.style.top = getScrollY()+'px';
		//$anchor.style.display = 'none';
		//$anchor.innerHTML = $newHash;
		document.body.insertBefore($anchor,document.body.firstChild);
	};
	// Keeps IE 5.0 from scrolling to the top every time a new history is entered.
	// Also retains the scroll position in the history (not on IE 5.5+).
	function getScrollY() {
		var $scrollPos = 0;
		if(typeof window.pageYOffset == 'number' )
			//Netscape compliant (and Omniweb and Safari)
			$scrollPos = window.pageYOffset;
		else if (!window.document.compatMode || window.document.compatMode == 'BackCompat')
			// IE and IE 6
			$scrollPos = document.body.scrollTop;
		else if ( document.documentElement && document.documentElement.scrollTop )
			// standards compliant mode
			$scrollPos = document.documentElement.scrollTop;
		return $scrollPos;
	};
	// clone getScrollY to getScrollX
	eval(String(getScrollY).toString().replace(/Top/g,'Left').replace(/Y/g,'X'));
	
	/**
	 * These are the platform specific interface methods. Since some platforms (most notably, IE 5.5+)
	 * require almost completely different techniques to create history entries, browser detection is
	 * used and the appropriate method is created. It would be nice to use object or feature detection
	 * here, but these workarounds deal mostly with very specific bugs and other oddities in the 
	 * various implementations. So browser sniffing it is.
	 */
	// IE 5.5 Windows
	if (typeof ActiveXObject != 'undefined' && !window.opera && navigator.userAgent.match(/MSIE (\d\.\d)/)[1] >= 5.5) {
		/*
		method: _createHistoryFrame
			This will create an iframe is none is found (called from <initialize>), and the time 
			has come (if <initialize> has been called.)
			
			This is for IE only for now.
		*/
		function _createHistoryFrame() {
			_historyFrameObj = document.createElement('iframe');
			//_historyFrameObj.setAttribute('name', 'HistoryFrame');
			_historyFrameObj.setAttribute('id', 'HistoryFrame');
			_historyFrameObj.style.position = 'absolute';
			_historyFrameObj.style.top = '-900px';
			document.body.insertBefore(_historyFrameObj,document.body.firstChild);
			// get reference to the frame from frames array (needed for document.open)
			// :NOTE: there might be an issue with this according to quirksmode.org
			// http://www.quirksmode.org/js/iframe.html
			_historyFrameRef = frames['HistoryFrame'];
			
			// add base history entry
			_createHistoryHTML(_currentHash);
		}
		
		/*
		method: _createHistoryHTML
			This is an alternative to <_setHistoryHTML> that is used by IE (and others if I can get it to work).
			This method will create the history page completely in memory, with no need to download a new file
			from the server.
		*/
		function _createHistoryHTML($newHash/*, $replace*/) {
			/*if (typeof $replace != 'undefined')
				$replace = 'replace';
			else $replace = false; // make sure*/
			_historyFrameRef.document.open('text/html');
			_historyFrameRef.document.write('<html><head></head><body onl',
				'oad="parent.unFocus.History.Keeper._updateFromHistory(\''+$newHash+'\');">',
				$newHash+'</body></html>');
			_historyFrameRef.document.close();
		}
		
		_self.addHistory = function($newHash) {
			// do initialization stuff on first call
			_createHistoryFrame();
			
			// replace this function with a new one on first call
			_self.addHistory = function($newHash) { // adds history and bookmark hash
				if (_currentHash != $newHash) {
					// IE will create an entry if there is an achor on the page, but it
					// does not allow you to detect the state change
					_currentHash = $newHash;
					// sets hash and notifies listeners
					_createHistoryHTML($newHash);
				}
			};
			// call the first call
			_self.addHistory($newHash);
		};
		
		// anonymouse method - subscribe to self to update the hash when the history is updated
		_self.addEventListener('historyChange', function($hash) { _setHash($hash) });
		
	} else /*if (!/Safari/.test(navigator.userAgent))*/ {
		_self.addHistory = function($newHash) { // adds history and bookmark hash
			if (_currentHash != $newHash) {
				_createAnchor($newHash);
				_currentHash = $newHash;
				_setHash($newHash);
				_self.notifyListeners('historyChange',$newHash);
			}
		};
	}
};
Keeper.prototype = new unFocus.Utilities.EventManager('historyChange');

return  new Keeper();

})();



function historyHandler () {

	window.onload = function() {
		//document.getElementById('loader').innerHTML = unescape(state);
	};
	
	// this is the public method that will be called to add an entry
	this.addHistory = function(data) {
		unFocus.History.Keeper.addHistory(escape(data));
	};
	
	// the method that will take updates from the History Keeper
	this.onHistoryChange = function(data) {
		//alert('onHistoryChange: ' + pushDeeplink);
		//document.title = 'Learning To Use the Back Button/-' + unescape(data);
		document.title = pageTitle + ' - ' + unescape(data);
		pushDeeplink (data);
	};
	
	// add the event listener
	// :NOTE: onHistoryChange has to be anchored to this object.
	// A private method would not be properly scoped, and could not update state.
	unFocus.History.Keeper.addEventListener('historyChange', this.onHistoryChange);
	
	this.getCurrentState = function() {
		alert(state);
	};
};

	document.historyHandler = new historyHandler ();

function getSWF (movieName) {
		
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    }
    else {
        return document[movieName]
    }
   
}

function pushDeeplink (id) {
  	getSWF("main").pushDeeplink(id);

}