// VERSION NOTES:
// *  DONE: current link cycle thread terminates on link activation
//    (thanks to JavaScript phenom, Kris Jordan!)
//  * DONE: "strange" global variable issue solved (again, thanks Kris!)
//  * image links now highlighted during scan iter, just as text links had been
//  * mouseovers simulated to provoke DHTML content (eg. see www.unc.edu)
//  * DONE: only the space bar activates a link
//  * DONE: link scan starts automatically
//  * highlighted link destination URL shown in status bar
//  * DONE: "HTB_" prefix applied uniformly to variable and function names to
//	  avoid XUL namespace conflicts
//  * DONE: link opens new page in same window (actually, document)
//  * DONE: Home, Back, and Forward buttons: for now, done with 'H','B','F'
//	  Later, these functions will be mapped to toolbar buttons that will
//	  be scanned just as the links of a page are
//  * code cleaned up


// HTB_ is used as a prefix for all variables to denote they are used
// with the Hawking Toolbar.  This is necessary because the script is
// used for a Firefox extension, in which the namespace is that of
// XUL.  So to prevent any variable name conflicts with XUL variables, we
// therefore give all our variables the HTB_ prefix.

var HTB_win;
var HTB_doc;
var HTB_links;
var HTB_numLinks;
var HTB_index = 0;
var HTB_firstIter = true; // first iteration of scanlinks()?
var timerID = null;
var timerRunning = false;
var HTB_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 HTB_scanLinks()
{
	if (HTB_firstIter)
	{
		HTB_index = 0;
		HTB_firstIter = false;
	}
	else // if (HTB_index > 0)
	{
		HTB_links[HTB_index-1].style.background = HTB_oldbg;
		if (HTB_index == HTB_numLinks)
			HTB_index = 0;
	}
	HTB_oldbg = HTB_links[HTB_index].style.background;
	if (null == HTB_oldbg) // is this test necessary ?
		HTB_oldbg = "#FFFFFF"; // white
	// highlight link
	HTB_links[HTB_index].style.background="#FFFF10"; // yellow
	// show link href in status bar
	HTB_win.status = HTB_links[HTB_index].href;
//	if (HTB_links[HTB_index].hasAttribute("style") )
//		alert("style!");
//	alert(HTB_links[HTB_index].href);
//	alert("link# " + HTB_index + " is : " + HTB_links[HTB_index].href); 
	HTB_index++;
	timerID = setTimeout("HTB_scanLinks()", 1000);
	timerRunning = true;
}

function HTB_init(_HTB_win) 
{
	HTB_win = _HTB_win;
	HTB_doc = HTB_win.document;
	HTB_links = HTB_doc.links;
	HTB_numLinks = HTB_links.length;
	HTB_win.onkeypress = HTB_doKeyPress;
	if(timerRunning)
    	clearTimeout(timerID)
   	timerRunning = false;
	window.status = "testing status bar";
	HTB_firstIter = true; // is this really necessary?
	HTB_scanLinks();
  	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_doKeyPress(HTB_event)
{
	if (HTB_event.keyCode == 13) // if <ENTER> then launch link
		HTB_doHyperlink();
	if (HTB_event.charCode == 66) // if a 'B' for BACK
		window.back();
	if (HTB_event.charCode == 70) // if a 'F' for FORWARD
		window.forward();
	if (HTB_event.charCode == 72) // if a 'H' for HOME
		window.home();
//	alert(HTB_event.charCode); // show key code, for development
}

function HTB_doHyperlink()
{
	var HTB_nextURL = HTB_links[HTB_index - 1].href;
	// subtract 1 b/c index is incremented before next iter scheduled
	// think about this, is this the correct title for our new window ??????
//	var HTB_nextTitle = HTB_links[HTB_index].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 close() !!!
	if(timerRunning)
    	clearTimeout(timerID)
   	timerRunning = false;
	window.dump("opening / closing!");
	// opens link in new window
	//window.open(HTB_nextURL, window.name); 
	// opens link in same window ("document", really)
	location.href = HTB_nextURL;
	HTB_init(window.self);
	//document.close();
	//newWin.focus();
}

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

