// NOTE: Additional Javascript functions are stored in /circuit/rollovers.js

// Parameters.

// Cookie name

histCookieName = "Soothcircuit"


// DATA ARRAYS

// 2-dimensional array for paired theme letters, indexed by first theme letter
var themes = new Array()
themes["t"] = new Array("t", "ti", "tk", "tf", "ta", "tc", "tb", "td")
themes["i"] = new Array("ti", "i", "ik", "if", "ia", "ic", "ib", "id")
themes["k"] = new Array("tk", "ik", "k", "kf", "ka", "kc", "kb", "kd")
themes["f"] = new Array("tf", "if", "kf", "f", "fa", "fc", "fb", "fd")
themes["a"] = new Array("ta", "ia", "ka", "fa", "a", "ac", "ab", "ad")
themes["c"] = new Array("tc", "ic", "kc", "fc", "ac", "c", "cb", "cd")
themes["b"] = new Array("tb", "ib", "kb", "fb", "ab", "cb", "b", "bd")
themes["d"] = new Array("td", "id", "kd", "fd", "ad", "cd", "bd", "d")

// Used for looking up second theme letter in themes array
var themeID = new Array()
themeID["t"] = 0
themeID["i"] = 1
themeID["k"] = 2
themeID["f"] = 3
themeID["a"] = 4
themeID["c"] = 5
themeID["b"] = 6
themeID["d"] = 7

// Used for finding alternative stanzas
var themeLetter = new Array("t", "i", "k", "f", "a", "c", "b", "d")

var componentNames = new Array("battery", "lamp", "antenna", "resistor", "transistor", "speaker", "capacitor", "ground")

// Lookup table to find theme letters for components
var componentThemes = new Array()
componentThemes["battery"] = "t"
componentThemes["lamp"] = "i"
componentThemes["antenna"] = "k"
componentThemes["resistor"] = "f"
componentThemes["transistor"] = "a"
componentThemes["speaker"] = "c"
componentThemes["capacitor"] = "b"
componentThemes["ground"] = "d"

// 2-D array for filenames for stanzas; indexed by theme letters
var stanzas = new Array()

stanzas["t"] = new Array("t01.htm", "t02.htm", "t03.htm", "t04.htm", "t05.htm", "t06.htm", "t07.htm", "t08.htm", "t09.htm", "t10.htm", "t11.htm", "t12.htm", "t13.htm", "t14.htm", "t15.htm", "t16.htm", "t17.htm", "t18.htm", "t19.htm", "t20.htm", "t21.htm", "t22.htm", "t23.htm", "t24.htm", "t25.htm", "t26.htm")

stanzas["ti"] = new Array("ti01.htm", "ti02.htm", "ti03.htm", "ti04.htm", "ti05.htm")

stanzas["tk"] = new Array("tk01.htm", "tk02.htm", "tk03.htm", "tk04.htm", "tk05.htm", "tk06.htm", "tk07.htm", "tk08.htm", "tk09.htm", "tk10.htm", "tk11.htm", "tk12.htm", "tk13.htm", "tk14.htm", "tk15.htm", "tk16.htm", "tk17.htm", "tk18.htm")

stanzas["tf"] = new Array("tf01.htm", "tf02.htm", "tf03.htm", "tf04.htm", "tf05.htm", "tf06.htm", "tf07.htm", "tf08.htm", "tf09.htm", "tf10.htm", "tf11.htm", "tf12.htm", "tf13.htm", "tf14.htm", "tf15.htm", "tf16.htm", "tf17.htm", "tf18.htm", "tf19.htm", "tf20.htm", "tf21.htm", "tf22.htm", "tf23.htm", "tf24.htm")

stanzas["ta"] = new Array("ta01.htm", "ta02.htm", "ta03.htm", "ta04.htm", "ta05.htm")

stanzas["tc"] = new Array("tc01.htm", "tc02.htm", "tc03.htm", "tc04.htm", "tc05.htm")

stanzas["tb"] = new Array("tb01.htm", "tb02.htm", "tb03.htm", "tb04.htm")

stanzas["td"] = new Array("td01.htm", "td02.htm", "td03.htm", "td04.htm", "td05.htm", "td06.htm", "td07.htm")

stanzas["i"] = new Array("i01.htm", "i02.htm", "i03.htm", "i04.htm", "i05.htm", "i06.htm", "i07.htm", "i08.htm")

stanzas["ik"] = new Array("ik01.htm", "ik02.htm", "ik03.htm", "ik04.htm", "ik05.htm", "ik06.htm", "ik07.htm")

stanzas["if"] = new Array("if01.htm", "if02.htm", "if03.htm", "if04.htm", "if05.htm", "if06.htm", "if07.htm", "if08.htm", "if09.htm", "if10.htm", "if11.htm")

