
/************************************************************************
 *																		*
 *		browsernav.js													*
 *																		*
 *			Handles back/forward for browser navigation of flash files	*
 *																		*
 *			To start, call "browsernav.init(flashName)"					*
 *			To change page, call "browsernav.changePage(page, title)"	*
 *																		*
 *																		*
 *		Copyright © 2009 by Josef Guenther								*
 *			All rights reserved.										*
 *																		*
 *			Contact me at:												*
 *			josefguenther@gmail.com										*
 *																		*
 *																		*
 ************************************************************************/



//
// Class
//
var browsernav = ({



	// Variables
	hash: undefined,
	flName: undefined,
	justChanged: false,
	
	init: function (flashName, startBlank)
	{
		// STOP: IE6
		if (isIE6) {
			return false;
		}
		
		
		// start page without hash?
		if (startBlank) {
			if (location.hash) {
				location.hash = "#";
			}
		}
		
		
		this.flName = flashName;
		this.hash = location.hash;
		
		// Setup iframe
		if (isIE) {
			var iframe = this.getiframe();
			iframe.open();
			iframe.close();
			iframe.location.hash = location.hash;
		}
		
		//
		setInterval("browsernav.checkHash()", 100);
		
		return true;
	},

	//
	// Notice hash changes
	//
	
	checkHash: function ()
	{
		var iframe, current_hash;
		
		// get current hash (IE: frome iframe, Other: from browser)
		if (isIE) {
			iframe = this.getiframe();
			current_hash = iframe.location.hash;
		} else {
			current_hash = location.hash;
		}
		
		// compare with current
		if (current_hash != this.hash) {
			
			// set hash
			this.hash = current_hash;
			
			// IE: update the browser location hash to be equal with iframe hash
			if (isIE && location.hash != current_hash) {
				location.hash = current_hash;
			}
			
			if (this.justChanged) {
				// not just changed
				this.justChanged = false;
			} else {
				
				// tell flash
				var ntitle = thisMovie(this.flName).changePage(current_hash.substring(1));
				
				if (ntitle) {
					this.setTitle(ntitle);
				} else {
					// if there is no hash, we must be back at the beginning, so set the title back to default
					if (!current_hash || current_hash == "#") {
						helpSubPage = '';
						document.title = pageName;
					}
				}
			}
		}
	},
	
	
	//
	// changePage - Called when page should be changed
	//
	changePage: function (pName, pTitle, flName)
	{
		// Forward/Back?
		if (pName == "-1") {
			history.go(-1);
			return;
		} else if (pName == "1") {
			history.go(1);
			return;
		}
		
		// record
		this.hash = pName;
		
		// set hash
		location.hash = pName;
		if (isIE) {
			var iframe = this.getiframe();
			iframe.open();
			iframe.close();
			iframe.location.hash = pName;
		}
		
		// set title
		this.setTitle(pTitle);
		
		// don't alert flash if it was just changed by flash
		if (flName == this.flName) {
			this.justChanged = true;
		}
	},
	
	setTitle: function (txt) {
		// set
		if (txt) {
			if (txt == "-") {
				document.title = pageName;
			} else {
				helpSubPage = txt;
				document.title = "equiseller.com | " + txt;
			}
		}
	},
	
	//
	// getiframe - Returns a reference to the iframe (for IE)
	//
	getiframe: function () {
		var ihistory = document.getElementById("bnav_history");
		return ihistory.contentWindow.document;
	}
});
	

