var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline=0;


$(document).ready(function(){

/* Make sure container is proper height */
var mydiv = document.getElementById("newscontent");
var mydivholder = mydiv.getElementsByTagName("div");

var maxHeight = 0;
var i = 0;

while(i < mydivholder.length)
{
if(maxHeight < mydivholder[i].offsetHeight) {
	maxHeight = mydivholder[i].offsetHeight;
}

i++;
}

document.getElementById("newscontent").style.height = maxHeight + "px";


/* Headline rotation */

  headline_count = $("div.newsslide").size();
  $("div.newsslide:eq("+current_headline+")").css('left','16px');
  
  headline_interval = setInterval(headline_rotate,8000); //time in milliseconds
  $('a#rightarrow').click(function() {
    clearInterval(headline_interval);
	headline_interval = setInterval(headline_rotate,8000); //time in milliseconds
    headline_rotate();
    return false;
  });
  $('a#leftarrow').click(function() {
    clearInterval(headline_interval);
	headline_interval = setInterval(headline_rotate,8000); //time in milliseconds
    headline_rotate_back();
    return false;
  });
});

function headline_rotate() {
  current_headline = (old_headline + 1) % headline_count; //remainder will always equal old_headline until it reaches headline_count - at which point it becomes zero. clock arithmetic
  $("div.newsslide:eq(" + old_headline + ")").animate({left: -305},"slow", function() {
    $(this).css('left','9999em');
    });
  $("div.newsslide:eq(" + current_headline + ")").show().animate({left: 16},"slow");  
  old_headline = current_headline;
}

function headline_rotate_back() {

current_headline = (old_headline - 1) % headline_count; //remainder will always equal old_headline until it reaches headline_count - at which point it becomes zero. clock arithmetic

if(current_headline < 0) {current_headline = (headline_count - 1);} /* loop */

  $("div.newsslide:eq(" + old_headline + ")").animate({left: 405},"slow", function() {
    $(this).css('left','9999em');
    });
  $("div.newsslide:eq(" + current_headline + ")").show().animate({left: 16},"slow");  
  old_headline = current_headline;
}
