// JS document

var tickerList = "#ticker-story-list";
var currentItem = 0;
var currentLength = 0;
var currentText1Length = 0;
var currentLinkLength = 0;
var currentText2Length = 0;
var tickerItemsText1 = new Array();
var tickerItemsText2 = new Array();
var tickerItemsLinkText = new Array();
var tickerItemHrefs = new Array();
var tickerItemTitles = new Array();
var tickerItemLength = new Array();
var tickerItemCounter = 0;
var placeHolder = " _"
var interval;
var tickerRunning = true;

$(document).ready(function(){
	$('#ticker-holder').css({'display' : 'block'});
	$('#ticker-controls').css({'display' : 'block'});
	
	//set up the ticker controls
	$('#ticker-play-pause').click(function() {
		startStop();
		return false;
	});
	$('#ticker-previous').click(function() {
		backTicker();
		return false;
	});
	$('#ticker-next').click(function() {
		forwardTicker();
		return false;
	});
	
	//copy the text, hrefs and titles of the ul into arrays
	$(tickerList + ' li').each(function(){
		tickerItemsText1[tickerItemCounter] = $(this).find('span:first-child').text();
		if( $(this).find('span').length > 1 )
			tickerItemsText2[tickerItemCounter] = $(this).find('span:last-child').text();
		else
			tickerItemsText2[tickerItemCounter] = '';
		tickerItemsLinkText[tickerItemCounter] = $(this).find('a').text();
		tickerItemHrefs[tickerItemCounter] = $(this).find('a').attr('href');
		tickerItemTitles[tickerItemCounter] = $(this).find('a').attr('title');
		tickerItemLength[tickerItemCounter] = tickerItemsText1[tickerItemCounter].length + tickerItemsText2[tickerItemCounter].length + tickerItemsLinkText[tickerItemCounter].length;
		tickerItemCounter ++;
	});
	//empty the list
	$(tickerList).empty();
	
	//wait 2 seconds and then start the ticker
	setTimeout(startTicker,2000); //time in milliseconds
	
	//set the ticker loop to start
	startTickerLoop();
});

function startTickerLoop() {
	interval = setInterval(startTicker,8000); //time in milliseconds
}

function startTicker() {
	//clear the list
	$(tickerList).empty().append('<li></li>');
	//add the first text holder if it excists
	if( tickerItemsText1[currentItem].length > 0 ) {
		$(tickerList + ' li').append('<span></span>');
	}
	//add the link holder if it exists
	if( tickerItemsLinkText[currentItem].length > 0 ) {
		$(tickerList + ' li').append('<a href="'+ tickerItemHrefs[currentItem] +'" title="'+ tickerItemTitles[currentItem] +'"></a>');
	}
	//add the last holder text if it exists
	if( tickerItemsText2[currentItem].length > 0 ) {
		$(tickerList + ' li').append('<span></span>');
	}
	//set the ticker ticking
	setTimeout(tickItem,50); //time in milliseconds
	
}

function tickItem() {
	//write the ticker item
	if( currentLength <= tickerItemLength[currentItem] ) {
		if( tickerItemsText1[currentItem].length > 0 && currentText1Length <= tickerItemsText1[currentItem].length ) {
			var tickerText = tickerItemsText1[currentItem].substring(0,currentText1Length);
			currentLength ++;
			$(tickerList + ' li span:first-child').text(tickerText);
			currentText1Length ++;
		}else if( tickerItemsLinkText[currentItem].length > 0 && currentLinkLength <= tickerItemsLinkText[currentItem].length ) {
			var tickerText2 = tickerItemsLinkText[currentItem].substring(0,currentLinkLength);
			$(tickerList + ' li a').text(tickerText2);
			currentLength ++;
			currentLinkLength ++;
		}else if( tickerItemsText2[currentItem].length > 0 && currentText2Length <= tickerItemsText2[currentItem].length ) {
			var tickerText3 = tickerItemsText2[currentItem].substring(0,currentText2Length);
			$(tickerList + ' li span:last-child').text(tickerText3);
			currentLength ++;
			currentText2Length ++;
		}
		setTimeout(tickItem,50);
	} else {
		currentLength = 0;
		currentText1Length = 0;
		currentLinkLength = 0;
		currentText2Length = 0;
		//set the next ticker item to tick
		if(currentItem < ( tickerItemLength.length - 1 ) )
			currentItem ++;
		else
			currentItem = 0;
	}

}

