var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;
var ticker_hovered = false;
var rotateOnHoverOff = false;
var initialDelay = 10000;
var intervalPeriod = 5000;
 
$(document).ready(function(){
  headline_count = $("div.ticker-headline").size();
  $("div.ticker-headline:eq("+current_headline+")").css('top','0px');
  
  headline_interval = setTimeout(first_rotate, initialDelay);
  $('.ticker-headline-wrapper').hover(function() {
      ticker_hovered = true;
    }, function() {
      ticker_hovered = false;
      if (rotateOnHoverOff) {
        rotateOnHoverOff = false;
        headline_rotate();
      }
    });
});
 
function headline_rotate() {
  if (!ticker_hovered) {
    current_headline = (old_headline + 1) % headline_count; 
    $("div.ticker-headline:eq(" + old_headline + ")").fadeOut(300, function() {
      $(this).css('top', '30px');
      // $("div.ticker-headline:eq(" + old_headline + ")").css('top', '30px');
      $("div.ticker-headline:eq(" + current_headline + ")").show().animate({top: 0},"slow");
      //$("div.ticker-headline:eq(" + current_headline + ")").css('top', '0px');
    });
    old_headline = current_headline;
  } else {
    rotateOnHoverOff = true;
  }
}

function first_rotate() {
  headline_rotate();
  headline_interval = setInterval(headline_rotate, intervalPeriod);
}
