gTestimonialSlideTime = 15000; // milliseconds

function show_next_testimonial ()
{
	if (window.gTestimonialTimer) {
		clearTimeout(gTestimonialTimer);
		gTestimonialTimer = null;
	}
	
	if (gShowingTestimonialIndex < 0) {
		gShowingTestimonialIndex = Math.floor(Math.random() * testimonials.length);
	}
	
	if (++gShowingTestimonialIndex >= testimonials.length)
		gShowingTestimonialIndex = 0;
		
	var oldHeight = 0;
	
	// bring all elements to the back
	for (var i = 0; i < testimonials.length; i++) {
		var elm = $('testimonial'+i);
		if (Element.hasClassName(elm, 'current_testimonial')) {
			Element.removeClassName(elm, 'current_testimonial');
			if (Prototype.Browser.WebKit) {
				elm.style.opacity = '0';
			}
			else {
				elm.opacityAnimator._start(1.0, 0.0);
			}
			oldHeight = elm.offsetHeight;
		}
	}
	
	// show the new element
	if (oldHeight > gTestimonialHeights[gShowingTestimonialIndex]) {
		Element.addClassName('testimonial'+gShowingTestimonialIndex, 'current_testimonial');
		setTimeout(function() {
			if (Prototype.Browser.WebKit) {
				$('testimonials_container').style.height = (gTestimonialHeights[gShowingTestimonialIndex])+'px';
				$('testimonial'+gShowingTestimonialIndex).style.opacity = '1.0';
			}
			else {
				gTestimonialsAnimator._start($('testimonials_container').offsetHeight-1, gTestimonialHeights[gShowingTestimonialIndex]);
				$('testimonial'+gShowingTestimonialIndex).opacityAnimator._start(0.0, 1.0);
			}
			gTestimonialTimer = setTimeout(show_next_testimonial, gTestimonialSlideTime);
		}, 1000);
	}
	else {
		if (Prototype.Browser.WebKit) {
			$('testimonials_container').style.height = (gTestimonialHeights[gShowingTestimonialIndex])+'px';
		}
		else {
			gTestimonialsAnimator._start($('testimonials_container').offsetHeight-1, gTestimonialHeights[gShowingTestimonialIndex]);
		}
		setTimeout(function() {
			Element.addClassName('testimonial'+gShowingTestimonialIndex, 'current_testimonial');
			if (Prototype.Browser.WebKit) {
				$('testimonial'+gShowingTestimonialIndex).style.opacity = '1.0';
			}
			else {
				$('testimonial'+gShowingTestimonialIndex).opacityAnimator._start(0.0, 1.0);
			}
			gTestimonialTimer = setTimeout(show_next_testimonial, gTestimonialSlideTime);
		}, 1000);
	}
}

function create_testimonail_elements ()
{
	gTestimonialHeights = [];
	
	if (!Prototype.Browser.WebKit) {
		gTestimonialsAnimator = new Fx.Style($('testimonials_container'), 'height', {duration:1000, link:'cancel'});
	}
	
	for (var i = 0 ; i < testimonials.length; i++) {
		// create the element
		var a = document.createElement('a');
		a.href = (document.body.className == 'index' ? 'testimonials/' : '../testimonials/');
		a.id = 'testimonial'+i;
		var span = document.createElement('span');
		a.appendChild(span);
		span.appendChild(document.createTextNode('"'+testimonials[i].quote + '" - ' + testimonials[i].author));
		
		// should be invisible at first
		if (window.ActiveXObject)
			a.style.filter = "alpha(opacity=0)";
		else
			a.style.opacity = '0';
			
		// add the element to the DOM
		$('testimonials_container').appendChild(a);
		
		// remember the element's height
		gTestimonialHeights[i] = a.offsetHeight;
		
		// set up animators (except on WebKit)
		if (!Prototype.Browser.WebKit) {
			a.opacityAnimator = new Fx.Style(a, 'opacity', {duration:1000, link:'cancel'});
			a.heightAnimator = new Fx.Style(a, 'height', {duration:1000, link:'cancel'});
		}
	}
	
	$('testimonials_container').style.height = '0';
}

function toggle_contact_info ()
{
	if (document.body.className.match(/showcontactinfo/)) {
		document.body.className = document.body.className.replace(/\s*showcontactinfo/, '');
	}
	else {
		document.body.className += ' showcontactinfo';
	}
	return false;
}

function escape_html (inHTML)
{
	return inHTML.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
}

window.onload = function setup_page ()
{
	gShowingTestimonialIndex = (-1); // as long as this number is higher than the testimonials, we're fine
	$('testimonials_container').innerHTML = '';
	create_testimonail_elements();
	show_next_testimonial();
	$('contact_more').onclick = toggle_contact_info;
}
