// ROTATE SPLASH
// Note: must use GIF for headlines instead of PNG. pngfix.js isn't applied to a graphic that was hidden but then shown

// Global variables
var numSplashes = 3; // number of splashes in rotation
var currentSplash = 1; // current splash displayed
var timer; // timer

jQuery(function($) {
  numSplashes = $("#splashes div[id^=splash-]").length;
  // Begin splash rotation
  rotateSplash(1);
});

function rotateSplash(showSplash) {
  // Place currentSplash into a local variable or else it will be undefined
  var curSplash = currentSplash;
  clearTimeout(timer);
    
  // If prev or next button was clicked...
  if (showSplash == 'prev') { 
    showSplash = curSplash - 1 
    if (showSplash < 1) { showSplash = numSplashes; };
  }

  if (showSplash == 'next') { 
    showSplash = curSplash + 1 
    if (showSplash > numSplashes) { showSplash = 1; };
  }
            
  // If curSplash == showSplash, means the page just loaded or next/prev was clicked at the end of the list. In these cases, don't fade.
  if (curSplash != showSplash) { 
    $('#splash-' + showSplash).fadeIn(1000);
    $('#splash-' + curSplash).fadeOut(1000);
  }

  // Change highlighted numberbox
  document.getElementById('rotate-' + curSplash).style.backgroundPosition = 'left top';
  document.getElementById('rotate-' + showSplash).style.backgroundPosition = 'left bottom';
      
  // showSplash becomes the new currentSplash
  // Update global var currentSplash with same value
  var curSplash = showSplash;
  currentSplash = showSplash;
      
  // Find next splash
  var nextSplash = curSplash + 1;
  if (nextSplash > numSplashes) { nextSplash = 1; };
              
  // Wait, then show the next splash

  timer = setTimeout('rotateSplash(' + nextSplash + ');' , 
  estimateReadingTime($('#splash-' + curSplash + ' .teaser').text()));

}
      
function estimateReadingTime(text) {
  var min = 6000;
  var max = 16000;
  
  // assume 3 wps, or 180 wpm
  var words_per_second = 3; 

  // estimate number of words
  var words = text.length / 5;
  
  // calculate seconds with boundary
  var seconds = Math.round((words / words_per_second) * 1000);
      seconds = Math.max(min, seconds);
      seconds = Math.min(seconds, max);
      
  return seconds;
}
