(function($) {
	if (!$) {
		var obj;
		if (obj = (window.synLog || window.console)) obj.log("synTestimonials requires jQuery to run");
		return false;
	}

	$(function() {
		var containers = $(".synTestimonials");
		if (containers.length > 0 && !containers.is(".testimonialsSource")) {
			$.get($(".viewMoreTestimonials").attr("href") || "testimonials.html", function(data) {
				var html = String(data),
				    startMarker = "<!--syn-testimonials-start-->",
				    endMarker = "<!--syn-testimonials-end-->",
				    startMarkerIndex = html.indexOf(startMarker),
				    endMarkerIndex = html.indexOf(endMarker),
				    testimonialsHTML = html.substring(startMarkerIndex + startMarker.length, endMarkerIndex),
				    testimonials = [];
				if (startMarkerIndex > -1 && endMarkerIndex > -1) {
					startMarker = "<!--testimonial-start-->";
					endMarker = "<!--testimonial-end-->";
					function digest() {
						startMarkerIndex = testimonialsHTML.indexOf(startMarker);
						endMarkerIndex = testimonialsHTML.indexOf(endMarker);
						if (startMarkerIndex > -1 && endMarkerIndex > -1) {
							testimonials.push({
								html: testimonialsHTML.substring(startMarkerIndex + startMarker.length, endMarkerIndex),
								used: false
							});
							testimonialsHTML = testimonialsHTML.substring(endMarkerIndex + endMarker.length);
							digest();
						}
					}
					digest();
				}
				if (testimonials.length > 0) {
					var numUsed = 0;
					containers.each(function() {
						var container = $(this),
						    content = container.find(".content"),
						    targetElement = (content.length > 0 ? content : container),
						    testimonial = null,
						    numTestimonials = $(this).attr("testimonials") || 1;
						    html = "";

						function addTestimonial() {
							// get an unused testimonial
							do {
								testimonial = testimonials[Math.floor(Math.random() * testimonials.length)];
							} while (testimonial.used);

							// set used marker
							testimonial.used = true;

							// if all the testimonials have been used, reset the used marker on each
							if (++numUsed >= testimonials.length) {
								for (var i = 0, t; t = testimonials[i]; i++) {
									t.used = false;
								}
								numUsed = 0;
							}

							// add testimonial HTML to the HTML string buffer
							html += testimonial.html;
						}

						if (numTestimonials === "height") {
							var contentHeight = 0,
							    parentElement = container.parent(),
							    parentElementHeight = parentElement.innerHeight(),
							    lastTestimonialHeight = 0;

							// show target element and hide content until ready to display
							container.show();

							do {
								// add to page
								addTestimonial();
								targetElement.html(html).find(".testimonial").removeClass("last").filter(":last").addClass("last");

								// calculate remaining height
								contentHeight = 0;
								parentElement.children().each(function() { contentHeight += $(this).height(); });

								// calculate last testimonial's height
								lastTestimonialHeight = targetElement.find(".testimonial:last").height();
							} while (parentElementHeight - lastTestimonialHeight > contentHeight);

							// re-show target element in preparation for the slideDown effect
							container.hide();
						} else {
							for (var j = 0; j < numTestimonials; j++) {
								addTestimonial();
							}
						}
						targetElement.html(html).find(".testimonial").removeClass("last").filter(":last").addClass("last");
						container.slideDown().fadeIn();
					});
				}
			});
		}
	});
})(window.jQuery);

