/**
 * Resizes floated image containers to the size of the image
 */
$.fn.imageWidth = function(threshold) {
	return this.each(function() {
		// Threshold is the maximum width an image plus its border and padding can be in relation to its parent container
		var threshold = (threshold) ? threshold : 2/3;
		
		// Determine the width of the image along with borders and padding
		var $image = $('img', $(this));
		var imageWidth = $image.width();
		var paddingLeft = parseInt($image.css('padding-left').replace('px', ''), 10);
		var paddingRight = parseInt($image.css('padding-right').replace('px', ''), 10);
		var borderLeft = parseInt($image.css('border-left-width').replace('px', ''), 10);
		var borderRight = parseInt($image.css('border-right-width').replace('px', ''), 10);
		
		// Calculate total edge (padding and border) width and the total width (edge and image)
		var edgeWidth = paddingLeft + paddingRight + borderLeft + borderRight;
		var totalWidth = imageWidth + edgeWidth;
		
		// Find parent and determine width
		var $parent = $(this).parent();
		var parentWidth = $parent.width();
		
		// If the image is greater then the threshold times the parent width resize the image and the container width
		// Otherwise set the image left's div to the size of the image plus the edge
		if ((threshold * parentWidth) <= totalWidth) {
			var revisedWidth = parentWidth * threshold;
			var revisedImageWidth = revisedWidth - edgeWidth;
			
			$image.width(revisedImageWidth);
			$(this).width(parseInt(revisedWidth, 10));
		} else {
			$(this).width(totalWidth);
		};
	});		
};

/**
 * Builds pull quote divs assuming you've wrappted your content with a span with the class: pullquote-left or pullquote-right
 */
$.fn.pullQuote = function() {
	return this.each(function() {
		var contents = $.trim($(this).html());
		var firstCharacterCode = contents.charCodeAt(0);
		if (firstCharacterCode < 65 || firstCharacterCode > 96) {
			contents = '&hellip; ' + contents;
		};
		
		var lastCharacter = contents.charAt(contents.length - 1);
		if ("?!.".search(lastCharacter) < 0) {
			contents = contents + ' &hellip;';
		};
		var $parent = $(this).parent();
		var $pullquote = $('<div>').attr('class', $(this).attr('class')).html(contents);
		$parent.before($pullquote);
	});		
};

/**
 * Clears a text form element when it has the style 'clear-default'
 */
$.fn.clickClear = function() {
	return this.each(function() {
		this.defaultValue = $(this).val();
		$(this).click(function() {
			if ($(this).val() == this.defaultValue) {
				$(this).val('');
			};
		}).focus(function() {
			if ($(this).val() == this.defaultValue) {
				$(this).val('');
			};
		}).blur(function() {
			if ($(this).val() == "") {
				$(this).val(this.defaultValue);
			};
		});
		
		$('form').submit(function(event) {
			if ($(this).val() == this.defaultValue) {
				$(this).val('');
			};
		});
	});	
};

var markupPrep = function() {
	var listPrep = function() {
		$('> li:last', 'ul, ol').addClass('last');
	};
	
	var	tablePrep = function() {
		$('table tr:even').addClass('alt');
		$('table td:last, table th:last').addClass('last');
	};
	
	listPrep();
	tablePrep();
};

var	changeTestimonial = function() {
	var $currentItem = $('.testimonials > ul > li:visible');
	var $nextItem = ($currentItem.next()) ? $currentItem.next() : $('.testimonials > ul > li:first');
	
	$currentItem.slideUp('normal', function() {
		$nextItem.slideDown('normal');
	});
};

var rotateTestimonials = function() {
	var timeout;
	if ($('body.home').size()) {
		timeout = setInterval("changeTestimonial()", 15000);
	};
};

$(document).ready(function() {
	// $('input.clear-default').clickClear();
	// $('div.image-left, div.image-right').imageWidth();
	$('span.pullquote-left, span.pullquote-right').pullQuote();
	
	markupPrep();
	rotateTestimonials();
});