stanzas["ia"] = new Array("ia01.htm", "ia02.htm", "ia03.htm", "ia04.htm", "ia05.htm")

stanzas["ic"] = new Array("ic01.htm", "ic02.htm", "ic03.htm", "ic04.htm", "ic05.htm")

stanzas["ib"] = new Array("ib01.htm", "ib02.htm", "ib03.htm", "ib04.htm")

stanzas["id"] = new Array("id01.htm", "id02.htm", "id03.htm", "id04.htm")

stanzas["k"] = new Array("k01.htm", "k02.htm", "k03.htm", "k04.htm", "k05.htm", "k06.htm", "k07.htm", "k08.htm", "k09.htm", "k10.htm", "k11.htm", "k12.htm", "k13.htm", "k14.htm", "k15.htm", "k16.htm", "k17.htm", "k18.htm", "k19.htm", "k20.htm", "k21.htm", "k22.htm", "k23.htm", "k24.htm", "k25.htm", "k26.htm", "k27.htm", "k28.htm", "k29.htm", "k30.htm", "k31.htm", "k32.htm", "k33.htm", "k34.htm", "k35.htm", "k36.htm", "k37.htm", "k38.htm", "k39.htm", "k40.htm", "k41.htm", "k42.htm", "k43.htm", "k44.htm", "k45.htm", "k46.htm", "k47.htm", "k48.htm")

stanzas["kf"] = new Array("kf01.htm", "kf02.htm", "kf03.htm", "kf04.htm", "kf05.htm", "kf06.htm", "kf07.htm", "kf08.htm", "kf09.htm", "kf10.htm", "kf11.htm", "kf12.htm", "kf13.htm", "kf14.htm", "kf15.htm", "kf16.htm", "kf17.htm", "kf18.htm", "kf19.htm", "kf20.htm", "kf21.htm", "kf22.htm", "kf23.htm", "kf24.htm", "kf25.htm")

stanzas["ka"] = new Array("ka01.htm", "ka02.htm", "ka03.htm", "ka04.htm", "ka05.htm")

stanzas["kc"] = new Array("kc01.htm", "kc02.htm", "kc03.htm", "kc04.htm", "kc05.htm", "kc06.htm", "kc07.htm", "kc08.htm", "kc09.htm", "kc10.htm", "kc11.htm", "kc12.htm", "kc13.htm", "kc14.htm", "kc15.htm", "kc16.htm", "kc17.htm")

stanzas["kb"] = new Array("kb01.htm", "kb02.htm", "kb03.htm", "kb04.htm")

stanzas["kd"] = new Array("kd01.htm", "kd02.htm", "kd03.htm")

stanzas["f"] = new Array("f01.htm", "f02.htm", "f03.htm", "f04.htm", "f05.htm", "f06.htm", "f07.htm", "f08.htm", "f09.htm", "f10.htm", "f11.htm", "f12.htm", "f13.htm", "f14.htm", "f15.htm", "f16.htm", "f17.htm", "f18.htm", "f19.htm", "f20.htm", "f21.htm", "f22.htm", "f23.htm", "f24.htm", "f25.htm", "f26.htm", "f27.htm", "f28.htm", "f29.htm", "f30.htm", "f31.htm", "f32.htm", "f33.htm", "f34.htm", "f35.htm", "f36.htm", "f37.htm", "f38.htm", "f39.htm", "f40.htm", "f41.htm")

stanzas["fa"] = new Array("fa01.htm", "fa02.htm", "fa03.htm", "fa04.htm", "fa05.htm")

stanzas["fc"] = new Array("fc01.htm", "fc02.htm", "fc03.htm", "fc04.htm", "fc05.htm")

stanzas["fb"] = new Array("fb01.htm", "fb02.htm", "fb03.htm")

stanzas["fd"] = new Array("fd01.htm", "fd02.htm", "fd03.htm", "fd04.htm")

stanzas["a"] = new Array("a01.htm", "a02.htm", "a03.htm", "a04.htm", "a05.htm", "a06.htm")

stanzas["ac"] = new Array("ac01.htm", "ac02.htm", "ac03.htm", "ac04.htm")

stanzas["ab"] = new Array("ab01.htm", "ab02.htm", "ab03.htm")

stanzas["ad"] = new Array("ad01.htm", "ad02.htm")

stanzas["c"] = new Array("c01.htm", "c02.htm", "c03.htm", "c04.htm", "c05.htm", "c06.htm")

stanzas["cb"] = new Array("cb01.htm", "cb02.htm", "cb03.htm")

stanzas["cd"] = new Array("cd01.htm", "cd02.htm", "cd03.htm")

stanzas["b"] = new Array("b01.htm", "b02.htm")

stanzas["bd"] = new Array("bd01.htm", "bd02.htm", "bd03.htm", "bd04.htm", "bd05.htm", "bd06.htm")

