var currentTestimonial = 0;

function onLoadFunction() {
  prepareTestimonials();
}

function prepareTestimonials() {
  var testimonials = $$('#TestimonialContainer div.Testimonial');
  var testimonialsText = $$('#TestimonialContainer div.TestimonialQuote');
  testimonialsText.each(function(testimonial) {
    testimonialText = testimonial.innerHTML;
    testimonialText = testimonialText.stripTags();
    testimonialText = testimonialText.truncate(200, '...');
    testimonial.innerHTML = '<p>' + testimonialText + '</p>';
  });
  testimonials.invoke('hide');  
  testimonials[0].show();
  $('TestimonialInnerContainer').appear({ duration: 0.8, queue: { position: 'end', scope: 'rotateTestimonials' }});
  setInterval(rotateTestimonials, '9000'); 
}

function rotateTestimonials() {
  var testimonials = $$('#TestimonialContainer div.Testimonial');
  if (currentTestimonial == '') currentTestimonial = 0;
  $(testimonials[currentTestimonial]).fade({ duration: 0.8, queue: { position: 'end', scope: 'rotateTestimonials' }});
  
  newTestimonial = Math.floor(Math.random() * testimonials.length);
  while (newTestimonial == currentTestimonial) {
    newTestimonial =  Math.floor(Math.random() * testimonials.length);
  }
  currentTestimonial = newTestimonial;
  $(testimonials[newTestimonial]).appear({ duration: 0.8, queue: { position: 'end', scope: 'rotateTestimonials' }});
}

