// $('img.photo',this).imagesLoaded(myFunction)
// execute a callback when all images have loaded.
// needed because .load() doesn't work on cached images

// mit license. paul irish. 2010.
// webkit fix from Oren Solomianik. thx!

// callback function is passed the last image to load
// as an argument, and the collection as `this`


$.fn.imagesLoaded = function(callback){
	var elems = this.filter('img'),
		len = elems.length;

	elems.bind('load',function(){
		if (--len <= 0){ callback.call(elems,this); }
	}).each(function(){
		// cached images don't fire load sometimes, so we reset src.
		if (this.complete || this.complete === undefined){
			var src = this.src;
			// webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
			// data uri bypasses webkit log warning (thx doug jones)
			this.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
			this.src = src;
		}
	});

	return this;
};


var refreshtime = 10000;
var shuffle = "false";
var reviews = Array();
var currentReview = -1;

function loadReviews(xmlPath)
{
	$.ajax({
		type: "GET",
		url: xmlPath + "reviews.xml",
		dataType: "xml",
		success: function(xml)
		{
			$(xml).find('settings').each(function(){
				refreshtime = $(this).find('refreshtime').text()?$(this).find('refreshtime').text():refreshtime;
				shuffle = $(this).find('shuffle').text();
			});

			$('#customer-reviews .body').after('<div id="contentTmp" class="hide"></div>');
			$(xml).find('data').each(function(){
				$(xml).find('text').each(function(){
					var text = $(this).find('value').text();
					$('#contentTmp').append(text);
				});
			});
			
			$('#contentTmp img').imagesLoaded(function()
			{
				delayDisplay(xml);
			});
		}
	});
}

function delayDisplay(xml)
{
	$('#contentTmp').remove();
	
	var maxHeight = 0;
	var isIE = ($.browser.msie) ? true : false;
	if(isIE) $('#customer-reviews .body').css('display', 'inline-block');
	
	$(xml).find('data').each(function(){
		$(xml).find('text').each(function(){
			var text = $(this).find('value').text();
			reviews.push(text);

			var curHeight = $('#customer-reviews .body').html(text).attr('clientHeight');
			maxHeight = (curHeight > maxHeight) ? curHeight : maxHeight;
		});
	});

	if(isIE) $('#customer-reviews .body').css('display', 'block');

	$('#customer-reviews .body').html('').animate({
		'min-height': (maxHeight+2)
	}, 1000, function()
	{
		$(this).hide();
		display();
		$(this).fadeIn();
	});
}

function display()
{
	if(reviews.length > 1)
	{
		if(shuffle == "true")
		{
			var randomReview = Math.floor(Math.random() * reviews.length);
			if(currentReview == -1)
			{
				currentReview = randomReview;
				$("#customer-reviews .body").html(reviews[currentReview]);
			}
			else
			{
				var counter = 0;
				while(currentReview == randomReview)
				{
					randomReview = Math.floor(Math.random() * reviews.length);
					if(counter > 1000)
					{
						break;
					}
					counter++;
				}
				currentReview = randomReview;
				$("#customer-reviews .body").fadeOut(400, function () {
					$("#customer-reviews .body").html(reviews[currentReview]).fadeIn(400);
				});
			}
		}
		else
		{
			
			if(currentReview == -1)
			{
				currentReview = 0;
				$("#customer-reviews .body").html(reviews[currentReview]);
			}
			else
			{
				currentReview++;
				if(currentReview >= reviews.length)
				{
					currentReview = 0;
				}
				$("#customer-reviews .body").fadeOut(400, function () {
					$("#customer-reviews .body").html(reviews[currentReview]).fadeIn(400);
				});
			}
		} 
		setTimeout("display();", refreshtime);
	}
	else if(reviews.length == 1)
	{
		$("#customer-reviews .body").html(reviews[0]);
	}
	
}
