function windowOpener(url, name, w, h) {
  	eval("msgWindow=window.open(url,name,'width=" + w + ",height=" + h + "');msgWindow.focus();");
}

function windowAdvOpener(url, name, w, h) {
  	eval("msgWindow=window.open(url,name,'width=" + w + ",height=" + h + ",scrollbars=yes,status=yes,resizable=yes');msgWindow.focus();");
}

function windowToolOpener(url, name, w, h) {
  	eval("msgWindow=window.open(url,name,'width=" + w + ",height=" + h + ",scrollbars=yes,status=yes,resizable=yes,location=yes,toolbar=yes');msgWindow.focus();");
}

glossWin = null;
function glossary(term) {
	if ((glossWin == null) || (glossWin.closed)) {
	  	glossWin=window.open("frameless/glossary.html#" + term, "Glossary", "width=480,height=400,scrollbars=yes,status=yes,resizable=yes", true);
  	}
  	glossWin.focus();
}


function openPageInFset(page) {
	openPageInFsetWindow(page, window);
	return;
}

function openPageInFsetWindow(page, whichWindow) {
	var rowV = 50;
	var colW = 160;
	if(navigator.appName == "Netscape") {
		colW = 165;
	}

	var d = whichWindow.document;
	d.write("<HTML>");
	d.write("<HEAD>");
	d.write("<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=iso-8859-1'>");
	d.writeln("<TITLE>The Particle Adventure</TITLE>");
	d.writeln("</HEAD>");
	d.writeln("<FRAMESET rows = '" + rowV + ", *' frameborder='no'  SCROLLING = NO BORDER = 0 FRAMEBORDER = 0 FRAMESPACING = 0>");
	if(parseInt(navigator.appVersion) < 4) {
		d.writeln("	<FRAME NAME = 'topbar' SRC = 'other_top.html' SCROLLING = NO NORESIZE BORDER = 0 FRAMEBORDER = 0 FRAMESPACING = 0>");
	} else {
		d.writeln("	<FRAME NAME = 'topbar' SRC = 'top.html' SCROLLING = NO NORESIZE BORDER = 0 FRAMEBORDER = 0 FRAMESPACING = 0>");
	}
	d.writeln("	<FRAMESET cols = '" + colW+ ",*' frameborder='no'  SCROLLING = NO BORDER = 0 FRAMEBORDER = 0 FRAMESPACING = 0>");
	if((parseInt(navigator.appVersion) < 4) || ((navigator.platform + "").indexOf("Linux") != -1)) {
		d.writeln("		<FRAME NAME = 'nav' SRC = 'other_nav.html' SCROLLING = NO NORESIZE BORDER = 0 FRAMEBORDER = 0 FRAMESPACING = 0>");
  	} else {
		d.writeln("		<FRAME NAME = 'nav' SRC = 'nav.html' SCROLLING = NO NORESIZE BORDER = 0 FRAMEBORDER = 0 FRAMESPACING = 0>");
	}
	d.writeln("		<FRAME NAME = 'body' SRC = '" + page + "' SCROLLING = AUTO NORESIZE BORDER = 0 FRAMEBORDER = 0 FRAMESPACING = 0>");
	d.writeln("	</FRAMESET>");
	d.writeln("</FRAMESET>");
	d.writeln("");
	d.writeln("<NOFRAMES>");
	d.writeln("	<P>");
	d.writeln("	The Particle Adventure uses frames and javascript. Please set your browser preferences to support frame pages, then reload.");
	d.writeln("	<P>");
	d.writeln("	<A HREF='index.html'>&lt;&lt; BACK</A>");
	d.writeln("</NOFRAMES>");
	d.writeln("</HTML>");
	
	d.close();
	return true;
}



function styleResizeCheck() {
	if (navigator.appName == "Netscape") {
		document.location = document.location;
	}
}


function javaSafeString(str) {
	// This is a really dumb hack to get around the fact that Navigator and IE interpret "//" as / and // respectively
	slashString = "\\";
	
	while ((str.indexOf("'") != -1) && (str.charAt(str.indexOf("'") - 1) != slashString.charAt(0)))  {
		first = str.substring(0,str.indexOf("'"));
		second = str.substring(str.indexOf("'") + 1, str.length);
		str = first + slashString.charAt(0) + "'" + second;
	}
	return str;
}




function getIndexOfPage(url) {
	var pageName = url +"";
	if (pageName.indexOf('/') != -1) {
		pageName = pageName.substring(pageName.lastIndexOf('/')+1,pageName.length);
	}
	for (i = pageList.length - 1; i >= 0; i--) {
		if(pageList[i].indexOf(":" + pageName) != -1) {
			return i;
		}
	}
	return -1;
}

function getListOfIndex(entry) {
	lVal = 1;
	depth = getDepthOfIndex(entry);
	for (;entry >= 0; entry--) {
		e_depth = getDepthOfIndex(entry);
		if (depth > e_depth) {
			lVal++;
		}
		if (depth != e_depth) {
			depth = e_depth;
		}
	}
	return lVal;
}


function getDepthOfIndex(entry) {
	var line = pageList[entry];
	var depth = 0;
	while(line.charAt(depth) == '#') {
		depth++;
	}
	return depth;
}