stanzas["d"] = new Array("d01.htm", "d02.htm", "d03.htm", "d04.htm", "d05.htm")


// temp ************************
/*
var stanzas = new Array()

stanzas["t"] =  new Array("t01.htm", "t02.htm")

stanzas["ti"] =  new Array("ti01.htm", "ti02.htm")

stanzas["tk"] =  new Array("tk01.htm", "tk02.htm")

stanzas["tf"] =  new Array("tf01.htm", "tf02.htm")

stanzas["ta"] =  new Array("ta01.htm", "ta02.htm")

stanzas["tc"] =  new Array("tc01.htm", "tc02.htm")

stanzas["tb"] =  new Array("tb01.htm", "tb02.htm")

stanzas["td"] =  new Array("td01.htm", "td02.htm")

stanzas["i"] =  new Array("i01.htm", "i02.htm")

stanzas["ik"] =  new Array("ik01.htm", "ik02.htm")

stanzas["if"] =  new Array("if01.htm", "if02.htm")

stanzas["ia"] =  new Array("ia01.htm", "ia02.htm")

stanzas["ic"] =  new Array("ic01.htm", "ic02.htm")

stanzas["ib"] =  new Array("ib01.htm", "ib02.htm")

stanzas["id"] =  new Array("id01.htm", "id02.htm")

stanzas["k"] =  new Array("k01.htm", "k02.htm")

stanzas["kf"] =  new Array("kf01.htm", "kf02.htm")

stanzas["ka"] =  new Array("ka01.htm", "ka02.htm")

stanzas["kc"] =  new Array("kc01.htm", "kc02.htm")

stanzas["kb"] =  new Array("kb01.htm", "kb02.htm")

stanzas["kd"] =  new Array("kd01.htm", "kd02.htm")

stanzas["f"] =  new Array("f01.htm", "f02.htm")

stanzas["fa"] =  new Array("fa01.htm", "fa02.htm")

stanzas["fc"] =  new Array("fc01.htm", "fc02.htm")

stanzas["fb"] =  new Array("fb01.htm", "fb02.htm")

stanzas["fd"] =  new Array("fd01.htm", "fd02.htm")

stanzas["a"] =  new Array("a01.htm", "a02.htm")

stanzas["ac"] =  new Array("ac01.htm", "ac02.htm")

stanzas["ab"] =  new Array("ab01.htm", "ab02.htm")

stanzas["ad"] =  new Array("ad01.htm", "ad02.htm")

stanzas["c"] =  new Array("c01.htm", "c02.htm")

stanzas["cb"] =  new Array("cb01.htm", "cb02.htm")

stanzas["cd"] =  new Array("cd01.htm", "cd02.htm")

stanzas["b"] =  new Array("b01.htm", "b02.htm")

stanzas["bd"] =  new Array("bd01.htm", "bd02.htm")

stanzas["d"] =  new Array("d01.htm", "d02.htm")

*/

// *******************************************************
// ******* PAGE FORMATTING FUNCTIONS *********************

