
function isUserLoggedIn() {
	// 
	// returns true if the user is logged in.   Returns false if
	// the user is not logged in.
	//

	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
	    xmlhttp=new XMLHttpRequest();
	} else {// code for IE6, IE5
	    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.open("GET","backend/user.php?action=get_user",false);
	xmlhttp.send();
	xmlDoc=xmlhttp.responseXML; 

	ret = false;

	elem=xmlDoc.getElementsByTagName("ISLOGGEDIN")
	if (elem.length > 0) {
		ret = (elem[0].childNodes[0].nodeValue == "yes") ? true : false;
	}
	
	return ret;
}

function checkPromoCode(code, arg) {
	// console.log("checkPromoCode "+code);
	var topNav = parent.document.getElementById("flashNavDropDown");
	topNav.checkPromoCode(code, arg);
	return 1;  // return 1 so that flash can tell if this call happened
}

function enlargeNavDiv() {
	// console.log("enlargeNavDiv");
	var topNav = document.getElementById("dropDownWapper");
	topNav.style.height = "671px";
	topNav.style.zIndex = 10;

	try {
		// workaround for broken safari/mac/powerpc flash over flash
		// flickering cursor bug.
		var iframe = getIFrameDocument("fooo");
		var flashContent = iframe.getElementById("flashContent");
		if (flashContent) {
			// console.log("mouseEnabled false");
			flashContent.mouseEnabled(false);
		}
	} catch(e) {
		// console.log("enlargeNavDiv "+e);
	}
}

function collapseNavDiv() {
	// console.log("collapseNavDiv");
	var topNav = document.getElementById("dropDownWapper");
	topNav.style.height = "91px";

	try {
		// workaround for broken safari/mac/powerpc flash over flash
		// flickering cursor bug.
		var iframe = getIFrameDocument("fooo");
		var flashContent = iframe.getElementById("flashContent");
		if (flashContent) {
			// console.log("mouseEnabled true");
			flashContent.mouseEnabled(true);
		}
	} catch(e) {
		// console.log("enlargeNavDiv "+e);
	}
}

function gameHelp() {
	try {
		var iframe = getIFrameDocument("fooo");
		var flashContent = iframe.getElementById("flashContent");
		if (flashContent) {
			// console.log("showGameHelp being called....");
			flashContent.showGameHelp();
		}
	} catch(e) {
		// console.log("enlargeNavDiv "+e);
	}
}

function gameMute() {
	try {
		var iframe = getIFrameDocument("fooo");
		var flashContent = iframe.getElementById("flashContent");
		if (flashContent) {
			// console.log("muteGame being called....");
			flashContent.muteGame();
		}
	} catch(e) {
		// console.log("enlargeNavDiv "+e);
	}
}

function gameUnmute() {
	try {
		var iframe = getIFrameDocument("fooo");
		var flashContent = iframe.getElementById("flashContent");
		if (flashContent) {
			// console.log("unmuteGame being called....");
			flashContent.unmuteGame();
		}
	} catch(e) {
		// console.log("enlargeNavDiv "+e);
	}
}


function sitePageLoadDone() {
	// console.log("sitePageLoadDone");
	var topNav = parent.document.getElementById("flashNavDropDown");
	topNav.hideLoader();
	collapseNavDiv();
	return 1;  // return 1 so that flash can tell if this call happened
}

function sitePageLoadProgress(val) {
	// console.log("sitePageLoadingProgress "+val);
	var topNav = parent.document.getElementById("flashNavDropDown");
	topNav.loadingProgress(val);
	return 1;  // return 1 so that flash can tell if this call happened
}

function navMute(val) { // true or false
	// console.log("sitePageLoadingProgress "+val);
	var topNav = parent.document.getElementById("flashNavDropDown");
	topNav.mute(val);
	return 1;  // return 1 so that flash can tell if this call happened
}

function getIFrameDocument(pId)
{
	// console.log("getIFrameDocument");

	var rv = null;
	var iFrame = document.getElementById(pId);

	if( iFrame.contentDocument )
	{
		// console.log("FF");
		rv = iFrame.contentDocument;
	}
	else
	{
		// console.log("IE");
		rv = document.frames[pId].document;
	}

	// console.log(rv);
	return rv;
}


previousLocation = null;

