var arrPos;
var prePos;
var numElements;
var spans;
var reverse = false;
var termTags;
var term;
var tmpStr;
var argList;
var termArr = new Array( 100 );
var termCount = new Array( 10 );
for (var i=0; i < 10; i++) {
	termCount[i] = 0;
}
for (var i=0; i<100; i++) {
	termArr[i] = new String(" ");
}

function termIn( arr, pos, str )
{
	for (var q=0; q<10; q++) {
		if ((new String(arr[(q * 10) + pos])).toLowerCase() == (str).toLowerCase())
			return true;
	}
	return false;
}

// Places terms in the term links
function getTerms()
{
	termTags = getElementsByClass( "gbcnstl", document, "span" );
	for (var i = 0; i < spans.length; i++) 
	{
		idx = parseInt((new String(spans[i].className)).substr(13,2), 10);
		tmpTerm = spans[i].innerHTML;
		if (!termIn( termArr, idx, tmpTerm))
		{
			termArr[idx + (termCount[idx] * 10)] = new String(tmpTerm);
			termCount[idx]++;		
		}
	}
}

// Sets the color and text of the tag links
function setTags()
{
	for (var i=0; i<100; i++) {
		if ((termArr[i] != undefined) && (termArr[i] != " ")) {
			termTags[i].innerHTML = termArr[i];
			termTags[i].className = new String("gbcnstl t0" + String(i%10));
		}
	}

}

// Causes the object 'base' to scroll smoothly from
// 'lastY' to 'thisY'
function smoothScroll ( thisY, lastY, base )
{	
	var mid = 15;
	var maxTime = 15;
	var twixt = ( maxTime - (maxTime % mid) ) / mid;
	var scrollCmd;
	var wait = 5000;

	if ((thisY == 0) && (lastY == 0)) {
		base.scrollTo(0,0);

	} else if (thisY > lastY) {
		offset = ((thisY - lastY) - ((thisY - lastY) % mid)) / mid;
		if (offset > 0) {
			for (var i=lastY; i<thisY; i+=offset) {
				base.scrollTo(0,i);
				var j = 0;
				var today = new Date();
				var msec = today.getTime();
				var esec = msec + twixt;
				for (var k=msec; k<esec; k=k) {
					today = new Date();
					k = today.getTime();
				}
				printDebug( "debug1", "i=" + String(i) );
			}
		}
		base.scrollTo(0,thisY);

	} else if (lastY > thisY) {
		offset = ((lastY - thisY) - ((lastY - thisY) % mid)) / mid;
		if (offset > 0) {
			for (var i=lastY; i>thisY; i-=offset) {
				base.scrollTo(0,i);
				var j = 0;
				var today = new Date();
				var msec = today.getTime();
				var esec = msec + twixt;
				for (var k=msec; k<esec; k=k) {
					today = new Date();
					k = today.getTime();
				}
				printDebug( "debug1", "i=" + String(i) );
			}
		}
		base.scrollTo(0,thisY);
	}
}