// Called by openFormatStanza(), closeFormatStanza(), and the 2 transition page formatting functions.
// Check current frame name to determine which format values to use;
// build formatting string using these values; formatType passed to this function
// determines which string to build
function getFormatCode(formatType) {
	var styleName = window.name
	// Margin value for transition pages
	var transMarginValue = 5
	// Gets the correct formatting values depending on name of current frame
	switch (styleName) {
		// STANZAS
		case "northLeft":
			var bgImageName = "images/bg-north.jpg"
			var tableWidth = 200
			var tableHeight = 170
			var topMargin = 0
			var bottomMargin = 0
			var leftMargin = 0
			var rightMargin = 15
			var textVAlign = "bottom"
			var textHAlign = "left"
			break
		case "northRight":
			var bgImageName = "images/bg-north.jpg"
			var tableWidth = 200
			var tableHeight = 170
			var topMargin = 0
			var bottomMargin = 0
			var leftMargin = 15
			var rightMargin = 0
			var textVAlign = "bottom"
			var textHAlign = "left"
			break
		case "eastTop":
			var bgImageName = "images/bg-sides-top.jpg"
			var tableWidth = 250
			var tableHeight = 150
			var topMargin = 0
			var bottomMargin = 15
			var leftMargin = 0
			var rightMargin = 20
			var textVAlign = "bottom"
			var textHAlign = "left"
			break
		case "eastBottom":
			var bgImageName = "images/bg-sides-bottom.jpg"
			var tableWidth = 250
			var tableHeight = 150
			var topMargin = 15
			var bottomMargin = 0
			var leftMargin = 0
			var rightMargin = 20
			var textVAlign = "top"
			var textHAlign = "left"
			break
		case "southRight":
			var bgImageName = "images/bg-south.jpg"
			var tableWidth = 200
			var tableHeight = 170
			var topMargin = 0
			var bottomMargin = 0
			var leftMargin = 15
			var rightMargin = 0
			var textVAlign = "top"
			var textHAlign = "left"
			break
		case "southLeft":
			var bgImageName = "images/bg-south.jpg"
			var tableWidth = 200
			var tableHeight = 170
			var topMargin = 0
			var bottomMargin = 0
			var leftMargin = 0
			var rightMargin = 15
			var textVAlign = "top"
			var textHAlign = "left"
			break
		case "westBottom":
			var bgImageName = "images/bg-sides-bottom.jpg"
			var tableWidth = 250
			var tableHeight = 150
			var topMargin = 15
			var bottomMargin = 0
			var leftMargin = 20
			var rightMargin = 0
			var textVAlign = "top"
			var textHAlign = "left"
			break
		case "westTop":
			var bgImageName = "images/bg-sides-top.jpg"
			var tableWidth = 250
			var tableHeight = 150
			var topMargin = 0
			var bottomMargin = 15
			var leftMargin = 20
			var rightMargin = 0
			var textVAlign = "bottom"
			var textHAlign = "left"
			break
		// TRANSITIONS
		case "transNorthWest":
			var bgImageName = "../images/bg-transNW.jpg"
			var textVAlign = "top"
			var textHAlign = "left"
			var topMargin = transMarginValue
			var bottomMargin = 0
			var leftMargin = transMarginValue
			var rightMargin = 0
			break
		case "transNorthEast":
			var bgImageName = "../images/bg-transNE.jpg"
			var textVAlign = "top"
			var textHAlign = "right"
			var topMargin = transMarginValue
			var bottomMargin = 0
			var leftMargin = 0
			var rightMargin = transMarginValue
			break
		case "transSouthEast":
			var bgImageName = "../images/bg-transSE.jpg"
			var textVAlign = "bottom"
			var textHAlign = "right"
			var topMargin = 0
			var bottomMargin = transMarginValue
			var leftMargin = 0
			var rightMargin = transMarginValue
			break
		case "transSouthWest":
			var bgImageName = "../images/bg-transSW.jpg"
			var textVAlign = "bottom"
			var textHAlign = "left"
			var topMargin = 0
			var bottomMargin = transMarginValue
			var leftMargin = transMarginValue
			var rightMargin = 0
			break
	}
	// Builds the code string for the specified format type
	switch (formatType) {
		// Format types used by both Stanzas and Transitions
		case "bgImage":
			var formatCode = '<style type="text/css"><!--body {background-image: url(' + bgImageName + ');}--></style>'
			break
		case "textAlign":
			var formatCode = '<td align="' + textHAlign + '" valign="' + textVAlign + '">'
			break
		case "topMargin":
			if (topMargin != 0) {
				var formatCode = '<tr><td height="' + topMargin + '"><img src="images/spacer.gif" width="1" height="' + topMargin + '" /></td></tr>'
			} else {
				var formatCode = ""
			}
			break
		case "bottomMargin":
			if (bottomMargin != 0) {
				var formatCode = '<tr><td height="' + bottomMargin + '"><img src="images/spacer.gif" width="1" height="' + bottomMargin + '" /></td></tr>'
			} else {
				var formatCode = ""
			}
			break
		case "leftMargin":
			if (leftMargin != 0) {
				var formatCode = '<td width="' + leftMargin + '"><img src="images/spacer.gif" width="' + leftMargin + '" height="1" /></td>'
			} else {
				var formatCode = ""
			}
			break
		case "rightMargin":
			if (rightMargin != 0) {
				var formatCode = '<td width="' + rightMargin + '"><img src="images/spacer.gif" width="' + rightMargin + '" height="1" /></td>'
			} else {
				var formatCode = ""
			}
			break
		// Format types used only by Stanzas
		case "tableSize":
			var formatCode = '<table width="' + tableWidth + '" height="' + tableHeight + '" border="0" cellpadding="0" cellspacing="0">'
			break
		case "textStyle":
			var formatCode = '<span class="' + styleName + '">'
			break
	}
	return formatCode
}

// Inserts correct formatting code before text in stanza page
function openFormatStanza() {
	var formatCode = getFormatCode("bgImage")
	formatCode += getFormatCode("tableSize")
	formatCode += getFormatCode("topMargin")
	formatCode += '<tr>' + getFormatCode("leftMargin") + getFormatCode("textAlign") + getFormatCode("textStyle")
	display(formatCode)
}

// Inserts correct formatting code after text in stanza page
function closeFormatStanza() {
	var formatCode = '</span></td>' + getFormatCode("rightMargin") + '</tr>' + getFormatCode("bottomMargin") + '</table>'
	display(formatCode)
}

