/*-  Queue up functions to fire onload/onresize
----------------------------------------------------------------------*/
/*
	Rotate features on testimonials index
*/
addLoadEvent(loadFeaturedTestimonials);

/*-  Rotate features on testimonials index
----------------------------------------------------------------------*/
function loadFeaturedTestimonials() {
	var isIE5Mac = (document.all && !window.attachEvent) ? 1 : 0;

	if (document.getElementById && document.getElementsByTagName && document.createElement && document.cloneNode) {
		var testimonials = document.getElementById("testimonials");
		if (testimonials) {
			// Let's loop through each "column"
			var columns = $(".column", testimonials);

			for (var i = 0; i < columns.length; i++) {
				// We'll need to reference the <h3> later, so let's get it now.
				var headline = columns[i].getElementsByTagName("h3")[0];

				// Select a testimonial at random from this column
				var people = $(".person", columns[i]);
				var featuredContent = people[rand(1, people.length) - 1];

				// Get the "id" of the person we'll be showing
				var testImg = featuredContent.getElementsByTagName("img")[0].getAttribute("src");
				var slug = testImg.split("/")[testImg.split("/").length - 1];
				slug = slug.substring(0, slug.length - 4);

				// the path to our images
				var imgPath = "/img/testimonials/";

				// Now, we'll create the new "featured" testimonial
				var div = document.createElement("div");
				div.className = "person testimonial-featured";

				// ...and the photo...
				var imgDiv = document.createElement("div");
				var shDiv = imgDiv.cloneNode(false);
				var ital = document.createElement("i");
				var photo = document.createElement("img");

				imgDiv.className = "image";
				shDiv.className = "sh";
				photo.setAttribute("src", imgPath + slug + "-lg.jpg");
				photo.setAttribute("alt", "");

				shDiv.appendChild(ital);
				imgDiv.appendChild(photo);
				imgDiv.appendChild(shDiv);

				// ...and the quote.
				var dl = document.createElement("dl");
				var dt = document.createElement("dt");
				var dd = document.createElement("dd");
				var quoteImg = document.createElement("img");
				var txt = document.createTextNode(" says:");

				quoteImg.setAttribute("src", imgPath + slug + "-quote.gif");
				quoteImg.setAttribute("alt", "");

				// Grab the link to the family
				var link = featuredContent.getElementsByTagName("dt")[0].getElementsByTagName("a")[0];

				dd.appendChild(quoteImg);
				dt.appendChild(link);
				dt.appendChild(txt);
				dl.appendChild(dt);
				dl.appendChild(dd);

				// Now that everything's assembled, let's insert them into the div, and then into the document.
				div.appendChild(imgDiv);
				div.appendChild(dl);

				insertAfter(featuredContent.parentNode, div, headline)

				// And now, let's remove the original testimonial
				featuredContent.parentNode.removeChild(featuredContent);
			}
		}
	}
}


/*-  Utility Functions
----------------------------------------------------------------------*/
/*
	Insert node after referenceNode in parent
*/
function insertAfter(parent, node, referenceNode) {
	parent.insertBefore(node, referenceNode.nextSibling);
}

/*
	Add Load Event
*/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/*
	Random number in range (min, max)
*/
function rand(min, max) {
	var range = (max - min) + 1;
	var result = Math.floor(Math.random() * range) + min;
	return result;
}