document.srcFile = '/javascript/testimonial_data.html';
document.nextItem = 0;

function showTestimonials(order)
{
	if (document.pause) { return; }
	if (!document.testimonials)
	{
		document.testimonials = getTestimonials();
	}
	var html = document.testimonials;
	var numItems = $(html).find('li').length;
	var n = getNextIndex(order, numItems);
	var i = 0;
	$(html).find('li').each(function()
	{
		if (n == i)
		{
			$('div#testimonial').html(getPauseLink() + $(this).html());
		}
		i++;
	});
}

function getNextIndex(order, numItems)
{
	if ('ascending' == order)
	{
		if (document.nextItem == numItems) { document.nextItem = 0; }
		return document.nextItem++;
	}
	if ('descending' == order)
	{
		if (document.nextItem == 0) { document.nextItem = numItems; }
		return --document.nextItem;
	}
	if ('semi-random' == order) { return getRandomNumber(numItems, true); }
	if ('random' == order) { return getRandomNumber(numItems, false); }
}

function getTestimonials()
{
	$.ajax({
		type: 'GET',
		url: document.srcFile,
		dataType: 'html',
		cache: true,
		async: false,
		success: function(html) {
			res = html;
		},
		error: function() {}
	});
	return res;
}

function getRandomNumber(max, dejaVu)
{
	if (dejaVu)
	{
		if (!document.dejaVu || document.dejaVu.length == max)
		{
			document.dejaVu = new Array();
		}
		for (var i = 0; i < max; i++)
		{
			var n = Math.floor(Math.random()*max);
			if (!inArray(n, document.dejaVu))
			{
				document.dejaVu[document.dejaVu.length] = n;
				break;
			}
		}
		return n;
	}
	else
	{
		return n = Math.floor(Math.random()*max);
	}
}

function inArray(item, myArray)
{
	for (var i = 0; i < myArray.length; i++)
	{
		if (myArray[i] == item) { return true; }
	}
	return false;
}

function setPause(pause)
{
	document.pause = pause;
	var mode = (!document.pause) ? 'pause' : 'play';
	var linkText = "<img src='/images/control_" + mode;
	linkText += ".png' width='16' height='16' border='0'>"
	$('a#pauseLink').html(linkText);
	$('a#pauseLink').attr("title", mode.replace(/p/, "P"));
	var href = "javascript: setPause(" + !document.pause + ");";
	$('a#pauseLink').attr('href', href);
}

function getPauseLink()
{
	if (!document.pause) { document.pause = false; }
	var strLink = "<a id='pauseLink' href='javascript: setPause(";
	strLink += !document.pause + ");' title='Pause'>";
	strLink += "<img src='/images/control_pause.png' width='16'";
	strLink += "height='16' border='0'></a>";
	return strLink;
}


