var testimonial_timer;
var testimonial_hover_start_time;
var MIN_TESTIMONIAL_TIME = 6000;
var MAX_TESTIMONIAL_TIME = 10000;
// These next two aren't to limit the testimonial lengths, but rather to
// define what char length the above two limits correspond to.
var MIN_TESTIMONIAL_LENGTH = 250;
var MAX_TESTIMONIAL_LENGTH = 500;

function switch_testimonial(new_id) {
	var newly_active_icon = $('#testimonial-link-' + new_id);
	var newly_active_testimonial = $('#testimonial-' + new_id);
	if (!newly_active_testimonial.is(':visible')) {
		var visible = $('#quotebox .testimonial:visible');
		visible.fadeOut('fast', function() {
			$('.testimonial-link').removeClass('active');
			newly_active_icon.addClass('active');
			newly_active_testimonial.fadeIn('fast', function() {
				set_testimonial_timer();
			});
		});
	}
	
}

function next_testimonial() {
	var current_testimonial = $('.testimonial:visible:first');
	var new_testimonial = current_testimonial.next('.testimonial');
	if (new_testimonial.length == 0) {
		new_testimonial = current_testimonial
				.siblings('.testimonial:first-child');
	}
	var new_id = new_testimonial.attr('id').match(/\d+/)[0];
	switch_testimonial(new_id);
}

function previous_testimonial() {
	var current_testimonial = $('.testimonial:visible:first');
	var new_testimonial = current_testimonial.prev('.testimonial');
	if (new_testimonial.length == 0) {
		new_testimonial = current_testimonial
				.siblings('.testimonial:last-child');
	}
	var new_id = new_testimonial.attr('id').match(/\d+/)[0];
	switch_testimonial(new_id);
}

function set_testimonial_timer() {
	var current_testimonial = $('.testimonial:visible:first .quotebody');
	var text_length = current_testimonial.text().length;
	// Scale the delay from 6s for testimonials of 250 characters or less to
	// 10s for those of 500 characters or more. (Though these values can be
	// changed by setting the appropriate constants at the top of the file.)
	var delay = (((text_length - MIN_TESTIMONIAL_LENGTH)
			          * (MAX_TESTIMONIAL_TIME - MIN_TESTIMONIAL_TIME)
			          / (MAX_TESTIMONIAL_LENGTH - MIN_TESTIMONIAL_LENGTH))
			      + MIN_TESTIMONIAL_TIME);
	delay = (delay > MAX_TESTIMONIAL_TIME) ? MAX_TESTIMONIAL_TIME : delay;
	delay = (delay < MIN_TESTIMONIAL_TIME) ? MIN_TESTIMONIAL_TIME : delay;
	clearTimeout(testimonial_timer);
	testimonial_timer = setTimeout('next_testimonial()', delay);
}

/

/*
$(function() {
	function slide_contact_info(offset) {
		var email_left = $('#contact_email').position().left;
		var call_left = $('#contact_call').position().left;
		var phone_number_left = $('#contact_number').position().left;
		$('#contact_email').animate( {
			'left' : email_left + offset
		});
		$('#contact_call').animate( {
			'left' : call_left + offset
		});
		$('#contact_number').animate( {
			'left' : phone_number_left + offset
		});
	}

	$('#contact_call').click(function(event) {
		slide_contact_info(-300);
		event.preventDefault();
	});

	$('#contact_number').click(function() {
		slide_contact_info(300);
	});

	$('#contact_box').mouseleave(function() {
		var email_left = $('#contact_email').position().left;
		if (email_left < 0) {
			slide_contact_info(300);
		}
	}); */
	

	var max_testimonial_height = 0;
	$('.testimonial_container .testimonial').each(function(i) {
		var height = $(this).height();
		if (height > max_testimonial_height) {
			max_testimonial_height = height;
		}
	});

	$('.testimonial_container').height(max_testimonial_height + 15);

	$('span.testimonial-link').click(function() {
		var id = $(this).attr('id').match(/\d+/)[0];
		switch_testimonial(id);
	});

	set_testimonial_timer();

	$('#quotebox')
			.hover(function() {
				clearTimeout(testimonial_timer);
				testimonial_hover_start_time = new Date();
				var quote_height = $('#quotebox').height();
				var icon_height = $('.quote_back_button').first().height();
				var new_top = (quote_height / 2) - (icon_height / 2) + 'px';
				$('.quote_back_button, .quote_forward_button').css( {
					top : new_top
				}).fadeIn('fast');
			}, function() {
				$('.quote_back_button, .quote_forward_button').fadeOut('fast');
				var now = new Date();
				// If someone hovers longer than a second, advance to next
					// testimonial when they leave the testimonial box.
					if ((now.getTime() - testimonial_hover_start_time.getTime()) > 1000) {
						next_testimonial();
					} else {
						set_testimonial_timer();
					}
				});

	$('.quote_back_button').click(function() {
		previous_testimonial();
	});

	$('.quote_forward_button').click(function() {
		next_testimonial();
	});

	$('.testimonial_container').click(function() {
		next_testimonial();
	});
});