// Add formatting for background and text depending on
// which frame the transition page is loaded into
function openFormatTransition() {
	formatCode = getFormatCode("bgImage")
	formatCode += '<table width="145" height="65" border="0" cellpadding="0" cellspacing="0">'
	formatCode += getFormatCode("topMargin")
	formatCode += '<tr>' + getFormatCode("leftMargin") + getFormatCode("textAlign") + '<span class="transition">'
	display(formatCode)
}

// Adds the correct code after the text in a transition page to complete the formatting
function closeFormatTransition () {
	var formatCode = '</span></td>' + getFormatCode("rightMargin") + '</tr>' + getFormatCode("bottomMargin") + '</table>'
	display(formatCode)
}

// ************************************************
// ************ PAGE SELECTION FUNCTIONS **********

function executeAfterLoading() {
	// If current page isn't loaded within the correct frame, load the index page instead
	if (top.location.href == window.location.href) {
		location = "index.htm"
	}
	// Execute this if config.js is loaded by navbar; sets variables that are maintained
	// in this frame
	if (self.name == "navBar") {
		// Global variables
		// Keeps track of which 4 components are used in circuit
		parent.navBar.themesInCircuit = new Array(4)
		parent.navBar.numStanzasLoaded = 0
	// Execute this if config.js is loaded by a stanza page or by the last transition page
	// in the transNorthWest frame;
	// Don't execute if justLoaded is true, indicating that the refresh button
	// has just been pressed
	} else {
		if ((parent.justLoaded == false) && (self.name != "transNorthEast") && (self.name != "transSouthEast") && (self.name != "transSouthWest")) {
			loadRemainingStanzas()
		}
	}
}

// Executed by navBar.htm when Enter key is pressed;
// Writes an html page to the question frame; the text includes
// the question typed in the text box
function loadQuestion() {
	var questionText = parent.navBar.document.forms[0].questionField.value
	
	var pageText = '<?xml version="1.0" encoding="iso-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">'
	pageText += '<head><title>Soothcircuit</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />'
	pageText += '<link href="soothcircuit.css" rel="stylesheet" type="text/css" /></head>'
	pageText += '<body background="images/bg-center.jpg">'
	pageText += '<table width="120" height="80" border="0" cellpadding="0" cellspacing="0">'
	pageText += '<tr><td align="center" valign="middle"><span class="question">'
	pageText += questionText
	pageText += '</span></td></tr></table></body></html>'
	
	parent.question.document.open()
	parent.question.document.write(pageText)
	parent.question.document.close()
	parent.navBar.document.forms[0].questionField.value = ""
	return true
}

// Load blank pages into all frames
function clearCircuit() {
	//Question
	parent.question.location = "blank_question.htm"
	parent.navBar.document.forms[0].questionField.value = ""
	// Stanzas
	parent.northLeft.location = "blank_stanza.htm"
	parent.northRight.location = "blank_stanza.htm"
	parent.eastTop.location = "blank_stanza.htm"
	parent.eastBottom.location = "blank_stanza.htm"
	parent.southRight.location = "blank_stanza.htm"
	parent.southLeft.location = "blank_stanza.htm"
	parent.westBottom.location = "blank_stanza.htm"
	parent.westTop.location = "blank_stanza.htm"
	// Transitions
	parent.transNorthWest.location = "transitions/blank_trans.htm"
	parent.transNorthEast.location = "transitions/blank_trans.htm"
	parent.transSouthEast.location = "transitions/blank_trans.htm"
	parent.transSouthWest.location = "transitions/blank_trans.htm"
	// Components
	parent.componentN.location = "circuit/blank_n.htm"
	parent.componentE.location = "circuit/blank_e.htm"
	parent.componentS.location = "circuit/blank_s.htm"
	parent.componentW.location = "circuit/blank_w.htm"
	// Reset variables
	parent.navBar.numStanzasLoaded = 0
	for (var i = 0; i < parent.navBar.themesInCircuit.length; i++) {
		parent.navBar.themesInCircuit[i] = ""
	}
}

// Extract filename from URL
function getFilename(fullURL) {
	return fullURL.substring(fullURL.lastIndexOf("/") + 1, fullURL.length)
}

// Build array of component frames that don't contain components
function getEmptyFrames() {
	var framesArray = new Array(0)
	if (getFilename(parent.componentN.location.href) == "blank_n.htm") {
		framesArray[framesArray.length] = "componentN"
	}
	if (getFilename(parent.componentE.location.href) == "blank_e.htm") {
		framesArray[framesArray.length] = "componentE"
	}
	if (getFilename(parent.componentS.location.href) == "blank_s.htm") {
		framesArray[framesArray.length] = "componentS"
	}
	if (getFilename(parent.componentW.location.href) == "blank_w.htm") {
		framesArray[framesArray.length] = "componentW"
	}
	return framesArray
}

