var $j = jQuery.noConflict();
var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;
var play_pause = 'pause';

$j(document).ready(function(){
							
    headline_count = $j("div.headline").size();
    
    // setup the first headline
    $j("div.headline:eq("+current_headline+")").css('top', '5px');
    
    // setup the counter text
    $j("#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();
        }
    );
*/
    $j('#scroll_play_pause').click(function(){
        if( play_pause == 'pause' ) {
            clearInterval(headline_interval);
            $j(this).text('>');
            play_pause = 'play';
        } else {
            headline_interval = setInterval(headline_rotate,5000);
            headline_rotate();
            $j(this).text('| |') ;
            play_pause = 'pause';
        }
    });

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

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

    
    $j('#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;
  

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

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