
$(document).ready(function(){
  /* Home page news slider... */
  var rowItems = $('.view-front-page-news .view-content .views-row');
  var rowItemCount = rowItems.size();
  var rowItemLimit = rowItemCount - 3;
  var animateDelay = 1200;
  var stopDelay = 3500;
  /* This function is used by the animate callbacks to make our code only run once. */
  function lastCallbackItem(item){
    return $(item).attr('class').match('views-row-last');
  }
  /* This advances the news slide back to top... */
  function startOver(){
    window.setTimeout(function(){
      rowItems.animate({
        top: 0
      }, animateDelay, function(){
        if(lastCallbackItem(this)){
          hideItem(0);
        }
      });
    }, stopDelay);
  }
  /* Advance news slider one item... */
  function hideItem(index){
    window.setTimeout(function(){
      rowItems.animate({
        top: '-=' + ($(rowItems[index]).height() + 14) + 'px'
      }, animateDelay, function(){
        /* This runs for each match, only advance to next element after the last. */
        if(lastCallbackItem(this)){
          index += 1;
          if(index < rowItemLimit){
            hideItem(index);
          }
          else{
            startOver();
          }
        }
      });
    }, stopDelay);
  }
  /* Start the show! */
  hideItem(0);
});