// Randomly select a component; called by chooseEmptyFrame()
// if user clicks on Random button
function getRandomComponent() {
	var selectNum = Math.floor(Math.random() * 8)
	return componentNames[selectNum]
}

// Randomly select an empty frame to hold new component
// and load the selected component into it
function chooseEmptyFrame(componentName) {
	// If user clicked the Random Select button,
	// randomly select a component name
	if (componentName == "random") {
		componentName = getRandomComponent()
	}
	var emptyFrames = getEmptyFrames()
	var numEmptyFrames = emptyFrames.length
	if (numEmptyFrames > 0) {
		var selectedFrameNo = Math.floor(Math.random() * numEmptyFrames)
		var selectedFrame = emptyFrames[selectedFrameNo]
		// Load component in selected frame and add theme to appropriate element
		// of themesInCircuit array
		switch (selectedFrame) {
			case "componentN":
				parent.componentN.location = "circuit/n_" + componentName + ".htm"
				parent.navBar.themesInCircuit[0] = componentThemes[componentName]
				break
			case "componentE":
				parent.componentE.location = "circuit/e_" + componentName + ".htm"
				parent.navBar.themesInCircuit[1] = componentThemes[componentName]
				break
			case "componentS":
				parent.componentS.location = "circuit/s_" + componentName + ".htm"
				parent.navBar.themesInCircuit[2] = componentThemes[componentName]
				break
			case "componentW":
				parent.componentW.location = "circuit/w_" + componentName + ".htm"
				parent.navBar.themesInCircuit[3] = componentThemes[componentName]
				break
		}
	}
	// A new frame has been filled
	return numEmptyFrames - 1
}

// Called by chooseStanza()
function getUnvisitedStanza(currentTheme, secondTheme) {
	var availableStanzas = new Array()
	if (secondTheme == "") {
		availableStanzas = stanzas[currentTheme]
	} else {
		availableStanzas = stanzas[themes[currentTheme][themeID[secondTheme]]]
	}
	// Randomly select an unvisited stanza;
	// If all stanzas are read, return "NoUnvisited"
	var selectedStanza = filteredRandom(notVisited, "NoUnvisited", availableStanzas)
	/*// temp
	if (parent.navBar.numStanzasLoaded == 0) {
		var historyContent = histArray()
		var rawHistory = histValue()
		alert(currentTheme + secondTheme + " -- " + selectedStanza + " -- " + availableStanzas + " --history: " + historyContent + " raw history: " + rawHistory)
	} */
	return selectedStanza
}

// Copy stanzas into filteredStanzas
// only if they are not included in stanzasToExclude array;
// this is used to avoid displaying visited stanzas that are already
// displayed in the circuit
function removeDisplayedStanzas(availableStanzas, stanzasToExclude) {
	var evalStanza
	var exclude
	var filteredStanzas = new Array()
	var filteredStanzasInd = 0
	// Test each stanza in availableStanzas
	for (var i = 0; i < availableStanzas.length; i++) {
		evalStanza = availableStanzas[i]
		exclude = false
		// Test stanza against stanzasToExclude array to see
		// if it's already displayed
		for (var j = 0; j < stanzasToExclude.length; j++) {
			if (evalStanza == stanzasToExclude[j]) {
				exclude = true
				break
			}
		}
		// If not displayed, add it to filteredStanzas
		if (exclude == false) {
			filteredStanzas[filteredStanzasInd] = evalStanza
			filteredStanzasInd += 1
		}
	}
	return filteredStanzas
}

