var ts_timer = 0;
var ts_timerlength = 8000;

function getTotalSpotlightStories() {
	var totalStories = 0;
	var divs = document.getElementById("topicspotlight").getElementsByTagName("div");
	for (i = 0; i < divs.length; i++) {
		if (divs[i].id.indexOf("ts-story") > -1)
			totalStories++;
	}
	return totalStories;
}

function getCurrentSpotlightStory() {
	var currentStory = "";
	var divs = document.getElementById("topicspotlight").getElementsByTagName("div");
	for (i = 0; i < divs.length; i++) {
		if (divs[i].className.indexOf("ts-story-current") > -1)
			currentStory = divs[i];
	}
	return currentStory;
}

function pauseSpotlight() {
	clearTimeout(ts_timer);
}

function updateSpotlight(direction) {
	toggle('ts-graphic-loading');
	clearTimeout(ts_timer);
	
	var currentStory = getCurrentSpotlightStory();
	var currentStoryNum = parseInt(currentStory.id.substring(currentStory.id.lastIndexOf("-")+1, currentStory.id.length));
	var nextStoryNum = 0;
	var totalStories = parseInt(getTotalSpotlightStories());
	if (currentStory) {
		currentStory.className = "ts-story";
		if (direction === "forward") {
			nextStoryNum = currentStoryNum + 1;
		} else if (direction === "back") {
			nextStoryNum = currentStoryNum - 1;
		}
		if (nextStoryNum > totalStories) {
			nextStoryNum = 1;
		} else if (nextStoryNum < 1) {
			nextStoryNum = totalStories;
		}
		var nextStory = document.getElementById("ts-story-" + nextStoryNum);
		if (nextStory) {
			document.getElementById("ts-num-current").innerHTML = nextStoryNum;
			nextStory.className = "ts-story ts-story-current";
		}
	}

	ts_timer = setTimeout("updateSpotlight('forward')", ts_timerlength);
	toggle('ts-graphic-loading');
}

function initSpotlight() {
	document.getElementById("ts-num-total").innerHTML = getTotalSpotlightStories();
	ts_timer = setTimeout("updateSpotlight('forward')", ts_timerlength);
}

addLoadEvent(initSpotlight);