Client = new Object();
Client.getBrowser = function() 
{ return window.navigator.appName.toLowerCase(); }
Client.isIE = function()
{ return (Client.getBrowser().indexOf("microsoft") != -1); }
Client.setTitle = function(strTitle)
{ top.document.title = strTitle; }


// called to get initial state into movie
function onStartUp() {
	HistoryManager.onPageRefresh(true);
}

// wrapper func for direct call from flash
function updateHistory(strS) {
	HistoryManager.updateHistory(strS);
}

HistoryManager = new Object();
HistoryManager.pgTitleCache = new Object();
HistoryManager.isStartUp = true;
HistoryManager.intrvlID = undefined;
HistoryManager.swfID = undefined;
HistoryManager.swfObj = undefined;
HistoryManager.oldHVal = undefined;


HistoryManager.init = function(swfID, defaultPageTitle) 
{
	this.oldHVal = location.hash;
	this.pgTitleCache["defaultTitle"] = defaultPageTitle;
	
	this.isStartUp = true
	
	// All that's needed for non-ie browsers as the history 
	// can be update withought the need for a named anchor
	// Note: "watch" dosen't detect <--> navigation 
	if(!Client.isIE()) {
		window.onfocus = HistoryManager.onWindowFocus;
		window.onblur = HistoryManager.onWindowBlur;
		HistoryManager.onWindowFocus();
	}
	else HistoryManager.embedHistoryFrame();

	this.swfID = swfID;
	this.swfObj = Client.isIE() ? window[this.swfID] : document[this.swfID];
}

HistoryManager.onWindowFocus = function()
{
	clearInterval(HistoryManager.intrvlID);// double check cancel
	HistoryManager.intrvlID = setInterval(HistoryManager.onHashChange, 100);
}

HistoryManager.onWindowBlur = function()
{ clearInterval(HistoryManager.intrvlID); }

HistoryManager.onPageRefresh = function(isStartUp) 
{
	var search = location.hash.slice(1);
	if(search != null && search != "" && HistoryManager.pgTitleCache[search] != undefined) {
		Client.setTitle(HistoryManager.pgTitleCache[search]);
	}
	else Client.setTitle(HistoryManager.pgTitleCache["defaultTitle"]);

	this.swfObj.handleHistoryChange(search, isStartUp);
}

// Non-IE only. Check for change to hash notify swf if found
HistoryManager.onHashChange = function() 
{
	if(this.oldHVal == location.hash) return;

	this.oldHVal = location.hash;
	HistoryManager.onPageRefresh(HistoryManager.isStartUp);
}

// Called from movie to update state 
HistoryManager.updateHistory = function(strS) 
{
	if(strS == undefined) return;
	this.isStartUp = false;
	
	strS = HistoryManager.handleTitleChange(strS);
	
	// because IE needs a named anchor to update the history
	// we need to use a seperate frame to change its state
	// this in turn will update the hash
	if(Client.isIE()) {
		var f = frames["fHistory"].location;
		var p = f.protocol+"//"+f.host+f.pathname+"?hash="+strS;
		frames["fHistory"].location = p;
	}
	else location.hash = '#'+strS; 
}

HistoryManager.handleTitleChange = function(strS) 
{
	var tBegin = strS.indexOf("pgtitle((");
	var tEnd = strS.indexOf("))");
	
	if(tBegin != -1 && tEnd != -1) {
		var title = strS.slice(tBegin+9, tEnd);
		var search = strS.slice(0, tBegin)+strS.slice(tEnd+2);
		HistoryManager.pgTitleCache[search] = title;
		Client.setTitle(title);
		return search;
	}
	return strS;
}

HistoryManager.embedHistoryFrame = function()
{
	document.write('<iframe id="fHistory" name="fHistory" src="history.html?hash=init" scrolling="no" width="0" height="0" border="0"></iframe>');
}