var hinterval;
var hclicked;
var hinterval_time = 7000; // 7 seconds
var hoffset = 1;

function updateHeadline() {
  $.getJSON(location.protocol + "//" + location.host + "/headlines/feed/" + hoffset, function(json) {
    $("span.hcontent").fadeOut("slow", function() {
      hoffset = json.offset;
      $("span.hcontent").html(json.title);
      if(hclicked != true) {
        hclicked = true;
        $(this).fadeIn("slow", function() {
          hclicked = false;
        });
      }
    });
  });
}

function nextHeadline() {
  hoffset++;
  updateHeadline();
}

function prevHeadline() {
  hoffset--;
  updateHeadline();
}

function startHeadlineTimer() {
  $(".block-k2headlines").fadeIn("slow");
  hinterval = setInterval(nextHeadline, hinterval_time);
}

function stopHeadlineTimer() {
  clearInterval(hinterval);
}

$(function() {
  // on document ready
  setTimeout("startHeadlineTimer()", 20000); // wait 16 (16000) seconds to start so that flash animation can finish.
   
  // stop timer on mouseover
  $("span.hcontent").hover(function() {
    stopHeadlineTimer();
  }, function() {
    stopHeadlineTimer();
    startHeadlineTimer();
  });
});
