 var headline_count;
 var headline_interval;
 var old_headline = 0;
 var current_headline = 0;
 var goto_headline = -1;
 
 $(document).ready(function(){
 
   // hide the play btn, so that only pause is visible
   $("#rotatePlayBtn").addClass("inactiveRotaterControl");
 
   // start the first (idx = 0) rotated item off as visible
   headline_count = $("div.rotatedItem").size();
   $("div.rotatedItem:eq("+current_headline+")").css('left','5px');
 
   // create the links for accessing specific rotated items
   var i=1;
   for (i=1; i <= headline_count; i++)
   {
     $('#rotaterControls').append('<a class="rotaterLink" id="rotaterLink' + i + '">' + i + '</a> ');
   }
   var initialLinkNumber = current_headline + 1;
   $("#rotaterLink" + initialLinkNumber).addClass("currentRotatedItem");  // activate first link

   // create and initialize "Read More" link
   $('#rotaterControls').prepend('<a id="rotaterReadMoreLink" href="">Read More</a>');
   var rotatedItemFullTextUrl = $("div.rotatedItem:eq(" + current_headline + ") > input").attr("value");
   $("#rotaterReadMoreLink").attr("href", rotatedItemFullTextUrl);
 
   // set the time interval to rotate
   headline_interval = setInterval(headline_rotate,7500); //time in milliseconds
 
   // add click event handler for the rotated item links
   $("#rotaterControls > a.rotaterLink").click(function() {
     clearInterval(headline_interval);
     if ($("#rotatePlayBtn").hasClass("inactiveRotaterControl"))
     {
       $("#rotaterControls > img").toggleClass("inactiveRotaterControl");
     }
     var currentId = $(this).attr("id");
     goto_headline = currentId.charAt(currentId.length - 1) - 1;
     headline_rotate();
   });
 
   // add click event handler for the controller buttons
   $("#rotaterControls > img").click(function() {
     
     // make the opposing button visible, hiding the current one in the process
     $("#rotaterControls > img").toggleClass("inactiveRotaterControl");
  
     if ($(this).attr("id") == "rotatePlayBtn")
     {
       headline_interval = setInterval(headline_rotate,7500); //time in milliseconds
       headline_rotate();
     }
     else
     {
       clearInterval(headline_interval);
     }
   });
 
   /*
   $('#rotatedContentContainer').hover(function() {
     clearInterval(headline_interval);
   }, function() {
     headline_interval = setInterval(headline_rotate,7500); //time in milliseconds
     headline_rotate();
   });
   */
 });
 
 function headline_rotate() {
   if (goto_headline != -1) {
     current_headline = goto_headline;
     goto_headline = -1;
   }
   else
     current_headline = (old_headline + 1) % headline_count;
 
   // highlight current link
   var oldLinkNumber = old_headline + 1;
   var newLinkNumber = current_headline + 1;
   $("#rotaterLink" + oldLinkNumber).toggleClass("currentRotatedItem");
   $("#rotaterLink" + newLinkNumber).toggleClass("currentRotatedItem");

   // link to "Read More" to the appropriate page
   var rotatedItemFullTextUrl = $("div.rotatedItem:eq(" + current_headline + ") > input").attr("value");
   $("#rotaterReadMoreLink").attr("href", rotatedItemFullTextUrl);
 
   /*
   // slide old headline off to the left, then set it out of view on the right
   $("div.rotatedItem:eq(" + old_headline + ")").animate({left: -355},"slow", function() {
     $(this).css('left','360px');
   });
   */
   // fade out old headline, then set it out of view on the right
   $("div.rotatedItem:eq(" + old_headline + ")").fadeTo("slow", 0.1, function() {
     $(this).css('left','360px');
   });

   // show new headline within viewable area
   //$("div.rotatedItem:eq(" + current_headline + ")").show().animate({left: 5},"slow");
   $("div.rotatedItem:eq(" + current_headline + ")").show().css('left', '5px').css("opacity", 0.1).fadeTo("slow", 1.0);

   old_headline = current_headline;
 }