function updateFromLocation()  {
	var playerVersion = swfobject.getFlashPlayerVersion().major;

	// console.log("updateFromLocation "+parent.window.location+" "+previousLocation+" "+playerVersion);

	if ((playerVersion < 10) || (topNavIsReady && bottomNavIsReady)) {
		var newLocation = parent.window.location;

		if (previousLocation != newLocation)  {
			// alert("updateFromLocation change "+newLocation+" "+previousLocation);
			gotoSitePage2(playerVersion);
		}
	} else {
		// console.log("updateFromLocation: waiting for navs");
	}

	setTimeout("updateFromLocation()",100);
}

function gotoSitePageRegardless(quasiURL) {
	//
	// called from flash's externalInterface() function or by the
	// user selecting a bookmark
	//

	// console.log("gotoSitePage "+quasiURL);
	parent.window.location.hash = quasiURL;
  	parent.previousLocation = null;

	return 1; // return a value so that flash can detect whether or not this function actually got called.
}

function gotoSitePage(quasiURL) {
	//
	// called from flash's externalInterface() function or by the
	// user selecting a bookmark
	//

	// console.log("gotoSitePage "+quasiURL);

	try {
		var iframe = getIFrameDocument("fooo");
		var flashContent = iframe.getElementById("flashContent");
		if (flashContent) {
			// allow any flash app the chance to respond to navigateAway
			flashContent.navigateAway(quasiURL.substr(1));
		}
		else {
			gotoSitePageRegardless(quasiURL);
		}
	} catch(e) {
		// flash app didn't implement the navigateAway function
		gotoSitePageRegardless(quasiURL);
	}
	return 1; // return a value so that flash can detect whether or not this function actually got called.
}

function gotoSitePage2(playerVersion) {
	//
	// called only from updateFromLoacation()
	//

	// console.log("gotoSitePage2 called "+parent.window.location.hash+" "+PH_app);

	var elem = parent.document.getElementById("fooo");
	if (! elem) {
		// iframe element not ready yet. Will be retried automatically
		// because this function is returning early.
		// console.log("gotoSitePage2 - can't find fooo");
		return;
	}


	parseLocationHash();

	// console.log("gotoSitePage2 called "+parent.window.location.hash+" "+PH_app);

	var topNav = parent.document.getElementById("flashNavDropDown");

	if (playerVersion >= 10) {
		if (PH_useNavLoadScreen && playerVersion) {
			try {
				topNav.showLoader();
				enlargeNavDiv();
			} catch(err) {
				// console.log("ERROR: " + err);
				// console.log("gotoSitePage2 show loader error: "+err);
				//
				// top nav is not ready yet. Will be retried automatically
				// because this function is returning early.
				//
				// console.log("gotoSitePage2 - topNav not yet ready");
				return;
			}
		} else {
			//
			// just in case we navigated to a new page before the old page
			// was done loading,  hide the loading screen that may be
			// left over.
			//
			try {
				topNav.hideLoader();
			} catch(err) {
				// console.log("gotoSitePage2 show loader error: "+err);
				//
				// top nav not ready yet.   There is no need to return early
				// (and cause a retry) because if the top nav isn't ready, there
				// was no way for it to have deployed the loading screen.
				//
				// console.log("gotoSitePage2 - topNav not yet ready, loader screen");
			}
		}
		updateNav();
	}

	// appIDParam = "&appID=" + appID;
	// PH_app = PH_app + appIDParam;

	// console.log("gotoSitePage2 "+PH_app);
	PH_prevHash = parent.window.location.hash;
//
//	if (elem.contentDocument) {
//		// Forward/back button behave correctly everywhere but IE
//		elem.contentDocument.location.replace(PH_app);
//	} else {
//		// code for IE version  less than 8.
//		// Forward/back button are broken here too.
//		elem.src = PH_app;
//	}
//

	// alert("new PH_app "+PH_app);

	var contentDoc = getIFrameDocument("fooo");
	contentDoc.location.replace(PH_app); // even using replace, history in IE8 is still wrong (but IE7 works)

	document.title = PH_title;
	trackPage(PH_pageTrackID);

	configureBottomPromos();

	previousLocation = ""+parent.window.location;

	window.scrollTo(0,0);
}

function configureBottomPromos() {

	if (PH_title.indexOf("Parents") != -1) {
		hideKidsPromos();
	} else if (PH_title.indexOf("Teachers") != -1) {
		hideKidsPromos();
	} else {
		setKidsPromos();
	}
}

