var theCharacterTimeout = 50;
var theCharIncrement = 1;
var theStoryTimeout     = 5000;
var theWidgetOne        = "_";
var theWidgetTwo        = "-";
var theWidgetNone       = "";

var theCurrentStory;
var theCurrentLength;
var theItemCount;
var newsList ;
var theStorySummary ;
var theAnchorObject;

// Functions for Latest News ticker.
// Ticker startup
function startTicker()
{
	// Define run time values
	theCurrentStory     = -1;
	theCurrentLength    = 0;
	// Locate base objects
	if (document.getElementById) {
			theAnchorObject = document.getElementById("tickerAnchor");	/*More efficient than JQuery */
			
			//Due to a performance problem in IE7 we need to show the chars 5 at a time
			if(isIE7())
			{
				theCharacterTimeout = 250;
				theCharIncrement = 5;
			}
			
			runTheTicker();   	
		 }
	else {
            document.write("<style>.ticki{display:none;}.ticko{border:0px; padding:0px;}</style>");
            return true;
	}
}
// Ticker main run loop
function runTheTicker()
{
	var myTimeout;  
	// Go for the next story data block
	if(theCurrentLength == 0)
	{
		theCurrentStory++;
		theCurrentStory      = theCurrentStory % theItemCount;
		
		theStorySummary = newsList[theCurrentStory].title;

		//Show the built in link if there is one, else construct one from the GUID
		if(newsList[theCurrentStory].link!="")
			theAnchorObject.href = newsList[theCurrentStory].link
		else
			theAnchorObject.href = "ShowNewsItem.do?"+newsList[theCurrentStory].id
	}
	
	// Stuff the current ticker text into the anchor
	// Modify the length for the substring and define the timer
	if(theCurrentLength <= theStorySummary.length)
	{
		theAnchorObject.innerHTML = theStorySummary.substring(0,theCurrentLength) + whatWidget();
		theCurrentLength+=theCharIncrement;
		myTimeout = theCharacterTimeout;
	}
	else
	{
		theAnchorObject.innerHTML = theStorySummary;
		theCurrentLength = 0;
		myTimeout = theStoryTimeout;
	}
	// Call up the next cycle of the ticker
	setTimeout("runTheTicker()", myTimeout);
}

// Widget generator
function whatWidget()
{
	if(theCurrentLength == theStorySummary.length)
	{
		return theWidgetNone;
	}

	if((theCurrentLength % 2) == 1)
	{
		return theWidgetOne;
	}
	else
	{
		return theWidgetTwo;
	}
}

function initTicker()
{
   
   
	//Only initialise if news on page
	if($("#ticker").length >0 )
	{
		//Get the feed and start the ticker
		//Also update the server with the browser width.
		$.getFeed({
		       url: "LatestNews.do?wld_id="+getWld_id()+"&browser_width="+$("body").width(),
		       success: function(feed) {		    			
						newsList = feed.items;
						theItemCount = newsList.length;
	
						if(theItemCount > 0)
						{
							//Sort items in list randomly
							newsList.sort(function() {return 0.5 - Math.random()});
							$(".tickerContent").show();
							startTicker();
						}
							
		       }
		   });

	}
}