// Chooses an appropriate stanza given the current components in the circuit;
// values of parameters tell which theme (component) occurpies each position
// relative to current stanza's position
function chooseStanza(currentTheme, nextTheme, oppositeTheme, previousTheme, stanzaNum) {
	var selectedStanza
	var secondTheme
	var availableStanzas = new Array()
	// Select an unvisited stanza looking in the theme groups
	// that represent components in the current circuit;
	// the order of the search is determined by the proximity
	// of each component/theme group to the current component
	// *USE MAIN THEMES*
	switch (stanzaNum) {
		// If current stanza is the first of a pair of stanzas
		case 1:
			// Store the appropriate second theme letters; the function will search
			// the theme groups in this order for an unvisited stanza;
			// for example, if current theme is "i" and themesToTest = ("i", "k", "a", "t"),
			// it will search i then ik then ia then ti
			var themesToTest = new Array(currentTheme, previousTheme, nextTheme, oppositeTheme)
			break
		// If current stanza is the second of a pair of stanzas
		case 2:
			var themesToTest = new Array(nextTheme, previousTheme, currentTheme, oppositeTheme)			
			break
	}
	// Look for unvisited stanzas in each specified theme group until one is found
	for (var i = 0; i < themesToTest.length; i++) {
		secondTheme = themesToTest[i]
		selectedStanza = getUnvisitedStanza(currentTheme, secondTheme)
		if (selectedStanza != "NoUnvisited") {
			break
		}
	}
	// If no unvisited stanzas are found above,
	// create an array of stanzas from theme groups in which the first theme letter
	// is the same as currentTheme and the second theme letter differs
	// from second theme letters of the groups that have already been searched;
	// then look for unvisited stanza in all these theme groups
	// *USE ALTERNATIVE THEMES*
	if (selectedStanza == "NoUnvisited") {
		var evalTheme
		var stanzasToAdd = new Array()
		var altStanzas = new Array()
		for (var i = 0; i <= 7; i++) {
			evalTheme = themeLetter[i]
			stanzasToAdd = stanzas[themes[currentTheme][themeID[evalTheme]]]
			if (evalTheme != currentTheme && evalTheme != nextTheme && evalTheme != oppositeTheme && evalTheme != previousTheme) {
				altStanzas = altStanzas.concat(stanzasToAdd)
			}
			availableStanzas = altStanzas
		}
		selectedStanza = filteredRandom(notVisited, "NoUnvisited", availableStanzas)
	}
	// If there are no unvisited stanzas available, randomly select a stanza
	// for one of the main themes; exlude stasnzas that are already displayed in the circuit
	// *USE VISITED STANZAS (MAIN THEMES)*
	if (selectedStanza == "NoUnvisited") {
		// If stanzas are currently displayed in circuit,
		// create array containing all stanzas currently displayed in circuit;
		// these must be excluded from selection to avoid duplicatation
		var filteredStanzas = new Array()
		if (parent.navBar.numStanzasLoaded > 0) {
			var stanzasToExclude = new Array()
			var histIndex = 1 - parent.navBar.numStanzasLoaded
			for (var i = 0; i < parent.navBar.numStanzasLoaded; i++) {
				stanzasToExclude[i] = hist(histIndex)
				histIndex = histIndex + 1
			}
		}
		// Go through each main theme group to find a stanza that isn't currently displayed
		for (var i = 0; i < themesToTest.length; i++) {
			secondTheme = themesToTest[i]
			availableStanzas = stanzas[themes[currentTheme][themeID[secondTheme]]]
			// If stanzas are currently displayed, remove them from availableStanzas
			if (parent.navBar.numStanzasLoaded > 0) {
				filteredStanzas = removeDisplayedStanzas(availableStanzas, stanzasToExclude)
				availableStanzas = filteredStanzas
			}
			if (availableStanzas.length != 0) {
				break
			}
		}
		/* If no stanzas available from main group (because all of them
		are already displayed), use stanzas from alternative theme groups */ 
		// *USE VISITED STANZAS (ALTERNATIVE THEMES)*
		if (availableStanzas.length == 0) {
			availableStanzas = altStanzas
			if (parent.navBar.numStanzasLoaded > 0) {
				filteredStanzas = removeDisplayedStanzas(availableStanzas, stanzasToExclude)
				availableStanzas = filteredStanzas
			}
		}
		selectedStanza = random(availableStanzas)
	}
	return selectedStanza
}

// Executed when a component icon is clicked on
function buildCircuit(componentName) {
	parent.justLoaded = false
	var numOfEmptyFrames = chooseEmptyFrame(componentName)
	if (numOfEmptyFrames == 0) {
		loadFirstTexts()
	}
}

// Loads first stanza and transition texts. The first stanza then executes
// loadRemainingStanzas() to load the rest of the stanzas into frames;
// This function is called by navbar.htm
function loadFirstTexts() {
	// If there is a question in the text box, run loadQuestion
	// which will retrieve question
	if (parent.navBar.document.forms[0].questionField.value != "") {
		loadQuestion()
	}
	// Transitions
	var theme1 = parent.navBar.themesInCircuit[0]
	var theme2 = parent.navBar.themesInCircuit[1]
	parent.transNorthEast.location = "transitions/" + theme1 + "-" + theme2 + ".htm"	
	theme1 = parent.navBar.themesInCircuit[1]
	theme2 = parent.navBar.themesInCircuit[2]
	parent.transSouthEast.location = "transitions/" + theme1 + "-" + theme2 + ".htm"	
	theme1 = parent.navBar.themesInCircuit[2]
	theme2 = parent.navBar.themesInCircuit[3]
	parent.transSouthWest.location = "transitions/" + theme1 + "-" + theme2 + ".htm"	
	theme1 = parent.navBar.themesInCircuit[3]
	theme2 = parent.navBar.themesInCircuit[0]
	parent.transNorthWest.location = "transitions/" + theme1 + "-" + theme2 + ".htm"	
}