function getIndexOfLesserDepth(index) {
	var depth = getDepthOfIndex(index);
	if (depth > 0) {
		while (getDepthOfIndex(index) >= depth) {
			index--;
		}
	}
	return index;
}

function getTitle(index) {
	if (index < 0)
		return "";
	var i = 0;
	var line = pageList[index];
	while (line.charAt(i) == '#') {
		i++;
	}
	return line.substring(i,line.lastIndexOf(':'));
}

function getURL(index) {
	if(index < 0)
		return "";
	line = pageList[index];
	return line.substring(line.lastIndexOf(':') + 1, line.length);
}

/******************************/
/* Title function *************/
/******************************/
function writeTopTitle() {
	/* New feature: If the user arrived at this page from a non-frame, add a friendly 
	 * navigation aid */
	if(parent.frames.length != 3) {
		pageName = document.location + "";
		pageName = pageName.substring(pageName.lastIndexOf('/') + 1, pageName.length);
		document.write("<P>To navigate the Particle Adventure site, you will need to ");
		document.write("<A HREF=\"javascript:openPageInFset('" + pageName + "');\">load the navigation frameset</A>.");
	} else {	
		pageIndex = getIndexOfPage(document.location);
		var title = "<SPAN CLASS=\"titleName\">" + getTitle(pageIndex) + "</SPAN>";
		var pageDepth = getDepthOfIndex(pageIndex);
		
		while (getDepthOfIndex(pageIndex) > 0) {
			pageIndex = getIndexOfLesserDepth(pageIndex);
			if((parent.nav) && (parent.nav.l_open)) {
				parent.nav.l_open(getListOfIndex(pageIndex));
			}
			url = getURL(pageIndex);
			title = "<FONT COLOR=black>" + getTitle(pageIndex) + "</FONT></A> : " + title;	
			title = "<A HREF = \"" + url + "\" onMouseOver = \"window.status = '" + javaSafeString(getTitle(pageIndex)) + "'; return true;\" CLASS = \"titleSection\">" + title;
		}
	
		document.write(title);
		
		if((parent.topbar) && (parent.topbar.makeUp)) {
			if (pageDepth > 1) {	
				parent.topbar.makeUp();
			} else {
				parent.topbar.makeNoUp();
			}
		}
	}
}

		
function toggleAnswer(number) {
	if (parseInt(navigator.appVersion) < 4) {
		alert("The answer is below in white: hilight it to see the answer.");
		return;
	}
	
	var ns = (document.layers)? true:false;
	var theElement;

	if(ns) {
		eval("theElement = document.answer" + number + ";");
		theElement.visibility =  (theElement.visibility != 'show') ? 'show':'hide';
	} else {
		eval("theElement = document.all.answer" + number + ";");
		theElement.style.visibility =  (theElement.style.visibility != 'visible') ? 'visible':'hidden';
	}
}


function writeOldNav() {
	if (parseInt(navigator.appVersion) < 4) {
		document.write("<FONT COLOR = #FFFFFF>");
	}
	return true;
}

function writeCloseOldNav() {
	if (parseInt(navigator.appVersion) < 4) {
		document.write("</FONT>");
	}
	return true;
}



var listTracker;
var scroll = 0;

function moveList(dy) {
	if((scroll == 0) && (dy > 0)) {
		return;
	}
	scroll += dy;
	if(scroll > 0) {
		scroll = 0;
	}
	listTracker[0].build(0,scroll + minY);
}

width = 160;
height = 26;

minY = 30;

function initNavList() {
  if(parseInt(navigator.appVersion) < 4) {
    parent.nav.location = "other_nav.html";
    return;
  }
  
  depth = 0;
  listTracker = new Array (new List(true, width, height));
  nameTracker = new Array ("");
  
  for (index = 0; index < pageList.length; index++) {
  	 while (getDepthOfIndex(index) < depth) {
  	 	listTracker[depth - 1].addList(listTracker[depth], nameTracker[depth]);
  	 	depth--;	
  	 }
  	 
  	 prefix = "<A HREF='" + getURL(index) + "' target = 'body' ";
  	 if (depth == 0) {
  	 	prefix += " class='chapter'>";
  	 } else {
  	 	prefix += " class='page'>";
  	 }
  	 if ((index + 1 < pageList.length) && (getDepthOfIndex(index + 1) > depth)) {
  	 	depth++;
  	 	nameTracker[depth] = prefix + getTitle(index) + "</SPAN></A>";
  	 	listTracker[depth] = new List(false, width, height);
  	 } else {
  	 	listTracker[depth].addItem(prefix + getTitle(index) + "</A></SPAN>");
  	 }
  } 
  while (0 < depth) {
  	 	listTracker[depth - 1].addList(listTracker[depth], nameTracker[depth]);
  	 	depth--;	
  }
  	 
  listTracker[0].build(0,minY);
  
  parent.nav.l_open(getListOfIndex(getIndexOfPage(parent.body.location) - 1));
 }





function pageInChapter(index, pageIndex) {
	if (index > pageIndex) 
		return false;
		
	cdepth = getDepthOfIndex(index);
	pdepth = getDepthOfIndex(pageIndex);

	if (cdepth > pdepth) {
		return false;
	}
	
	pdepth = cdepth;
			
	for (;index <= pageIndex; index++) {
		if (getDepthOfIndex(index) < pdepth) {
			return false;
		}
		if (index == pageIndex)
			return true;
	}
	return false;
}
