function toggleContent(e) {
	var index = e.data.index;
	if (index != lastTabIndex) {
		$(".toggleLink").parent().removeClass("selected");
		$(".toggleLink").eq(index).parent().addClass("selected");
		if( $(".toggle").eq(lastTabIndex).children('#slideImages')) {
			$('#slideImages').hide();
		}
		$(".toggle").eq(lastTabIndex).hide(function(){
			//$(".toggle").eq(index).slideDown(500,function(){
				$(".toggle").eq(index).show();
			//});
		});
		if( $(".toggle").eq(index).children('#slideImages')) {
			$('#slideImages').show();
		}
		lastTabIndex = index;
	}
	return false;
}

$(function(){

	// Remove after testing
	if (typeof quotetest == 'undefined') return;
	
	// Basic settings and variables
	var fadeSpeed = 750;
	var slideDuration = typeof delay == 'undefined' ? 10 : delay;
	slideDuration = slideDuration * 1000;
	var index = 1;
	var photoContainer = $('#rightSidebar div.photo').eq(1);
	var isFirstLoopComplete = false;
	var testimonialIndex = 0;
	var imageIndex = 0;
	
	// Get the current page's filename
	var url = document.location.toString();
	var filename = url.substring(url.lastIndexOf('/') + 1, url.lastIndexOf('.htm'));
	
	// Load testimonials data
	$.get('images/' + filename + '/testimonials.xml?r=' + Math.random(), function(data){
		// Get array of testimonials
		var testimonials = [];
		$(data).find('testimonial').map(function(){
			testimonials.push($(this));
		});
		// Create array of images
		var images = [];
		for (var i=1;i<=imgcount;i++) {
			images.push('images/' + filename + '/' + i + '.jpg');
		}
		// Shuffle testimonials and photos
		testimonials = shuffle(testimonials);
		images = shuffle(images);
		// Create an array of items containing both testimonials and images
		var items = [];
		for (var i=0;i<(((testimonials.length + images.length) * 2) - 1);i++) {
			// Reset indexes if we've run out of either testimonials or images before going through the total number of items
			if (typeof testimonials[testimonialIndex] == 'undefined') testimonialIndex = 0;
			if (typeof images[imageIndex] == 'undefined') imageIndex = 0;
			// Alternate between adding testimonials and images to the items array
			if (i % 2 == 0) {
				items.push(testimonials[testimonialIndex]);
			} else {
				items.push(images[imageIndex]);
			}
			// Increment indexes for next loop
			testimonialIndex++;
			imageIndex++;
		}
		// Merge testimonials and images into a single randomized array
		//var items = shuffle(testimonials.concat(images));
		// Setup slideshow
		var timer = setInterval(function(){
			// Append next item to container
			if (index <= items.length && !isFirstLoopComplete) {
				var item = items[index - 1];
				if (typeof item == 'object') {
					// Append quote
					photoContainer.append('<p class="quote">' + item.find('quote').text() + '<span class="author">' + item.find('author').text() + '<br />' + item.find('location').text() + '</span></p>');
				} else {
					// Append image
					photoContainer.append('<p class="image"><img src="' + item + '" alt="" width="215" height="215" /></p>');
				}
			}
			if (index > items.length) {
				// Start loop over
				index = 0;
				previousIndex = items.length;
				isFirstLoopComplete = true;
			} else {
				previousIndex = index - 1;
			}
			// Fade out previous item
			photoContainer.find('p').eq(previousIndex).fadeOut(fadeSpeed);
			// Fade in next item and animate container height to next item's height
			var height = photoContainer.find('p').eq(index).height();
			if (photoContainer.find('p').eq(index).hasClass('quote')) height += 6;
			photoContainer.animate({height: (height) + "px"});
			photoContainer.find('p').eq(index).fadeIn(fadeSpeed);
			index++;
		}, slideDuration);		
	}, 'xml');
	
	return;
	
});
											
var shuffle = function(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};