/* Script for rotating current headlines */

//Arrays containing the headlines and links to stories
var newsHeadline = new Array();
var newsLink = new Array();

//ID of previous news headline
var prevID = -1;
//How many news stories will there be in total
var numHeadlines = 0;
//How long do we wait between headlines (in ms)
var waitTime = 3000;
//Events containing the cycling
var startEvent;
var cycleEvent;
var autocEvent;
//Status of cycle
var state=1;

function cycleNews() {
	var currentID = 0;
	if (prevID < numHeadlines-1) {
		currentID = prevID + 1;
	} else {
		currentID = 0;
	}
	prevID = currentID;
	switchHeadline ("headline_area", currentID);
}

function switchHeadline(aID, hID) {
	var headlineOutput = '';
	headlineOutput = '<a class="headlines_link" href="' + newsLink[hID] + '">' + newsHeadline[hID] + '</a>';
	document.getElementById(aID).innerHTML = headlineOutput;
}

function stopCycle() {
  window.clearTimeout(startEvent);
  window.clearTimeout(cycleEvent);
  window.clearTimeout(autocEvent);
}

function startstop() {
  if (state == 0)  {
    state = 1;
    startEvent = window.setTimeout("cycleNews();autoCycle();", 500);
  } else {
    state = 0;
    stopCycle();
  }
} // end function

function setNumHeadlines(num) { //Mutator function for numHeadlines	
	numHeadlines = num;
}

function autoCycle() { //Cycles through the headlines every waitTime ms
	if (state == 1)  {
		cycleEvent = window.setTimeout("cycleNews();", waitTime);
		autocEvent = window.setTimeout("autoCycle();", waitTime);
	} else {
		stopCycle();
	}
}

