// JavaScript Document: Nav.js
// Copyright (c) 2004, 2005, 2007 by Gary Bradshaw.  All rights reserved.

// These functions are designed to provide basic navigation functions within a frame context.
// See Nav.doc for more information
var previousPage;
var pageStringToShow;
var titleString;
var explicitNextPage = null;
var explicitBackPage = null;
var replaceTopFrame = false;
// These values are for the hilite functions, but we declare them at the top to avoid having to tag each page.
var worldCookie;
var moduleNumber;
var modulePart;

// These functions write to the frames specified by top.titleFrame and top.pageNumFrame.
// Those variables must be set appropriately

function changeTitleBar()
  { if (titleString == null || top.titleFrame == null ) return; // This shouldn't happen, but seems to anyway.
        else if (top.titleFrameLoaded == false)
                {
                  setTimeout("changeTitleBar()", 1000);
                }
        else if (titleFrame != null)
                { var newNode = top.titleFrame.document.createTextNode(titleString);
                  var oldNode = top.titleFrame.document.getElementById("txtStr").firstChild;
                  var removedNode = top.titleFrame.document.getElementById("txtStr").replaceChild(newNode, oldNode);
                }
  }

 function changePageString()
  { if (top.titleFrameLoaded == false) setTimeout("changePageString()", 1000);
  	else if ( pageStringToShow == null) return;
        else
          { var newPNode = top.pageNumFrame.document.createTextNode(pageStringToShow);
            var oldPNode = top.pageNumFrame.document.getElementById("pageString").firstChild;
                var removedPNode = top.pageNumFrame.document.getElementById("pageString").replaceChild(newPNode, oldPNode);
          }
  }
function toggleLoVision ()
  { if (showSkin != null)
  	  { showSkin = null;
	  	deleteCookie("showSkin");
	  }
	else
	  { setCookie ("showSkin", "loVision");
	    showSkin = "loVision";
	  }
  }

function togglePhoneVision (tVal)
  { if (tVal == null)
	  { if (showSkin == "phone")
  	  	{ showSkin = null;
	  		deleteCookie("showSkin");
	  	}
		else
		  { setCookie ("showSkin", "phone");
	    	showSkin = "phone";
	  	  }
	  }
	else if (tVal && showSkin != "phone")
		  { setCookie ("showSkin", "phone");
	    	showSkin = "phone";
	  	  }
	else if (!tVal && showSkin == "phone")
  	  	{ showSkin = null;
	  		setCookie("showSkin","false");
	  	}
	}

function finishPageLoad(newString)
  {	pageStringToShow = getPageNumber();
  	if (pageStringToShow == 0) pageStringToShow = "Contents";
	else if (pageStringToShow < 0) pageStringToShow="Index";
	else pageStringToShow += " of "+top.lastPage;
  	changePageString();
  	storePage();
	if (newString != null)
	  { titleString = newString;
	  	changeTitleBar();
	  }
  }


function getCookieValue (cookieName)
  { var allCookies = document.cookie;
    var cookieStartsAt = allCookies.indexOf(" " + cookieName + "=");
    if (cookieStartsAt == -1)
      { cookieStartsAt = allCookies.indexOf(cookieName + "=");
      }
    if (cookieStartsAt == -1)
      { var cookieValue = null;
      }
    else
     { cookieStartsAt = allCookies.indexOf("=", cookieStartsAt) + 1;
       var cookieEndsAt = allCookies.indexOf(";", cookieStartsAt);
       if (cookieEndsAt == -1)
         { cookieEndsAt = allCookies.length;
	 }

       var cookieValue = unescape (allCookies.substring(cookieStartsAt, cookieEndsAt));
     }
	if (cookieValue == false || cookieValue == "false") cookieValue = null;
    return cookieValue;
  }

function setCookie (cookieName, cookieValue)
  { // We set the cookie to expire one year from now
    var nextYear = new Date();
    nextYear.setFullYear(nextYear.getFullYear()+1);
    var cookieExpires = "; expires="+nextYear.toGMTString();

    // Set the path to refer to any page on this server
    var cookiePath = '; path=/';

    var cookieString = cookieName + "=" + cookieValue + cookieExpires + cookiePath
    document.cookie = cookieString;
    if (getCookieValue(cookieName) == null && cookieValue != "false")
      { alert("Cookies must be enabled for certain ePsych functions to operate properly!");
      }
  }    

function deleteCookie (cookieName)
  { var allCookies = document.cookie;
    var cookieStartsAt = allCookies.indexOf(" " + cookieName + "=");
    if (cookieStartsAt == -1)
      { cookieStartsAt = allCookies.indexOf(cookieName + "=");
      }
    if (cookieStartsAt == -1)
      { return;
	  }
    else
     {  var cookieExpires = '; expires="+Thu, 01-Jan-1970 00:00:01 GMT';

   		// Set the path to refer to any page on this server
    	var cookiePath = '; path=/';

		var cookieString = cookieName + "= false" + cookieExpires + cookiePath;
		document.cookie = cookieString;
	 }
  }

function rememberPage()
  { var parURL = top.document.URL;
  	var questionLoc = parURL.lastIndexOf("?");
	if (questionLoc == -1) var pageToSave = escape(parURL+"?"+getmainFrameURL());
	else var pageToSave = escape(parURL.substring(0, questionLoc)+"?"+getmainFrameURL());
    setCookie("savedPage",pageToSave);
  }

