// --- Global variable stuff here
var theItemCount;
var theCurrentStory;
var theCurrentLength;
var theStorySummary;
var theTargetLink;
var theCharacterTimeout;
var theStoryTimeout;
var theBrowserVersion;
var theWidgetOne;
var theWidgetTwo;
var theSpaceFiller;
var theLeadString;
var theStoryState;

// --- Check for old browser and force applet
theBrowserVersion = parseInt(navigator.appVersion);

if (theBrowserVersion < 4)
{
	location.href = "about:blank";
}

// --- Only run for V4 browsers (check browser again here - some old browsers won't do this inline)
function startTicker()
{
	/* theBrowserVersion = parseInt(navigator.appVersion);

	if (theBrowserVersion < 4)
	{
		location.href = "about:blank";
		return;
	}*/

	// ------ Set up initial values
	theCharacterTimeout =   50;
	theStoryTimeout     = 3000;
	theWidgetOne        =  "|";
	theWidgetTwo        =  "|";



	// ------ Set up initial values
	theStoryState       = 1;          
	theItemCount        = document.getElementById("itemcount").innerHTML;
	theCurrentStory     = -1;
	theCurrentLength    = 0;
	theLeadString       = "";
	theSpaceFiller      = " &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<BR><BR><BR>";


	// ------ Begin the ticker       
	runTheTicker();
}



// --- The basic rotate function
function runTheTicker()
{
	if(theStoryState == 1)
	{
		setupNextStory();
	}

	if(theCurrentLength != theStorySummary.length)
	{
		drawStory();
	}
	else
	{
		closeOutStory();
	}
}



// --- Index to next story
function setupNextStory()
{
	theStoryState = 0;

	theCurrentStory++;

	theCurrentStory = theCurrentStory % theItemCount;

	var div = document.getElementById("story_" + (theCurrentStory+1));          

	var divs = div.getElementsByTagName("DIV");

	if(divs[0].innerText)
		theStorySummary = divs[0].innerText;
	else
		theStorySummary = divs[0].textContent;
	
	if(!theStorySummary)theStorySummary = ""
	
	theTargetLink   = divs[1].innerHTML;

	theTarget       = divs[2].innerHTML;

	//if(theTargetLink == "")
	//{
	//   theTargetLink = document.body.children.incoming.children.stories.children[theCurrentStory].children.UrlLink.innerText;
	//}

	theCurrentLength = 0;

	document.getElementById("hottext").href = theTargetLink;
	document.getElementById("hottext").target = theTarget;
}



// --- Draw a teletype line
function drawStory()
{

	var myWidget;

	if((theCurrentLength % 2) == 1)
	{
		myWidget = theWidgetOne;
	}
	else
	{
		myWidget = theWidgetTwo;
	}
	document.getElementById('hottext').innerHTML = theLeadString + theStorySummary.substring(0,theCurrentLength) + myWidget + theSpaceFiller;
	theCurrentLength++;
	setTimeout(runTheTicker, theCharacterTimeout);
}

// --- Finalise the item
function closeOutStory()
{
	document.getElementById('hottext').innerHTML = theLeadString + theStorySummary + theSpaceFiller;
	theStoryState = 1;
	setTimeout(runTheTicker, theStoryTimeout);
}

   
window.onload = function(){
startTicker();
}