var agt = navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1) && ((agt.indexOf("mac")==-1) && ((is_major > 4) || (agt.indexOf("msie 4")==-1))));
var is_gecko = (agt.indexOf("gecko") != -1);

// This function simply performs the same purpose as position: fixed for IE browsers that do not support that CSS.  This
// works fine and is not the subject of the bug.
function navBar (leftspacing, topspacing, suffix, refresh) {
	if (is_ie) {
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
		document.all['navbar_' + suffix].style.position = "absolute";
		document.all['navbar_' + suffix].style.left = leftspacing + x;
		document.all['navbar_' + suffix].style.top = topspacing + y;
		document.all['edge_' + suffix].style.position = "relative";
		document.all['edge_' + suffix].style.left = -105;
		document.all['edge_' + suffix].style.top = -100;
		document.all['page_' + suffix].style.position = "relative";
		document.all['page_' + suffix].style.left = 0;
		document.all['page_' + suffix].style.top = -3;
		document.all['position_' + suffix].style.position = "relative";
		document.all['position_' + suffix].style.left = 0;
		document.all['position_' + suffix].style.top = -3;
		document.all['navmin_' + suffix].style.position = "absolute";
		document.all['navmin_' + suffix].style.left = leftspacing + x;
		document.all['navmin_' + suffix].style.top = topspacing + y;
		setTimeout("navBar(" + topspacing + ", " + leftspacing + ", \'" + suffix + "\', " + refresh + ");",refresh);
	}
}

// This function allows for the scrolling of the iframe by using links (either containing images or text) that, when
// the cursor hovers over the appropriate one, scrolls the iframe without the use of a standard scrollbar.
function scrollNav (dir, distance, refresh, page) {
	// This should apply for IE, Opera and Mozilla, as the window.frames collection is a proprietary JavaScript
	// extension.
	if (window.frames) {
		iframe = window.frames[0];
	// This should apply to all other browsers that are DOM level 1
	} else if (document.getElementsByTagName) {
		// Get all iframe elements (there should be only one)
		iframenodes = document.getElementsByTagName("iframe");
		for (i = 0;i < iframenodes.length;i++) {
			if ((iframenodes[i].getAttribute("src") == "navbar.htm") || (iframenodes[i].getAttribute("src") == "nav_" + page + ".htm")) {
				// Ensure that the single iframe element is a node and not a node collection.
				iframe = iframenodes[i];
				break;
			}
		}
	}
	if (dir == "up") {
		iframe.scrollBy(0, 0 - distance);
	} else if (dir == "down") {
		iframe.scrollBy(0, distance);
	}
	// This simply sets a timeout to continue scrolling as long as the cursor remains over the link
	scrollNavTimer = setTimeout("scrollNav(\'" + dir + "\', " + distance + ", " + refresh + ");",refresh);
}

// This function should, and apparently does, end the timeout and thus halts scrolling when the cursor moves off
// the link.
function stopScroll () {
	clearTimeout(scrollNavTimer);
}

function navSrc (src, page) {
	// This should apply for IE, Opera and Mozilla, as the window.frames collection is a proprietary JavaScript
	// extension.
	if (window.frames) {
		iframe = window.frames[0];
	// This should apply to all other browsers that are DOM level 1
	} else if (document.getElementsByTagName) {
		// Get all iframe elements (there should be only one)
		iframenodes = document.getElementsByTagName("iframe");
		for (i = 0;i < iframenodes.length;i++) {
			if ((iframenodes[i].getAttribute("src") == "navbar.htm") || (iframenodes[i].getAttribute("src") == "nav_" + page + ".htm")) {
				// Ensure that the single iframe element is a node and not a node collection.
				iframe = iframenodes[i];
				break;
			}
		}
	}
	// And change the iframe src
	iframe.location = src
}

// The onload script (at the end of the page)
function load (leftspacing, topspacing, suffix, refresh) {
	navBar(leftspacing, topspacing, suffix, refresh);
	getPosition(refresh);
}

