// Common interface functions

// establish "urlStart", for referencing files (e.g. images) in JavaScript-generated HTML
var urlStart = location.href.substr (0, location.href.indexOf("lbp5e/")) + "lbp5e/";

// Set the following variable to the frame to be drawn to
var displayFrame;

// Layer_Display: set the stylesheet "display" copy of layer l to d
function Layer_Display (l, d) {
	displayFrame.document.getElementById(l).style.display = d;		// works in both IE and NS6
}

function Layer_Vis (l, v) {
	displayFrame.document.getElementById(l).style.visibility = v;
}

// --------------------------------------------------
// Store_Cookie: store the named value in a cookie.  Adapted from code on p. 336 of Rhino book
// --------------------------------------------------
	function Store_Cookie (name, value) {
		var later = new Date();
		later.setFullYear (later.getFullYear() + 10);
		document.cookie = name + "=" + escape(value) + "; path=/; expires=" + later.toGMTString();
	}

// --------------------------------------------------
// Get_Cookie: get the value of the named cookie.  Adapted from code on p. 338 of Rhino book
// --------------------------------------------------
function Get_Cookie (name) {
	var allcookies = document.cookie;
	var pos = allcookies.indexOf (name + "=");
	if (pos != -1) {
		var start = pos + name.length + 1;
		var end = allcookies.indexOf (";", start);
		if (end == -1) end = allcookies.length;
		return unescape (allcookies.substring (start, end));
	} else
		return "";
}

function Stored_Chapter () {
	var ch = Get_Cookie ("chapter");
	if (ch == "")
		return -1
	else
		return ch;
}

var editMode = (Get_Cookie("edit") == "true");

// search string when the page is loaded should start with "cc"
// chapnum should be -1 for the chapter index or book home; otherwise a number to indicate the chapter we're working on
var searchArr = location.search.substr(1).split("_");

// If we don't find a chapter number in the search string, look for it in a cookie
// Stored_Chapter will return -1 if there's nothing there either.
if (searchArr[0] == "") {
	chapnum = Stored_Chapter();
} else {
	chapnum = searchArr[0] * 1;	
}


// load the .js file that defines the headings, questions, etc. for the specified chapter
// (unless we're showing the chapter index, 
// or unless we're showing tutorials, in which case there is a "metafile" for the tutorials in all the chapters
if (chapnum != -1 && location.href.indexOf("tutorials") == -1) {
	// In editMode, load the data dynamically
//	if (editMode) {
//		document.writeln('<script src="http://' + location.host + '/cgi-bin/RBWcreateJS.pl?chapter=', chapnum, '"><\/script>');
//	} else {
		document.writeln('<script src="../chapters/chap' + chapnum + '.js"><\/script>');
//	}
}

function Start_Display_Frame() {
	displayFrame.document.write ('<html><head>');
	displayFrame.document.write ('<link rel="stylesheet" href="' + urlStart + 'common/styles.css" type="text/css">');
	displayFrame.document.write ('<\/head>');
	displayFrame.document.write ('<body background="' + urlStart + 'common/bgd_frames.jpg" text="#000000" style="margin:10px; padding:0">');
}

function Start_Resource_Table() {
	displayFrame.document.write('<table cellspacing="0" cellpadding="0" border="0" width="100%">');
	displayFrame.document.write('<tr>');			// top border of round rect
	displayFrame.document.write('<td background="' + urlStart + 'common/cornertl.gif" width="30" height="30">&nbsp;<\/td>');
	displayFrame.document.write('<td background="' + urlStart + 'common/bordert.gif">&nbsp;<\/td>');
	displayFrame.document.write('<td background="' + urlStart + 'common/cornertr.gif" width="30">&nbsp;<\/td>');
	displayFrame.document.write('<\/tr>');
	displayFrame.document.write('<tr>');
	displayFrame.document.write('<td background="' + urlStart + 'common/borderl.gif">&nbsp;<\/td>');	// left border
	displayFrame.document.write('<td valign="top" height="200" bgcolor="#ffffff">');					// this cell encloses the content
}

