	
// ---------------------------------------------------------------------------
// 	Basic Interaction code
// ---------------------------------------------------------------------------
	
	// State
	var currentLeftPage;
	var currentRightPage;
	
	// Move the book to the start of the book
	function skipToStart()
	{
		displayPage(startPage);
	}
	
	// Move to the end
	function skipToEnd()
	{
		displayPage(lastPage);
	}

	
	// Move the book a page forward
	function movePageForward()
	{
		var proposedLeftPage = currentLeftPage + 2;
		if (proposedLeftPage > lastPage)
			proposedLeftPage = getLeftPageNumWhenViewing(lastPage);
			
		displayPage(proposedLeftPage);
	}
	
	// Move the book back
	function movePageBack()
	{
		var proposedLeftPage = currentLeftPage - 2;
		if (proposedLeftPage < startPage)
			proposedLeftPage = getLeftPageNumWhenViewing(startPage);
			
		displayPage(proposedLeftPage);
	}
	
	function getLeftPageNumWhenViewing(pageNumber)
	{
		return (pageNumber % 2 == 0) 
			? pageNumber
			: pageNumber - 1;
	}
	
	function getRightPageNumWhenViewing(pageNumber)
	{
		return (pageNumber % 2 == 0) 
			? pageNumber + 1
			: pageNumber;
	}
	
	// Move the book to the specified page
	function displayPage(pageNumber) 
	{
		// Sanity check
		if (pageNumber > lastPage)
			pageNumber = lastPage;
		if (pageNumber < startPage)
			pageNumber = startPage;
		
		// Set the appropriate state variables
		// indicating which page is on the right or left
		currentLeftPage = getLeftPageNumWhenViewing(pageNumber);
		currentRightPage = getRightPageNumWhenViewing(pageNumber);

		// Now get the source text
		var leftSourceDiv = getDivByID("page" + currentLeftPage);
		var rightSourceDiv = getDivByID("page" + currentRightPage);
		
		//	alert("cannot find the dif for page " + currentRightPage);
	
		// Copy this to our visible page divs.
		var viewerLeftPage = getDivByID("viewer-left-page");
		var viewerRightPage = getDivByID("viewer-right-page");
		viewerLeftPage.innerHTML = leftSourceDiv ? leftSourceDiv.innerHTML : ""
		viewerRightPage.innerHTML = rightSourceDiv ? rightSourceDiv.innerHTML : "";
		
		// Update the form fields
		document.pageSkipForm.pageTarget.value = pageNumber;
		var sectionBox = document.sectionSelectForm.sectionChooser;
		var targetIndex = sectionBox.options.length - 1;
		for (var i = 0; i < sectionBox.options.length; i++)
		{
			//alert(sectionBox.options[i].value);
			if (parseInt(sectionBox.options[i].value) > currentRightPage) {
				targetIndex = (i > 0) ? i -1 : i;
				break;
			}
		}
		
		sectionBox.selectedIndex = targetIndex;
	}
	
	// Display a popup with the additional text
	function showOmitted(omittedID)
	{
		// Copy the omitted text to our little popup thing
		var popupContentDiv = getDivByID("omitted-popup-content");
		popupContentDiv.innerHTML = getDivByID(omittedID).innerHTML;
		
		// Display the popup
		getDivByID("omitted-popup").style.display = "block";
	}
	
	
	function getDivByID(divID)
	{
		var resDiv = document.getElementById(divID);
		
		if (resDiv == null) {
			for (var i = 0; i < 5; i++) {
				resDiv = document.getElementById(divID);
				if (resDiv!= null)
					break;
			}
		}
		
		return resDiv;
	}
	
	
	function jumpToSection()
	{
		displayPage(parseInt(document.sectionSelectForm.sectionChooser.value));
	}
	
	function jumpToPage()
	{
		displayPage(parseInt(document.pageSkipForm.pageTarget.value));
		return false;
	}
	
	
	
	
	
	
// ---------------------------------------------------------------------------
// 	Basic Interaction code
// ---------------------------------------------------------------------------

	var RIGHT_ARROW_KEY = 39;
	var RIGHT_ARROW_KEY_MAC = 63235;
	var LEFT_ARROW_KEY = 37;
	var LEFT_ARROW_KEY_MAC = 63234; 
	var UP_ARROW_KEY = 38;
	var UP_ARROW_KEY_MAC = 63232;
	var DOWN_ARROW_KEY = 40;
	var DOWN_ARROW_KEY_MAC = 63233;
	var SPACE_BAR_KEY = 32;
	var SPACE_BAR_KEY_MAC = 32;



	document.onkeypress = function keyPress2(evt)
	{
		if(document.all) //it's IE
			var x = window.event.keyCode;
		else if(document.layers) //it's netscape
			var x = evt.which
		else
			var x = evt.keyCode

		var unicode = evt.charCode; // Firefox
	

		if (x == LEFT_ARROW_KEY || x == UP_ARROW_KEY || 
			x == LEFT_ARROW_KEY_MAC || x == UP_ARROW_KEY_MAC)
		{
			movePageBack();
		}
		else if (x == RIGHT_ARROW_KEY || x == DOWN_ARROW_KEY || 
			x == RIGHT_ARROW_KEY_MAC || x == DOWN_ARROW_KEY_MAC ||
			x == SPACE_BAR_KEY || x == SPACE_BAR_KEY_MAC) {
				movePageForward();
		}
		else if (unicode == SPACE_BAR_KEY) {
			movePageForward();
		}
		else {
			return true;
		}
	
		return false; // prevent double-fire
	}
  

	