// The function to get the current page position and then write it to the NavBar
function getPosition (refresh) {
	var hPositionArray = new Array();
	var hTitles = new Array();
	// Get the current offset
	offset = document.body.scrollTop;
	// It better adhere to DOM level 1...
	if (document.getElementsByTagName) {
		hNodes = document.getElementsByTagName("h1");
		for (i = 0;i < hNodes.length;i++) {
			// Get the children of the h1 nodes (the collection of one a element)
			hChildren = hNodes[i].childNodes;
			for (j = 0;j < hChildren.length;j++) {
				// Get the children of the a node (the collection of the inner text)
				aChildren = hChildren[j].childNodes;
				for (k = 0;k < aChildren.length;k++) {
					// Get the text
					innerText = aChildren[k].data;
				}
			}
			// Add the inner text to the hTitles array, and add its position on the page as well.
			hTitles.push(innerText);
			hPositionArray.push(hNodes[i].offsetTop);
		}
		// Same for h2
		hNodes = document.getElementsByTagName("h2");
		for (i = 0;i < hNodes.length;i++) {
			hChildren = hNodes[i].childNodes;
			for (j = 0;j < hChildren.length;j++) {
				aChildren = hChildren[j].childNodes;
				for (k = 0;k < aChildren.length;k++) {
					innerText = aChildren[k].data;
				}
			}
			hTitles.push(innerText);
			hPositionArray.push(hNodes[i].offsetTop);
		}
		// And h3
		hNodes = document.getElementsByTagName("h3");
		for (i = 0;i < hNodes.length;i++) {
			hChildren = hNodes[i].childNodes;
			for (j = 0;j < hChildren.length;j++) {
				aChildren = hChildren[j].childNodes;
				for (k = 0;k < aChildren.length;k++) {
					innerText = aChildren[k].data;
				}
			}
			hTitles.push(innerText);
			hPositionArray.push(hNodes[j].offsetTop);
		}
		// Make sure the arrays are sorted by the position
		hSortArrays = symmSort(hPositionArray, hTitles);
		hPositionArray = hSortArrays[0];
		hTitles = hSortArrays[1];
		// Now get the position span's text node.
		spanNodes = document.getElementsByTagName("span");
		for (i = 0;i < spanNodes.length;i++) {
			if (spanNodes[i].className = "position") {
				spanChildren = spanNodes[i].childNodes;
				for (j = 0;j < spanChildren.length;j++) {
					innerText = spanChildren[j];
				}
			}
		}
		// Now for the main part.
		// First, escape it if above the first h element
		if (offset < hPositionArray[0]) {
			innerText.deleteData(0, innerText.length - 2);
			innerText.replaceData(0, innerText.length, hTitles[0]);
		// Second, escape it if below the last h element
		} else if (offset > hPositionArray[hPositionArray.length - 1]) {
			innerText.deleteData(0, innerText.length - 2);
			innerText.replaceData(0, innerText.length, hTitles[hPositionArray.length - 1]);
		} else {
			// Otherwise, go through all of the hPositions...
			for (i = 0;i < hPositionArray.length;i++) {
				// And if the offset is greater than the current hPosition and less than that of the next (i.e. between
				// the two)...
				if ((offset > hPositionArray[i]) && (offset < hPositionArray[i + 1])) {
					// Then set the inner text and break the loop.
					innerText.deleteData(0, innerText.length - 2);
					innerText.replaceData(0, innerText.length, hTitles[i]);
				}
			}
		}
		// Add a reset timer
		setTimeout("getPosition(" + refresh + ");",refresh);
	}
}

function symmSort (array1, array2) {
	//initialize:
	var copy1 = new Array();
	var copy2 = new Array();
	var smartDouble = new Array();
	//make copies:
	for (i = 0;i < array1.length;i++) {
		copy1.length++;
		copy1[copy1.length - 1] = array1[i];
		smartDouble.length++;
		smartDouble[smartDouble.length - 1] = 0;
	}
	copy2.length = copy1.length;
	//sort copies:
	copy1.sort(compareNumbers);
	//core:
	var redrawCopy2 = 0;
	for (x = 0;x < copy1.length;x++) {
		for (y = 0;y < array1.length;y++) {
			if (copy1[x] == array1[y]) {
				if (smartDouble[y] == 1) {
					continue;
				}
				copy2[redrawCopy2++] = array2[y];
				smartDouble[y] = 1;
			}
		}
	}
	/* keep this comment to reuse freely:
	http://www.unitedscripters.com */
	return new Array(copy1,copy2);
}

function compareNumbers(a, b) {
	return a - b;
}

function navBarSize(dir, suffix) {
	if (document.getElementById) {
		navDiv = document.getElementById("navbar_" + suffix);
		minDiv = document.getElementById("navmin_" + suffix);
	}
	if (dir == "min") {
		navDiv.style.visibility = "hidden";
		minDiv.style.visibility = "visible";
	} else if (dir == "max") {
		navDiv.style.visibility = "visible";
		minDiv.style.visibility = "hidden";
	}
}