/*!
 * PagerDuty JavaScript Library
 * http://www.pagerduty.com/
 *
 */

/**
 * Testimonial slide show
 * Takes into account the number of words and adjusts the timeout accordingly.
 */
function setupTestimonialSlideShow(delayPerWord /* in milliseconds*/) {
  var currentVisibleTestimonial = 0;
  
  function getDelayinMS(text){
    // number of words in the text
    var length = text.split(" ").length;
    return length * delayPerWord;
  };
  
  function slideLoop(){
    var testimonials = $("div.testimonial-container div.testimonial");
    $(testimonials[currentVisibleTestimonial]).fadeOut("normal");

    if(currentVisibleTestimonial == (testimonials.length - 1)) {
      currentVisibleTestimonial = 0;
    } else {
      currentVisibleTestimonial++;
    }
    var newTestimonial = $(testimonials[currentVisibleTestimonial]);
    newTestimonial.fadeIn("normal");
    var newText = newTestimonial.find("blockquote p").text();
    window.setTimeout(slideLoop, getDelayinMS(newText));
  };
  
  // start the initial timer
  window.setTimeout(slideLoop, getDelayinMS($("div.testimonial-container div.testimonial:visible blockquote p").text()));
  
}
