var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;
var play_pause = 'pause';

$(document).ready(function(){
    headline_count = $("div.headline").size();
    
    // setup the first headline
    $("div.headline:eq("+current_headline+")").css('top', '5px');
    
    // setup the counter text
    $("#scroll_counter").text('1/' + headline_count);
    
    // call the rotate function at regular intervals
    headline_interval = setInterval(headline_rotate,5000);
/*  
    $('#scrollup').hover(
        function() {
        clearInterval(headline_interval);
        }, 
        function() {
            headline_interval = setInterval(headline_rotate,5000);
            headline_rotate();
        }
    );
*/
    $('#scroll_play_pause').click(function(){
        if( play_pause == 'pause' ) {
            clearInterval(headline_interval);
            $(this).text('>');
            play_pause = 'play';
        } else {
            headline_interval = setInterval(headline_rotate,5000);
            headline_rotate();
            $(this).text('| |') ;
            play_pause = 'pause';
        }
    });

    $('#scroll_play').click(function(){
            headline_interval = setInterval(headline_rotate,5000);
            headline_rotate();
            play_pause = 'pause';
    });

   $('#scroll_stop').click(function(){
            clearInterval(headline_interval);
            play_pause = 'play';
    });

    
    $('#scroll_next').click(function(){
            clearInterval(headline_interval);
            headline_interval = setInterval(headline_rotate,5000);
            headline_rotate();
    });
});

function headline_rotate() {
  current_headline = (old_headline + 1) % headline_count;
  

  $("div.headline:eq(" + old_headline + ")")
	.animate({top: -205},"slow", function() {
	  $(this).css('bottom', '210px');
	});

  $("div.headline:eq(" + current_headline + ")")
	.animate({top: 5},"slow");
  
  old_headline = current_headline;  
  
  // incriment the headline text
  $("#scroll_counter").text( (current_headline+1) + '/' + headline_count);
  
}