function unMuddleBookName(name) {
	var ret = ""
	for (i=0; i<name.length; i++) {
		var c = name.charAt(i);
		if (c == '_') ret += ' ';
		else ret += c;
	}
	return 	ret;
}

//
// variables set via parseLocationHash().
//
var PH_app = null;
var PH_prevHash = "#intro";
var PH_title = null;
var PH_pageTrackID = null;
var PH_navHighLight = null;
var PH_useNavMute = null;
var PH_musicState = null;
var PH_helpState = null;
var PH_complexityState = null;
var PH_promoRewardState = null;
var PH_useNavLoadScreen = false;
var PH_sitePage = "";

var homeCalledCnt = 0;
var loginIntroCalledCnt = 0;

function parseLocationHash() {

	var hash = parent.window.location.hash;

	if (!parent.window['mobileBrowser']) {
	  mobileBrowser = false;
	}
	// console.log("parseHash: "+hash);

	// while (hash.charAt(hash.length-1) == "/") {
		// // remove trailing slashes
		// hash = hash.substr(0, hash.length-1);
	// }

	PH_title = "Magic Tree House";
	PH_pageTrackID = null;
	PH_sitePage = null;
	var sitePageArgs = "";
	var sitePage  = hash.substr(1);
	// So that php scripts can read the parameters, we must strip the #
	var prevPageRef = PH_prevHash.replace('#', '');
	
	var ndx = sitePage.indexOf("?");
	if (ndx >= 0) {
		sitePageArgs = sitePage.substr(ndx+1);
		sitePage = sitePage.substr(0, ndx);
	}

	if (sitePageArgs != "") {
		sitePageArgs = "&"+sitePageArgs;
	}

	if (sitePage.substr(0,1) == "/") {
		sitePage = sitePage.substr(1);
	}


	var bookDetailString = "bookdetail-";
	var teachersString = "teachers-";
	var parentsString = "parents-";
	var promotionsString = "promotions-";

	if (sitePage.substr(0,bookDetailString.length).toLowerCase() == bookDetailString) {
		var book = sitePage.substr(bookDetailString.length);
		PH_app = "iframe_swf.html?BookDetail.swf?book="+book+"";
		book = unMuddleBookName(book);
		PH_title += ": Book Detail : "+book;
		PH_pageTrackID += ": Book Detail : "+book;
		PH_navHighLight = "my_library";
		PH_useNavLoadScreen = true;
		PH_musicState = "on";
		PH_useNavMute = "yes";
		PH_helpState = "off";
		PH_complexityState = "complex";
		PH_promoRewardState = "off";
		PH_sitePage = "bookdetail";
	} else if (sitePage.substr(0,teachersString.length).toLowerCase() == teachersString) {
		// Teachers pages
		var tail = sitePage.substr(teachersString.length);

		if (sitePage.indexOf("-guide-") != -1) {
			PH_app = "teachersclub/resource_center/pdfs/"+sitePage+".html?noop=1";
			PH_title += ": Teachers Club Resource Center Book Guide : " + tail;
		} else if (sitePage.indexOf("newsletter_signup") != -1) {
			PH_app = "./t_newsletter_signup.php?noop=1";
		} else {
			PH_app = "./t_"+tail+".html?noop=1";
			PH_title += ": Teachers Club Resource Center Featured Article : " + sitePage;
		}
		PH_navHighLight = "for_teachers";
		PH_useNavLoadScreen = false;
		PH_musicState = "off";
		PH_useNavMute = "yes";
		PH_helpState = "off";
		PH_complexityState = "complex";
		PH_promoRewardState = "off";
	} else if (sitePage.substr(0,parentsString.length).toLowerCase() == parentsString) {
		// Parents pages
		var tail = sitePage.substr(parentsString.length).toLowerCase();
		switch(tail) {
			// We could allow html pages to process PHP...
			case 'newsletter_signup':
				PH_app = "./p_newsletter_signup.php?noop=1";
			break;
			default:
				PH_app = "./p_"+tail+".html?noop=1";
			break;
		}
		PH_title += ": For Parents : "+sitePage;
		PH_navHighLight = "for_parents";
		PH_useNavLoadScreen = false;
		PH_musicState = "off";
		PH_useNavMute = "yes";
		PH_helpState = "off";
		PH_complexityState = "complex";
		PH_promoRewardState = "off";
	} else if (sitePage.substr(0,promotionsString.length).toLowerCase() == promotionsString) {
		// promotions pages
		var tail = sitePage.substr(promotionsString.length).toLowerCase();
		PH_app = "./promotions/"+tail+"/index.html?noop=1";
		// PH_title += ": "+sitePage;
		// the specific promotions page is responsible for setting the parent's document title
		PH_navHighLight = null;
		PH_useNavLoadScreen = false;
		PH_musicState = "off";
		PH_useNavMute = "yes";
		PH_helpState = "off";
		PH_complexityState = "complex";
		PH_promoRewardState = "off";
	} else {
		sitePage = sitePage.toLowerCase();
		switch(sitePage) {
			case 'press':
				PH_app = "promotions/digital-press-kit/media-home.html?noop=1";
				PH_title += ": Press";
				PH_navHighLight = null;
				PH_useNavLoadScreen = false;
				PH_musicState = "off";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "simple";
				PH_promoRewardState = "off";
				break;
			case 'media_assets':
				PH_app = "promotions/digital-press-kit/media-assets.html?noop=1";
				PH_title += ": Digital Press Kit : Media Assets";
				PH_navHighLight = null;
				PH_useNavLoadScreen = false;
				PH_musicState = "off";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "simple";
				PH_promoRewardState = "off";
				break;
			case 'neighborhood_activities':
				PH_app = "promotions/neighborhood-activities/neighborhood-activities.html?noop=1";
				PH_title += ": Neighborhood Activities";
				PH_navHighLight = "for_parents";
				PH_useNavLoadScreen = false;
				PH_musicState = "off";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'live_tour':
				PH_app = "promotions/live-tour/index.html?noop=1";
				PH_title += ": Live Tour";
				PH_navHighLight = null;
				PH_useNavLoadScreen = false;
				PH_musicState = "off";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'my_magic_tree_house':
				PH_app = "iframe_swf.html?myMTH.swf?noop=1";
				PH_title += ": My Magic Tree House";
				PH_navHighLight = "my_magic_tree_house";
				PH_useNavLoadScreen = true;  // enable when real swf is ready
				PH_musicState = "on";
				PH_useNavMute = "yes";
				PH_helpState = "on";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'gallery':
				PH_app = "iframe_swf.html?myMTH.swf?mode=gallery";
				PH_title += ": Gallery";
				PH_navHighLight = "my_magic_tree_house";
				PH_useNavLoadScreen = true;  // enable when real swf is ready
				PH_musicState = "on";
				PH_useNavMute = "yes";
				PH_helpState = "on";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'my_passport':
				PH_app = "iframe_swf.html?passport.swf?noop=1";
				PH_title += ": My Passport";
				PH_navHighLight = "my_library";
				PH_useNavLoadScreen = true;
				PH_musicState = "on";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'my_wishlist':
				PH_app = "iframe_swf.html?wishlist.swf?noop=1";
				PH_title += ": My Wishlist";
				PH_navHighLight = "my_library";
				PH_useNavLoadScreen = true;
				PH_musicState = "on";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'about_the_magic_tree_house':
				if (mobileBrowser) { window.location.replace("ip_about_the_books.php"); }
				PH_app = "iframe_swf.html?About.swf?noop=1";
				PH_title = "About the Magic Tree House";
				PH_navHighLight = "about_the_magic_tree_house";
				PH_useNavLoadScreen = true;
				PH_musicState = "on";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'about_the_series':
				if (mobileBrowser) { window.location.replace("ip_about_the_series.php"); }
				PH_app = "iframe_swf.html?About.swf?page=series";
				PH_title += ": About the Books";
				PH_navHighLight = "about_the_magic_tree_house";
				PH_useNavLoadScreen = true;
				PH_musicState = "on";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'about_the_authors':
				if (mobileBrowser) { window.location.replace("ip_about_the_authors.php"); }
				PH_app = "iframe_swf.html?About.swf?page=authors";
				PH_title += ": About the Authors";
				PH_navHighLight = "about_the_magic_tree_house";
				PH_useNavLoadScreen = true;
				PH_musicState = "on";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'about_the_illustrator':
				if (mobileBrowser) { window.location.replace("ip_about_the_illustrator.php"); }
				PH_app = "iframe_swf.html?About.swf?page=illustrator";
				PH_title += ": About the Illustrator";
				PH_navHighLight = "about_the_magic_tree_house";
				PH_useNavLoadScreen = true;
				PH_musicState = "on";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'tree_house_on_stage':
				if (mobileBrowser) { 
					window.location.replace("ip_on_stage.php"); 
				} else {
					//PH_app = "iframe_swf.html?OnStage.swf?noop=1";
					PH_app = "on_stage.php?noop=1&prevhash="+prevPageRef;
					PH_title = "Magic Tree House On Stage";
					PH_navHighLight = "tree_house_on_stage";
					PH_useNavLoadScreen = false;
					PH_musicState = "off";
					PH_useNavMute = "yes";
					PH_helpState = "off";
					PH_complexityState = "complex";
					PH_promoRewardState = "off";
				}
				break;
			case 'my_library':
			case 'read_a_sample':
				if (mobileBrowser) { window.location.replace("teachers/resource_center/ip_library.php"); }
				PH_app = "iframe_swf.html?Library.swf?noop=1";
				PH_title += ": My Library";
				PH_navHighLight = "my_library";
				PH_useNavLoadScreen = true;
				PH_musicState = "on";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'mission_rewards':
				PH_app = "iframe_swf.html?MissionRewards.swf?noop=1";
				PH_title += ": Mission Rewards";
				PH_navHighLight = "mission_rewards";
				PH_useNavLoadScreen = true;
				PH_musicState = "on";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "on";
				break;
			case 'more_videos':
				if (mobileBrowser) { window.location.replace("ip_index.php"); }
				PH_app = "more_videos_page.php?noop=1";
				PH_title += ": More Videos";
				PH_navHighLight = "tree_mail";
				PH_useNavLoadScreen = false;
				PH_musicState = "off";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'tree_mail':
				if (mobileBrowser) { window.location.replace("ip_index.php"); }
				PH_app = "tree_house_mail.php?noop=1";
				PH_title += ": Tree House Mail";
				PH_navHighLight = "tree_mail";
				PH_useNavLoadScreen = true;
				PH_musicState = "off";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'account':
				PH_app = "p_account_settings.php?prevhash="+prevPageRef;
				PH_title += ": Account Settings";
				PH_navHighLight = null;
				PH_useNavLoadScreen = false;
				PH_musicState = "off";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'account_email_saved':
				PH_app = "p_account_settings_email_saved.php?prevhash="+prevPageRef;
				PH_title += ": Account Settings";
				PH_navHighLight = null;
				PH_useNavLoadScreen = false;
				PH_musicState = "off";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'account_settings_saved':
				PH_app = "p_account_settings_saved.php?prevhash="+prevPageRef;
				PH_title += ": Account Settings";
				PH_navHighLight = null;
				PH_useNavLoadScreen = false;
				PH_musicState = "off";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'account_error':
				PH_app = "p_account_settings_error.php?prevhash="+prevPageRef;
				PH_title += ": Account Settings";
				PH_navHighLight = null;
				PH_useNavLoadScreen = false;
				PH_musicState = "off";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'account_delete':
				PH_app = "p_account_settings_confirm_delete.php?prevhash="+prevPageRef;
				PH_title += ": Account Settings";
				PH_navHighLight = null;
				PH_useNavLoadScreen = false;
				PH_musicState = "off";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'logout':
				PH_app = "backend/logout.php?prevhash="+prevPageRef;
				PH_title += ": Logout";
				PH_navHighLight = null;
				PH_useNavLoadScreen = false;
				PH_musicState = "on";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'password_reset':
				PH_app = "backend/password_reset.php?prevhash="+prevPageRef;
				PH_title += ": Login Password Reset";
				PH_navHighLight = null;
				PH_useNavLoadScreen = false;
				PH_musicState = "on";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'login':
				PH_app = "backend/login.php?prevhash="+prevPageRef;
				PH_title += ": Login";
				PH_navHighLight = null;
				PH_useNavLoadScreen = false;
				PH_musicState = "on";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'login_leader_board':
				PH_app = "backend/login.php?prevhash=leader_board";
				PH_title += ": Login";
				PH_navHighLight = null;
				PH_useNavLoadScreen = false;
				PH_musicState = "on";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'login_account':
				PH_app = "backend/login.php?account=true&new=true&prevhash="+prevPageRef;
				PH_title += ": Login";
				PH_navHighLight = null;
				PH_useNavLoadScreen = false;
				PH_musicState = "off";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'join':
				PH_app = "backend/join.php?prevhash="+prevPageRef;
				PH_title += ": Join Now";
				PH_navHighLight = null;
				PH_useNavLoadScreen = false;
				PH_musicState = "on";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'for_parents':
				PH_app = "p_about_the_series.php?noop=1";
				PH_title += " For Parents: About the Series";
				PH_navHighLight = "for_parents";
				PH_useNavLoadScreen = false;
				PH_musicState = "off";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'for_teachers':
				PH_app = "t_landing.php?noop=1";
				PH_title += " Teachers Club";
				PH_navHighLight = "for_teachers";
				PH_useNavLoadScreen = false;
				PH_musicState = "off";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'teachers_resource_center':
				// PH_app = "t_resource_center.html?noop=1";
				PH_app = "teachersclub/resource_center?noop=1";
				PH_title += " Teachers Club: Resource Center";
				PH_navHighLight = "for_teachers";
				PH_useNavLoadScreen = false;
				PH_musicState = "off";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'teachers_forum':
				// PH_app = "t_ideasForum.html?noop=1";
				PH_app = "teachersclub/forums?noop=1";
				PH_title += " Teachers Club: Classroom Idea Forum";
				PH_navHighLight = "for_teachers";
				PH_useNavLoadScreen = false;
				PH_musicState = "off";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'game':
				if (mobileBrowser) { window.location.replace("ip_index.php"); }
				PH_app = "iframe_swf.html?game.swf?noop=1";
				PH_title += " Game";
				PH_navHighLight = "game";
				PH_useNavLoadScreen = false;
				PH_musicState = "off";
				PH_useNavMute = "no";
				PH_helpState = "on";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'fact_tracker_showdown':
				if (mobileBrowser) { window.location.replace("ip_index.php"); }
				PH_app = "iframe_swf.html?FactTracker.swf?noop=1";
				PH_title += " Fact Tracker Showdown";
				PH_navHighLight = "game";
				PH_useNavLoadScreen = false; // enable when real swf is ready
				PH_musicState = "off";
				PH_useNavMute = "no";
				PH_helpState = "on";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'leader_board':
				if (mobileBrowser) { window.location.replace("ip_index.php"); }
				PH_app = "iframe_swf.html?LeaderBoard.swf?noop=1";
				PH_title += " Leader Board";
				PH_navHighLight = "game";
				PH_useNavLoadScreen = true; // enable when real swf is ready
				PH_musicState = "on";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
			case 'intro':
			case 'home':
			default:
				if (mobileBrowser) { window.location.replace("ip_index.php"); }

				if (isUserLoggedIn() || (loginIntroCalledCnt > 0)) {
					PH_app = "iframe_swf.html?Home.swf?noop=1";
					if (homeCalledCnt == 0) PH_app += "&intro=1";
					PH_title += ": Home";
					PH_navHighLight = "home";
					PH_useNavLoadScreen = true;
					PH_musicState = "on";
					PH_useNavMute = "yes";
					PH_helpState = "off";
					PH_complexityState = "complex";
					PH_promoRewardState = "on";
					homeCalledCnt++;
				}
				else {
					PH_app = "iframe_swf.html?LoginIntro.swf?noop=1";
					PH_title += ": Login";
					PH_navHighLight = "home";
					PH_useNavLoadScreen = true;
					PH_musicState = "on";
					PH_useNavMute = "yes";
					PH_helpState = "off";
					PH_complexityState = "complex";
					PH_promoRewardState = "off";
					loginIntroCalledCnt++;
				}
				break;
			case 'twenty_year':
				if (mobileBrowser) { window.location.replace("ip_index.php"); }
				PH_app = "iframe_swf.html?TwentyYear.swf?noop=1";
				PH_title += ": Twenty Year Anniversary";
				PH_navHighLight = "home";
				PH_useNavLoadScreen = true;
				PH_musicState = "on";
				PH_useNavMute = "yes";
				PH_helpState = "off";
				PH_complexityState = "complex";
				PH_promoRewardState = "off";
				break;
		}
	}


	if (mobileBrowser) {
		if ((PH_app.indexOf(".swf") > -1) || (PH_app.indexOf(".php") > -1)) {
			window.location.replace("ip_index.php");				
		}
	}

	if (! PH_pageTrackID) PH_pageTrackID = PH_title;

	PH_app += sitePageArgs;

	if (! PH_sitePage) {
		PH_sitePage = sitePage;
	}
}

