var win;
var doc;
var HTB_links;
var HTB_numLinks;
var HTB_index = 0;
// threading issues keep HTB_Index = 0 outside of scanLinks, so we have a
// public accessor copy that is updated: HTB_publicIndex.  This is fixed, but
// I'd still like to understand it fully.
var HTB_publicIndex = HTB_index;
var timerID = null;
var timerRunning = false;
var oldbg;

// The odd structure of this function is due to constraints 
// of Javascript's threading model.  For setTimeout() to work
// correctly, ie. to actually pause before doing next link, the
// function must call itself as one of that function's arguments.
function scanLinks(HTB_index)
{
	if (HTB_index > 0)
	{
		HTB_links[HTB_index-1].style.background = oldbg;
		if (HTB_index == HTB_numLinks)
			HTB_index = 0;
	}
	oldbg = HTB_links[HTB_index].style.background;
	if (null == oldbg) // is this test necessary ?
		oldbg = "#FFFFFF"; // white
	HTB_links[HTB_index].style.background="#FFFF10"; // yellow
//	alert("link# " + HTB_index + " is : " + HTB_links[HTB_index].href);
	HTB_publicIndex = HTB_index; 
	HTB_index++;
	timerID = setTimeout("scanLinks('" + HTB_index + "')", 1000);
	timerRunning = true;
}

function highlightLinks(_win) 
{
	win = _win;
	doc = win.document;
	HTB_links = doc.links;
	HTB_numLinks = HTB_links.length;
	if(timerRunning)
    	clearTimeout(timerID)
   	timerRunning = false;
   	HTB_index = 0;
	scanLinks(HTB_index);
  	var str = "There are " + HTB_numLinks + " links.";
  	var linkHref = document.createTextNode(str);
   	var lineBreak = document.createElement("br");
	document.body.appendChild(lineBreak);
	document.body.appendChild(linkHref);
  	document.body.appendChild(lineBreak);
}

function HTB_hyperlink()
{
	var HTB_nextURL = HTB_links[HTB_publicIndex].href;
	// think about this, is this the correct title for our new window ??????
	var HTB_nextTitle = HTB_links[HTB_publicIndex].innerHTML;
//	alert("There are " + HTB_numLinks + " links\n" +
//		  "Current link# is: " + HTB_publicIndex + "\n" +
//		  "You want to link to: " + HTB_nextURL + "\n" + 
//		  "With a title of : " + HTB_nextTitle);

	// this isn't working!  not the threads, not the close() !!!
	if(timerRunning)
    	clearTimeout(timerID)
   	timerRunning = false;
	window.dump("opening / closing!");
	window.open(HTB_nextURL, window.name); 
	//window.close();
	//newWin.focus();
}

window.onkeypress = HTB_hyperlink;
//document.addEventListener("keypress", HTB_hyperlink, false);
//window.onload(document.addEventListener("click", HTB_hyperlink(), false);
//window.onload = highlightLinks(window.self);

