var testimonialCount = testimonials.length / 2;
var testimonialUnitTime = 60; // msec
var testimonialFadeUnits = 10; // multiply this time testimonialUnitTime for duration of fade-in in msecs
var testimonialDisplayUnits = 120; // multiply this time testimonialUnitTime for duration of full-display period in msecs
var testimonialTotalOfFadePlusDisplayUnits = testimonialFadeUnits + testimonialDisplayUnits;
var posWithinFadeAndDisplayCountUp = 0;
var indexCurrentTestimonial = 0;
var bNeedToSetTestimonialText = true;
var testimonialOpacity = 100;

var gbTimedFade = false;

function StartTestimonials(bTimedFade)
{
	gbTimedFade = bTimedFade;
	indexCurrentTestimonial = 0;
	bNeedToSetTestimonialText = true;
	if (gbTimedFade)
	{
		posWithinFadeAndDisplayCountUp = 0;
		setInterval("IncrementTestimonialTimer()", testimonialUnitTime); 
	}
	else
	{
		ShowCurrentTestimonial();
	}
}

function ShowCurrentTestimonial()
{
    if (indexCurrentTestimonial >= testimonialCount)
    {
        indexCurrentTestimonial = 0;
    }	
	if (bNeedToSetTestimonialText)
	{
        MM_setTextOfLayer('Testimonial','', '&#8220;' + testimonials[2 * indexCurrentTestimonial + 1] + '&#8221;');
        MM_setTextOfLayer('TestimonialAuthor','', '- ' + testimonials[2 * indexCurrentTestimonial]);
		if (gbTimedFade)
		{
			posWithinFadeAndDisplayCountUp = 0;
		}
	}
	if (!gbTimedFade)
    {
        return;
	}
	if (posWithinFadeAndDisplayCountUp < testimonialFadeUnits)
    {
        testimonialOpacity = (posWithinFadeAndDisplayCountUp * 100) / testimonialFadeUnits; 
		bNeedToSetTestimonialText = false;
    }
    else if (posWithinFadeAndDisplayCountUp < testimonialTotalOfFadePlusDisplayUnits)
    {
		bNeedToSetTestimonialText = false;
		if (testimonialOpacity == 100)
		{
			return; // no need to redisplay the text
		}
        testimonialOpacity = 100;
    }
    else
    {
		// Start the next testimonial, and start the fade.
        testimonialOpacity = 0;
		bNeedToSetTestimonialText = true;
		indexCurrentTestimonial++;  
		posWithinFadeAndDisplayCountUp = 0;
    }
    SetLayerOpacity('Testimonial', testimonialOpacity);
    SetLayerOpacity('TestimonialAuthor', testimonialOpacity);
}

function IncrementTestimonialTimer()
{
    posWithinFadeAndDisplayCountUp++;
    ShowCurrentTestimonial();
}

function OnNextTestimonial() // designed to work both with and without timed testimonials
{
	indexCurrentTestimonial++; 
	bNeedToSetTestimonialText = true;
	ShowCurrentTestimonial();
}

function OnPrevTestimonial()
{
	indexCurrentTestimonial--;
	if (indexCurrentTestimonial < 0)
	{
		indexCurrentTestimonial = testimonialCount - 1;
	}
	bNeedToSetTestimonialText = true;
	ShowCurrentTestimonial();
}