function startStop() {
	if(tickerRunning) {
		stopTicker(0);
	}
	else {
		$(tickerList).empty();
		tickerRunning = true;
		startTicker();
		startTickerLoop();
		
		//change the button
		$('#ticker-play-pause').html("<span class='ticker-pause'></span>Pause");
	}
}

function stopTicker(offset) {
	//stop the loop
	clearInterval(interval);
	tickerRunning = false;
	
	currentLength = 10000;
	//clear the list
	$(tickerList).empty().append('<li></li>');
	//add the first text holder if it excists
	if( tickerItemsText1[currentItem].length > 0 ) {
		$(tickerList + ' li').append('<span></span>');
		$(tickerList + ' li span:first-child').text(tickerItemsText1[currentItem]);
	}
	//add the link holder if it exists
	if( tickerItemsLinkText[currentItem].length > 0 ) {
		$(tickerList + ' li').append('<a href="'+ tickerItemHrefs[currentItem] +'" title="'+ tickerItemTitles[currentItem] +'"></a>');
		$(tickerList + ' li a').text(tickerItemsLinkText[currentItem]);
	}
	//add the last holder text if it exists
	if( tickerItemsText2[currentItem].length > 0 ) {
		$(tickerList + ' li').append('<span></span>');
		$(tickerList + ' li span:last-child').text(tickerItemsText2[currentItem]);
	}
	setTimeout(stopTick,60); //time in milliseconds
	
	//stop the tick
	if( currentItem == 0 &&  offset == -1 )
		offset = tickerItemLength.length - 1;
	//currentLength = tickerItemLength[currentItem];
	
	//change the button
	$('#ticker-play-pause').html("<span class='ticker-play'></span>Play");
	
	return false;
}

function stopTick() {
	currentLength = 0;
}

function forwardTicker() {
	//stop the loop
	if(tickerRunning)
		stopTicker(1);
		
	//increment the ticker item if ticker is not running
	if(tickerRunning == false) {
		if(currentItem < ( tickerItemLength.length - 1 ) )
			currentItem ++;
		else
			currentItem = 0;
	}
	//clear the list
	$(tickerList).empty().append('<li></li>');
	//add the first text holder if it excists
	if( tickerItemsText1[currentItem].length > 0 ) {
		$(tickerList + ' li').append('<span></span>');
		$(tickerList + ' li span:first-child').text(tickerItemsText1[currentItem]);
	}
	//add the link holder if it exists
	if( tickerItemsLinkText[currentItem].length > 0 ) {
		$(tickerList + ' li').append('<a href="'+ tickerItemHrefs[currentItem] +'" title="'+ tickerItemTitles[currentItem] +'"></a>');
		$(tickerList + ' li a').text(tickerItemsLinkText[currentItem]);
	}
	//add the last holder text if it exists
	if( tickerItemsText2[currentItem].length > 0 ) {
		$(tickerList + ' li').append('<span></span>');
		$(tickerList + ' li span:last-child').text(tickerItemsText2[currentItem]);
	}
	
	return false;
}

function backTicker() {
	//stop the loop
	if(tickerRunning) 
		stopTicker(-1);
	
	//decrement the ticker item if ticker is not running
	if(tickerRunning == false) {
		if(currentItem == 0 ) {
			currentItem = tickerItemLength.length - 1;
		}
		else {
			currentItem --;
		}
	}
	//clear the list
	$(tickerList).empty().append('<li></li>');
	//add the first text holder if it excists
	if( tickerItemsText1[currentItem].length > 0 ) {
		$(tickerList + ' li').append('<span></span>');
		$(tickerList + ' li span:first-child').text(tickerItemsText1[currentItem]);
	}
	//add the link holder if it exists
	if( tickerItemsLinkText[currentItem].length > 0 ) {
		$(tickerList + ' li').append('<a href="'+ tickerItemHrefs[currentItem] +'" title="'+ tickerItemTitles[currentItem] +'"></a>');
		$(tickerList + ' li a').text(tickerItemsLinkText[currentItem]);
	}
	//add the last holder text if it exists
	if( tickerItemsText2[currentItem].length > 0 ) {
		$(tickerList + ' li').append('<span></span>');
		$(tickerList + ' li span:last-child').text(tickerItemsText2[currentItem]);
	}
	
	return false;
}