var testimonialFadeDelay = 8000;
var testimonialRotateTimeout = null;

function rotateTestimonials(nextIndex) {
    clearTimeout(testimonialRotateTimeout);
    var list_items = $$('.sidebar_testimonials .sidebar_testimonial');

        if (list_items.length == 1) {
                list_items[0].setAttribute('style', '');
        } else if (list_items.length < 1) {
                return false;
        } else {
                if (typeof(nextIndex) != 'undefined') {
                nextTestimonial(nextIndex)
            } else {
                nextTestimonial();
            }

            testimonialRotateTimeout = setTimeout(function () { rotateTestimonials(); }, testimonialFadeDelay);
        }
}

function nextTestimonial(nextIndex) {
    var list_items = $$('.sidebar_testimonials .sidebar_testimonial');

    var currentIndex = null;

    // first, determine currentIndex and nextIndex
    for (var i = 0; i < list_items.length; i++) {
        if (list_items[i].className == 'sidebar_testimonial selected') {
            currentIndex = i;
        }
    }
    if (typeof(nextIndex) == 'undefined' || nextIndex == null) {
        nextIndex = currentIndex + 1;
        if (nextIndex > (list_items.length-1)) nextIndex = 0;
    }

    // stop now if there's nothing to do
    if (currentIndex == nextIndex) return false;

    // hide all elements that aren't current, and reset classes
    for (var i = 0; i < list_items.length; i++) {
        if (i != currentIndex) {
            Element.hide(list_items[i]);
        }

        list_items[i].className = 'sidebar_testimonial';
    }

    // do the transition in css first
    list_items[nextIndex].className = 'sidebar_testimonial selected';

    // then with js effects
    Effect.Appear(list_items[nextIndex], { duration: 0.4 })
    if (currentIndex != null) Effect.Fade(list_items[currentIndex], { duration: 0.4 })
}

