//<!--
// Links Menu Javascripts
// ----------------------
// Copyright 2005 Sherman J. Silber.  The Infertility Center of Saint Louis.  http://www.infertile.com

/* globals declarations */
var nLinksAvailable = 0;		/* DO NOT EDIT HERE. Value is set below, near top of <BODY> tag. */
var minNumOfLinksToPrint = 0;	/* Ditto. DO NOT EDIT HERE. */
var maxNumOfLinksToPrint = 0;	/* Ditto. DO NOT EDIT HERE. (If no new value is set below, no maximum (except, of course, mainBodyHeight and nLinksAvailable) will apply. */
var nLinksAlreadyPrinted = 0;
var mainBodyHeight = 0;
var alreadyPrintedList = new Array();

/* Set all truth values to zero in the table of links which have already been printed. */
function initializeAlreadyPrintedList() {
	for (var i = 0 ; i < nLinksAvailable ; i++) alreadyPrintedList[i] = 0;
}

/* Recursively find links in random order and request a printing of each unique one. */
/* Break recursion when the list of links is depleted. Or break when the printed list is as high as the main body's content (assuming a minimum number of links has already been printed). */
function getAndPrintRandomLinks() {
	if (isLinkListHighEnough()  ||  nLinksAlreadyPrinted >= nLinksAvailable) {
		return;
	} else {
		var j = Math.floor(Math.random() * nLinksAvailable);
		if (alreadyPrintedList[j] == 0) {
			alreadyPrintedList[j] = 1;
			var linkElement = document.getElementById('linkListItem_' + j);
			if (linkElement.href != document.URL) {
				printSingleLinkRow(linkElement);
				nLinksAlreadyPrinted++;
			}
		}
		getAndPrintRandomLinks();
	}
}

function updateMainBodyHeight() {
	mainBodyHeight = document.getElementById('mainBodyContent').offsetHeight;
}

function leftMenuHeight() {
	return (document.getElementById('leftMenuContent').offsetHeight);
}

/* Compares height of the left menu with the height of the main content of the page. 
/* To save a couple cycles, updateMainBodyHeight() isn't called here (since it's static anyway). Be sure to call it at some point after the browser loads <div> mainBodyContent. */
function isLinkListHighEnough() {
	if (nLinksAlreadyPrinted >= maxNumOfLinksToPrint  &&  maxNumOfLinksToPrint > 0) return 1;
	if (minNumOfLinksToPrint > nLinksAlreadyPrinted) return 0;
	if (mainBodyHeight) {	/* Just checking in this "if" statement whether "offsetHeight" is supported by this browser. */
		if (mainBodyHeight - 60 > leftMenuHeight()) {  /* The 60 here just pretties it up a bit, I think. */
			return 0;
		}
	}
	return 1;
}

function printSingleLinkRow(linkItem) {
	var rowcode = '<table border="0" cellpadding="0" cellspacing="0" valign="top"><tr><td height="6" width="14"></td><td width="129"></td></tr><tr><td></td><td align="left" valign="top" height="6" class="knowledgebaseArticles">';
	rowcode = rowcode + '<a href="' + linkItem.href + '" class="knowledgebaseArticles">' + linkItem.innerHTML + '</a>';
	rowcode = rowcode + '<hr size="1" noshade align="left" width="82\%"></td></tr></table>';
	
	var already = document.getElementById('linksListFrame').innerHTML;
	document.getElementById('linksListFrame').innerHTML = already + rowcode;
}

//-->