bottomNavIsReady = false;
topNavIsReady = false;

function bottomNavReady() {
	bottomNavIsReady = true;
	// console.log("bottom nav is ready");
	updateNav();
}

function topNavReady() {
	// console.log("top nav is ready");
	topNavIsReady = true;
	updateNav();
}

function updateNav() {
	// console.log("updateNav "+topNavIsReady+" "+bottomNavIsReady+" "+PH_app);
	if (!(topNavIsReady && bottomNavIsReady && PH_app)) return;

	var topNav = parent.document.getElementById("flashNavDropDown");
	var botNav = parent.document.getElementById("flashFooter");

	try {
		if (topNav) topNav.configure(PH_useNavMute, PH_musicState, PH_complexityState, PH_promoRewardState, PH_navHighLight, PH_helpState);
		if (botNav) botNav.configure(PH_useNavMute, PH_musicState, PH_complexityState, PH_promoRewardState, PH_navHighLight, PH_helpState);
	} catch(e) {
		// console.log("updateNav: "+e);
	}
}

//////////////////// support for populating bottom and side ad bars ///////////////////////

// DEFINED IN data/ad_config.js
var adsConfig = [];

bottomAdSpec = new Array();
bottomAdOneSpec = null;
bottomAdTwoSpec = null;
bottomAdThreeSpec = null;
bottomAdReloadCnt = 0;

