// JavaScript Document

var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;

var paused = false;
var scrolling = false;

var scrollDelay = 8000;

$(document).ready(function(){
    headline_count = $("div.newsline").size();
	
	// make first item visible
    $("div.newsline:eq("+current_headline+")").css('top', '16px');
	// set up timed calls to headline forward
	headline_interval = setInterval(headline_forward,scrollDelay);
	
	// stop scroller when user has mouse over headline
    $('#scrollup').hover(function() {
        clearInterval(headline_interval);
    }, function() {
	// if user has not paused, or headline is not in the middle of an animation, resume scrolling on mouse out
		if (!paused && !scrolling) {
			clearInterval(headline_interval);
			headline_interval = setInterval(headline_forward,scrollDelay);
		}
    });	
	
	$("a#back").click(function() {	
	// only allow clicks when not animating. clear the timer, move, and then re-instate the timer if not paused
		if (!scrolling) {
			clearInterval(headline_interval);
			headline_backward();
			if (!paused) headline_interval = setInterval(headline_forward,scrollDelay);
		}
		return false;
   });
	
	$("a#next").click(function() {
	// only allow clicks when not animating. clear the timer, move, and then re-instate the timer if not paused
		if (!scrolling) {
			clearInterval(headline_interval);
			headline_forward();
			if (!paused) headline_interval = setInterval(headline_forward,scrollDelay);
		}
		return false;
   });	 
	
	$("a#pause").click(function() {
		if (!scrolling) {
			if (!paused) {
			// if not paused - change button to play, and clear scroll timer set paused flag
				$(this).css('background', 'url(/images/play_btn.gif) top left no-repeat');
				clearInterval(headline_interval);
				paused = true;
				return false;
			}
			else {
			// if paused - change button to pause, and re-instate scroll timer clear paused flag
				$(this).css('background', 'url(/images/pause_btn.gif) top left no-repeat');
				paused = false;
				headline_forward();
				headline_interval = setInterval(headline_forward,scrollDelay);
				return false;
			}
		}
   });
});

function headline_forward() {
	// set scrolling flag to true and give visual clue that buttons are disabled.
	scrolling = true;
	toggleButtons();
	
	// use modulous to cyclilly increment the current headline pointer
    current_headline = (old_headline + 1) % headline_count;
	
	// animate oldheadline out and reset it's beginning animation position, and new in, after new headline comes into view, re-enable buttons
    $("div.newsline:eq(" + old_headline + ")").animate({top: -250},"slow", function() { $(this).css("top","210px"); });
    $("div.newsline:eq(" + current_headline + ")").animate({top: 16},"slow", function() { scrolling = false; toggleButtons();  });
    old_headline = current_headline;
}

function headline_backward() {
	// set scrolling flag to true and give visual clue that buttons are disabled.
	scrolling = true;
	toggleButtons();
	
	// point to previous headline.  if previous headline is the last index of the array, move to that one
    current_headline = (old_headline - 1);
	if (current_headline < 0) current_headline = headline_count + current_headline;
	
	// show a reversed animation to indicate movement backwards, after previous headline comes into view, re-enable buttons
    $("div.newsline:eq(" + old_headline + ")").animate({top: 210},"slow", function() { });
    $("div.newsline:eq(" + current_headline + ")").css("top","-250px").animate({top: 16},"slow", function() { scrolling = false; toggleButtons();  });
    old_headline = current_headline;
}

function toggleButtons() {
	if (scrolling)
		$("ul#playPause	a").css("color","#DDD");
	else
		$("ul#playPause	a").css("color","#000");
}