// Returns an array of objects of the class specified by 'searchClass'
// contained in the document specified by 'node' 
// 'tag' specifies which type of tag to look at
function getElementsByClass( searchClass, node, tag )
{
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName( tag );
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (var i=0, j=0; i < elsLen; i++)
	{
		if (pattern.test(els[i].className))
		{
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

// Retrieve the args from the GET field
function getArgs()
{
	var args = new Object();
	var query = top.location.search.substring(1);
	//var pairs = query.split("");
	var pairs = query;
	//for (var i=0; i<pairs.length; i++) {
		//var pos = pairs[i].indexOf('=');
		var pos = pairs.indexOf('=');
		//if (pos == -1) continue;
		if (pos == -1) return " ";
		//var argname = pairs[i].substring(0,pos);
		var argname = pairs.substring(0,pos);
		//var value = pairs[i].substring(pos+1);
		var value = pairs.substring(pos+1);
		args[argname] = unescape(value);
	//}
	selElem = args;
	return args;
}

// Used to print debug statments to the debug# input text boxes
function printDebug( bug, value )
{
	document.getElementById(bug).value = value;
}

// Set the frame to the cached document
function setFrame ()
{
	try {
		frames['docFrame'].location.href = argList["page"].toString();
		printDebug("debug1", argList["page"].toString());	

	} catch (err) {
		printDebug("debug1", "Using Default Document");	
	}
}

// This function should be executed when the page is loaded
// It initializes the spans array
function init()
{
	argList = getArgs();
	setFrame();
}


function initFrame()
{
	spans = getElementsByClass("gbcnst", frames['docFrame'].document, "span");
	numElements = spans.length;
	arrPos = 0;
	prePos = 1;
	getTerms();
	setTags();
}

// Find an element's Y-coord in the docFrame
function getYof( elm )
{
	var pos = 0;
	var done = false;
	while (!done) {
		pos += elm.offsetTop;
		//try {
		elm = elm.offsetParent;
		if (elm == null)
		//} catch (err) {
			done = true;
		}//
	return pos;
}
/*function getYof( elm )
{
	var pos = 0;
	var done = false;
	while (!done) {
		pos += elm.offsetTop;
		elm = elm.parentNode;
		try {
			if (elm.nodeName == "BODY")
				done = true;
		} catch (err) {
			printDebug("debug2","Does not have name element");
		}
	}
	return pos;
}*/

// These functions navigate between the different terms 
function setTerm()
{
	spans[prePos].className = spans[prePos].className.substr(0, 15);
	spans[arrPos].className = spans[arrPos].className + new String("x");
	var oldY = getYof(spans[prePos]);
	var newY = getYof(spans[arrPos]);
	if ((newY > 40) && (oldY > 40))
		smoothScroll( newY - 40, oldY - 40, frames.docFrame );
	else if (oldY > 40)
		smoothScroll( 0, oldY - 40, frames.docFrame );
	else if (newY > 40)
		smoothScroll( newY - 40, oldY, frames.docFrame );
	else 
		smoothScroll( 0, 0, frames.docFrame );
}

// Increments the position in the 'spans' array
function incArrPos()
{
	arrPos += 1;
	if (arrPos >= numElements)
	{
		arrPos = 0;
	}
}

// Decrements the position in the 'spans' array
function decArrPos()
{
	arrPos -= 1;
	if (arrPos < 0)
	{
		arrPos = numElements - 1;
	}
}

// Jumps to the next object in the 'spans' array
function nextTerm()
{
	prePos = arrPos;
	incArrPos();
	setTerm();
}

// Jumps to the previous tobject in the 'spans' array
function prevTerm()
{
	prePos = arrPos;
	decArrPos();
	setTerm();
}

// Navigates to the next element in the 'spans' array that
// matches the id specified by sTerm
function nextTermByName( sTerm )
{
	prePos = arrPos;
	incArrPos();
	var finite = arrPos;
	var infinite = false;

	while ((sTerm.toLowerCase() != spans[arrPos].innerHTML.toLowerCase()) && (!infinite))
	{
		incArrPos( sTerm );
		if (arrPos == finite)
		{
			infinite = true;
		}
	}
	setTerm();
}

// Navigates to the previous element in the 'spans' array that
// matches the id specified by sTerm
function prevTermByName( sTerm )
{
	prePos = arrPos;
	decArrPos();
	var finite = arrPos;
	var infinite = false;

	while ((sTerm.toLowerCase() != spans[arrPos].innerHTML.toLowerCase()) && (!infinite))
	{
		decArrPos();
		if (arrPos == finite)
		{
			infinite = true;
		}
	}
	setTerm();
}

// This is called by a tag link when it is clicked. It searches for 
// a term in the direction specified by the boolean 'reverse' variable
// reverse = false //forward
function termByName( sTermNum )
{
	printDebug("debug2",parseInt(sTermNum, 10));
	var varVal = parseInt(sTermNum, 10);
	if (reverse)
		prevTermByName( new String(termArr[varVal]) );
	else
		nextTermByName( new String(termArr[varVal]) );
}

// This is called when the reverse tag link is clicked, and flips both
// the value of the 'reverse' variable and the text of the reverse link.
// If the direction is forward, the link says "reverse". If the direction
// is reverse, the link says "forward". The link text specifies which
// direction you will switch to if you click on it.
function flipFlop()
{
	if (reverse) {
		document.getElementById('anchorFlipFlop').innerHTML = new String("Reverse");
		reverse = false;
	} else {
		document.getElementById('anchorFlipFlop').innerHTML = new String("Forward");
		reverse = true;
	}
	printDebug( "debug1", String((new Date).getTime()));
}