sideAdSpec = new Array();
sideAdOneSpec = null;
sideAdTwoSpec = null;
sideAdReloadCnt = 0;

function bottomAdOneClick()   { eval(bottomAdOneSpec[1]); }
function bottomAdTwoClick()   { eval(bottomAdTwoSpec[1]); }
function bottomAdThreeClick() { eval(bottomAdThreeSpec[1]); }

function sideAdOneClick()     { eval(sideAdOneSpec[1]); }
function sideAdTwoClick()     { eval(sideAdTwoSpec[1]); }

/*
function bottomAdSpecForPage(pageName, specArray) {
	//
	// prepend "XYZ_" to the pageName so that pageNames do not conflict
	// with builtin methods/properties (e.g. "join") of the Array object.
	//
	bottomAdSpec["XYZ_"+pageName] = specArray;
}

function sideAdSpecForPage(pageName, specArray) {
	//
	// prepend "XYZ_" to the pageName so that pageNames do not conflict
	// with builtin methods/properties (e.g. "join") of the Array object.
	//
	sideAdSpec["XYZ_"+pageName] = specArray;
}
*/

function getAdsBySection(section) 
{
	return adsConfig[section];
}

function setSidePromoAd(pSlot, pURL) 
{
	/******************************************************
		Sets the side promo ads within the iframe contents. 

		pSlot is promo slot index, valid values are 1, 2.
		pURL is path to promo ad image, NULL will hide the promo
	*******************************************************/

	var zSlot = (( pSlot < 1 ) || ( pSlot > 2 )) ? 1 : pSlot;

	var zPromo = document.getElementById( "promo_side_" + zSlot );

	if( !pURL && zPromo ) // URL is null && promo element found
	{
		// console.log("side !pURL && zPromo");
		zPromo.style.display = "none";
	}
	else if ( zPromo && pURL != "" )
	{
		// console.log("setSidePromoAd "+pSlot+" "+pURL);
		// console.log('side zPromo && pURL != ""');
		zPromo.src = pURL;
		zPromo.style.display = "inline";
	}
	else {
		// console.log("side nothing "+zPromo+" "+pURL);
	}
}