function storePage()
  { var parURL = top.document.URL;
  	var questionLoc = parURL.lastIndexOf("?");
	if (questionLoc == -1) var pageToSave = escape(parURL+"?"+getmainFrameURL());
	else var pageToSave = escape(parURL.substring(0, questionLoc)+"?"+getmainFrameURL());
    setCookie("storedPage",pageToSave);
  }

function loadPage()
  { var pageToLoad=getCookieValue("savedPage");
   if (pageToLoad != null)
      { if (pageToLoad.indexOf(".html") == -1) pageToLoad += ".html";
		top.window.location.replace(pageToLoad);
      }
    else
      { alert ("Sorry, I couldn't fine a saved page for me to return to!");
      }
  }

function getPageNumber()
  { var frameURL = top.mainFrame.document.URL;
    var slashLocation = frameURL.lastIndexOf('/') + 1;
    var pageName = unescape(frameURL.substring(slashLocation, frameURL.length));
    var pageNumber = parseInt(pageName);
    if (isNaN(pageNumber))  pageNumber = -1;
    return(pageNumber);
  }

// Used only in tree-branching units
function goUpPage()
  { top.mainFrame.location.replace(top.mainFrame.upPage);
  }

// This is a general function that will work even for non-numeric pages
function getmainFrameURL()
  { var frameURL = top.mainFrame.document.URL;
  	var topURL = top.document.URL;
	var newURL = frameURL.substr(topURL.lastIndexOf('/')+1);
    return(newURL);
  }

function goNextPage()
  { var pageIndex = getPageNumber();
	var baseURL = top.location.href.substring(0,top.location.href.lastIndexOf("/")+1);
	var goToName;
	top.explicitBackPage = null;
	if (pageIndex == top.transitionPageNumber) 
	  {	goToName = baseURL + top.transitionPage;
	  	top.location.replace(goToName);
	  }
	else if (top.explicitNextPage != null) // The page is telling us where to go
	  { goToName = baseURL + explicitNextPage;
	    if (replaceTopFrame == true || top.explicitNextPage.substring(0,3) == '../' || top.explicitNextPage.substring(0,1) == '/')
		  {	replaceTopFrame = false;
		  	top.location.replace(goToName);
		  }
		else
		  {	top.explicitNextPage = null; // Zero it out before we go anywhere
			top.mainFrame.location.replace(goToName);
		  }
	  }
	else
	  { pageIndex++;
	  	top.explicitNextPage = null; // Zero it out before we go anywhere
	    goToName = baseURL+pageIndex+".html";
		//alert("goToName: " + goToName + " top.mainFrame: " + top.mainFrame.location);
	    top.mainFrame.location.replace(goToName);
	  }
	 return false;
  }
  
function goPrevPage()
  { var pageIndex = getPageNumber()-1;
	var baseURL = top.location.href.substring(0,top.location.href.lastIndexOf("/")+1);
	var goToName;
	top.explicitNextPage = null;
	if (top.explicitBackPage != null) // The page is telling us where to go
	  { goToName = baseURL + explicitBackPage;
	    if (replaceTopFrame == true || top.explicitBackPage.substring(0,3) == '../' || top.explicitBackPage.substring(0,1) == '/')
		  {	replaceTopFrame = false;
		  	top.location.replace(goToName);
		  }
		else 
		  {	top.explicitBackPage = null; // Zero it out before we go anywhere
			top.mainFrame.location.replace(goToName);
		  }
	  }
	else if (pageIndex >= 0)
	  {	goToName = baseURL+pageIndex+".html";
	  	top.explicitBackPage = null; // Zero it out before we go anywhere
		top.mainFrame.location.replace(goToName);
	  }
	else
	  { goToName = baseURL + "../index.html";
	  	top.explicitBackPage = null; // Zero it out before we go anywhere
	  	top.location.replace(goToName);
	  }
  }
  
function goToPage(pageString)
  { var frameIndex = parseInt(pageString);
	if (frameIndex > 0 && frameIndex <= top.lastPage)
	  {	var newURL = top.document.URL;
		var newName = unescape(newURL);
		var slashLoc = newName.lastIndexOf('/');
		var goToName = newName.substring(0,slashLoc)+"/"+frameIndex+".html";
	    if (replaceTopFrame == true)
		  {	replaceTopFrame = false;
		  	top.location.replace(goToName);
		  }
		else top.mainFrame.location.replace(goToName);
	  }
	else
	  { alert ("You can only go back to a page between 1 and "+top.lastPage);
	  	document.goForm.pagePanel.value="1";
	  }
  }

 function goContentsPage()
  { if (contentsPage != null)
  	  {	var newURL = document.URL;
		var newName = unescape(newURL);
		var slashLoc = newName.lastIndexOf('/');
		var goToName = newName.substring(0,slashLoc)+"/"+contentsPage;
		top.mainFrame.location.replace(goToName);
	  }
  }
 
function goMapPage()
  { if (mapPage != null)
  	  {	var newURL = document.URL;
		var newName = unescape(newURL);
		var slashLoc = newName.lastIndexOf('/');
		var goToName = newName.substring(0,slashLoc)+"/"+mapPage;
	 	top.window.location.replace(goToName);
	  }
   }
  