function End_Resource_Table() {
	displayFrame.document.write('<\/td>');
	displayFrame.document.write('<td background="' + urlStart + 'common/borderr.gif">&nbsp;<\/td>');	// right border
	displayFrame.document.write('<\/tr>');
	displayFrame.document.write('<tr>');			// bottom border
	displayFrame.document.write('<td background="' + urlStart + 'common/cornerbl.gif" width="30" height="30"><img name="prevI" src="' + urlStart + 'common/nothing.gif" width="30" height="1" border="0"><\/td>');
	displayFrame.document.write('<td background="' + urlStart + 'common/borderb.gif">&nbsp;<\/td>');
	displayFrame.document.write('<td background="' + urlStart + 'common/cornerbr.gif" width="30"><img name="prevI" src="' + urlStart + 'common/nothing.gif" width="30" height="1" border="0"><\/td>');
	displayFrame.document.write('<\/tr>');
	displayFrame.document.write('<\/table>');
}

function End_Display_Frame() {
	displayFrame.document.write('<\/body><\/html>');
}


function Write_Book_Index (which) {
	displayFrame.document.write ('<div class="feedback">Click below or choose from the drop-down menu above to open the ', which, ' for a chapter.<\/div>');
	displayFrame.document.write ('<ol>');
	displayFrame.document.write ('<li><a href="Javascript:top.Switch_Chapter(\'01\')">Scope and Outlook<\/a><\/li>');
	displayFrame.document.write ('<li><a href="Javascript:top.Switch_Chapter(\'02\')">Functional Neuroanatomy<\/a><\/li>');
	displayFrame.document.write ('<li><a href="Javascript:top.Switch_Chapter(\'03\')">Neurophysiology<\/a><\/li>');
	displayFrame.document.write ('<li><a href="Javascript:top.Switch_Chapter(\'04\')">Chemical Bases of Behavior<\/a><\/li>');
	displayFrame.document.write ('<li><a href="Javascript:top.Switch_Chapter(\'05\')">Hormones and the Brain<\/a><\/li>');
	displayFrame.document.write ('<li><a href="Javascript:top.Switch_Chapter(\'06\')">Evolution of Brain and Behavior<\/a><\/li>');
	displayFrame.document.write ('<li><a href="Javascript:top.Switch_Chapter(\'07\')">Life-Span Development<\/a><\/li>');
	displayFrame.document.write ('<li><a href="Javascript:top.Switch_Chapter(\'08\')">Sensory Processing, Touch, Pain<\/a><\/li>');
	displayFrame.document.write ('<li><a href="Javascript:top.Switch_Chapter(\'09\')">Hearing, Vestibular, Taste, Smell<\/a><\/li>');
	displayFrame.document.write ('<li><a href="Javascript:top.Switch_Chapter(\'10\')">Vision: From Eye to Brain<\/a><\/li>');
	displayFrame.document.write ('<li><a href="Javascript:top.Switch_Chapter(\'11\')">Motor Control and Plasticity<\/a><\/li>');
	displayFrame.document.write ('<li><a href="Javascript:top.Switch_Chapter(\'12\')">Sex<\/a><\/li>');
	displayFrame.document.write ('<li><a href="Javascript:top.Switch_Chapter(\'13\')">Homeostasis<\/a><\/li>');
	displayFrame.document.write ('<li><a href="Javascript:top.Switch_Chapter(\'14\')">Biological Rhythms, Sleep<\/a><\/li>');
	displayFrame.document.write ('<li><a href="Javascript:top.Switch_Chapter(\'15\')">Emotions, Aggression, Stress<\/a><\/li>');
	displayFrame.document.write ('<li><a href="Javascript:top.Switch_Chapter(\'16\')">Psychopathology<\/a><\/li>');
	displayFrame.document.write ('<li><a href="Javascript:top.Switch_Chapter(\'17\')">Learning and Memory I<\/a><\/li>');
	displayFrame.document.write ('<li><a href="Javascript:top.Switch_Chapter(\'18\')">Learning and Memory II<\/a><\/li>');
	displayFrame.document.write ('<li><a href="Javascript:top.Switch_Chapter(\'19\')">Language and Cognition<\/a><\/li>');
	displayFrame.document.write ('<\/ol>');
}