function setBottomPromoAd( pSlot, pURL, pLink )
{
	/******************************************************
		Sets the bottom promo ads on the outer 'parent'

		pSlot is promo slot index, valid values are 1, 2, 3.
		pURL is path to promo ad image, NULL will hide the promo
		pLink is the onclick javascript for the ad image, assumed NULL if pURL is NULL
	*******************************************************/

	var zSlot = (( pSlot < 1 ) || ( pSlot > 3 )) ? 1 : pSlot;

	var zPromo = document.getElementById( "promo_bottom_" + zSlot );

	var zLink;

	if( !pURL && zPromo ) // URL is null && promo element found
	{
		// console.log("bottom !pURL && zPromo");
		zPromo.style.display = "none";
	}
	else if ( zPromo && (pURL !== null) )
	{
		// console.log('bottom zPromo && pURL != ""');
		// console.log("setBottomPromoAd "+pSlot+" "+pURL);
		zPromo.src = pURL;
		zPromo.style.display = "inline";
	}
	else {
		// console.log("bottom nothing "+zPromo+" "+pURL);
	}

}

function hideKidsPromos() 
{
	setBottomPromoAd(1, null);
	setBottomPromoAd(2, null);
	setBottomPromoAd(3, null);
}

function setKidsPromos() 
{
	//
	// called from top level wrapper context
	//
	var adSpec = getAdsBySection(PH_sitePage);

	if (adSpec == undefined) {
		if (mobileBrowser) {
			adSpec = getAdsBySection("mobile_wrapper_promo");
		} else {
			adSpec = getAdsBySection("kids_wrapper_promo"); // Default
		}
	}

	if (adSpec == undefined)  {
		setBottomPromoAd(1, null);
		setBottomPromoAd(2, null);
		setBottomPromoAd(3, null);
		return;
	}

	numAds = adSpec.length;

	// console.log("numAds "+numAds);

	bottomAdOneSpec   = adSpec[bottomAdReloadCnt++ % numAds];
	bottomAdTwoSpec   = adSpec[bottomAdReloadCnt++ % numAds];
	bottomAdThreeSpec = adSpec[bottomAdReloadCnt++ % numAds];

	// console.log("bottomAdOneSpec "+bottomAdOneSpec[0]);
	// console.log("bottomAdTwoSpec "+bottomAdTwoSpec[0]);
	// console.log("bottomAdThreeSpec "+bottomAdThreeSpec[0]);

	setBottomPromoAd(1, bottomAdOneSpec[0], bottomAdOneSpec[1]);
	setBottomPromoAd(2, bottomAdTwoSpec[0], bottomAdTwoSpec[1]);
	setBottomPromoAd(3, bottomAdThreeSpec[0], bottomAdThreeSpec[1]);
}

