var win;
var doc;
var links;
var numLinks;
var 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(index)
{
	if (index > 0)
	{
		links[index-1].style.background = oldbg;
		if (index == numLinks)
			index = 0;
	}
	oldbg = links[index].style.background;
	if (null == oldbg) // is this test necessary ?
		oldbg = "#FFFFFF"; // white
	links[index].style.background="#FFFF10"; // yellow
	index++;
	timerID = setTimeout("scanLinks('" + index + "')", 1000);
	timerRunning = true;
}

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