// Executed by first stanza that is loaded by loadFirstTexts()
// and then by each successively loaded stanza
function loadRemainingStanzas() {
	// Get themesInCircuit array from vavBar and put it in a local variable
	var l_themesInCircuit = parent.navBar.themesInCircuit
	switch (parent.navBar.numStanzasLoaded) {
		case 0:
			// North
			var currentTheme = l_themesInCircuit[0]
			var nextTheme = l_themesInCircuit[1]
			var oppositeTheme = l_themesInCircuit[2]
			var previousTheme = l_themesInCircuit[3]
			var selectedStanza = chooseStanza(currentTheme, nextTheme, oppositeTheme, previousTheme, 1)
			// Path must be specified because first stanza is called by transistion file
			// from /transistions
			parent.northLeft.location = "../" + selectedStanza
			break
		case 1:
			var currentTheme = l_themesInCircuit[0]
			var nextTheme = l_themesInCircuit[1]
			var oppositeTheme = l_themesInCircuit[2]
			var previousTheme = l_themesInCircuit[3]
			var selectedStanza = chooseStanza(currentTheme, nextTheme, oppositeTheme, previousTheme, 2)
			parent.northRight.location = selectedStanza	
			break
		case 2:
			// East
			var currentTheme = l_themesInCircuit[1]
			var nextTheme = l_themesInCircuit[2]
			var oppositeTheme = l_themesInCircuit[3]
			var previousTheme = l_themesInCircuit[0]
			var selectedStanza = chooseStanza(currentTheme, nextTheme, oppositeTheme, previousTheme, 1)
			parent.eastTop.location = selectedStanza
			break
		case 3:
			var currentTheme = l_themesInCircuit[1]
			var nextTheme = l_themesInCircuit[2]
			var oppositeTheme = l_themesInCircuit[3]
			var previousTheme = l_themesInCircuit[0]
			var selectedStanza = chooseStanza(currentTheme, nextTheme, oppositeTheme, previousTheme, 2)
			parent.eastBottom.location = selectedStanza
			break
		case 4:	
			// South
			var currentTheme = l_themesInCircuit[2]
			var nextTheme = l_themesInCircuit[3]
			var oppositeTheme = l_themesInCircuit[0]
			var previousTheme = l_themesInCircuit[1]
			var selectedStanza = chooseStanza(currentTheme, nextTheme, oppositeTheme, previousTheme, 1)
			parent.southRight.location = selectedStanza
			break
		case 5:
			var currentTheme = l_themesInCircuit[2]
			var nextTheme = l_themesInCircuit[3]
			var oppositeTheme = l_themesInCircuit[0]
			var previousTheme = l_themesInCircuit[1]
			var selectedStanza = chooseStanza(currentTheme, nextTheme, oppositeTheme, previousTheme, 2)
			parent.southLeft.location = selectedStanza
			break
		case 6:	
			// West
			var currentTheme = l_themesInCircuit[3]
			var nextTheme = l_themesInCircuit[0]
			var oppositeTheme = l_themesInCircuit[1]
			var previousTheme = l_themesInCircuit[2]
			var selectedStanza = chooseStanza(currentTheme, nextTheme, oppositeTheme, previousTheme, 1)
			parent.westBottom.location = selectedStanza
			break
		case 7:
			var currentTheme = l_themesInCircuit[3]
			var nextTheme = l_themesInCircuit[0]
			var oppositeTheme = l_themesInCircuit[1]
			var previousTheme = l_themesInCircuit[2]
			var selectedStanza = chooseStanza(currentTheme, nextTheme, oppositeTheme, previousTheme, 2)
			parent.westTop.location = selectedStanza			
			break
/*		case 8:
			// After all files are loaded, display the animated version of each component image
			// instead of the still version; the images are initialized at .7 sec. intervals
			// so they appear to cycle clockwise around the circuit
			setTimeout("parent.componentN.document.componentImage.src = parent.componentN.animatedImage.src", 500)
			setTimeout("parent.componentE.document.componentImage.src = parent.componentE.animatedImage.src", 1200)
			setTimeout("parent.componentS.document.componentImage.src = parent.componentS.animatedImage.src", 1900)
 			setTimeout("parent.componentW.document.componentImage.src = parent.componentW.animatedImage.src", 2600)			
			break
*/
		default:
			// Exit function if value is other 0-7; this will be the case if a user
			// has pressed the browser's reload key
			return
	}
	parent.navBar.numStanzasLoaded = parent.navBar.numStanzasLoaded + 1
	//alert(selectedStanza)
}

// ************************************************
// ************ MISCELLANEOUS FUNCTIONS ***********

// Displays prompt; if OK is clicked, history is cleared;
// Used by link on index page
function clearHistoryButton() {
	var clearHistory = confirm("You are about to clear the Soothcircuit's memory.\r\nContinue?")
	if (clearHistory) {
		deleteHist()
	}
}