function setAdultsPromos(prefix /* e.g. "../../" */, which/* "parents" or "teachers" */) 
{
	//
	// called from iframe contents context
	//
	var adSpec = getAdsBySection(which + "_wrapper_promo");

	if (adSpec == undefined)  return;

	numAds = adSpec.length;

	// console.log("numAds "+numAds);

	sideAdOneSpec   = adSpec[sideAdReloadCnt++ % numAds];
	sideAdTwoSpec   = adSpec[sideAdReloadCnt++ % numAds];

	// console.log("sideAdOneSpec "+sideAdOneSpec[0]);
	// console.log("sideAdTwoSpec "+sideAdTwoSpec[0]);

	setSidePromoAd(1, prefix+sideAdOneSpec[0]);
	setSidePromoAd(2, prefix+sideAdTwoSpec[0]);
}


////////// ominture support ////////////////

// var s_account="ranhmagictreehouse,ranhrollup";
var s_account="ranhrollup";
var rh_division="RH Childrens Books";
var rh_imprint="";

function trackPage(pageName) {
	try {
		/* You may give each page an identifying name, server, and channel on
		the next lines. */
		s.rhEan="__###REPLACE###.ean.safe__";
		s.pageName=pageName;
		s.server="";
		s.channel="";
		s.pageType="";
		s.prop1="__###REPLACE###.ti_division.safe__";
		s.prop2="__###REPLACE###.category_display.safe__";
		s.prop3="";
		s.prop4="";
		s.prop5="__###REPLACE###.imprint.safe__";
		s.prop6="__###REPLACE###.title.safe__";
		s.prop7="__###REPLACE###.author_display.safe__";
		s.prop8="__###REPLACE###.media.safe__";
		s.prop9="";
		s.prop10="__###REPLACE###.query_title_subtitle_auth_isbn__";
		s.prop11="__###REPLACE###.num_of_matches__";
		/* Conversion Variables */
		s.campaign="";
		s.events="";
		s.eVar1="__###REPLACE###.query_title_subtitle_auth_isbn__";
		/************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/
		var s_code=s.t();
	} catch(e) {
		console.log("ominture tracking is not working");
	}
}



