/**
 * 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:odd').addClass('alt');
		$('table td:last, table th:last').addClass('last');
	};
	
	listPrep();
	tablePrep();
};

// Loads in Modernizr script which checks for CSS3 capabilities
$('body').ready(function() {
	$.getScript('/js/modernizr.min.js');
});

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

/*****  The code below needs to be reworked badly *******/ 

<!--

/*  reset the dropdown menu item displayed onload (desired when browser reload has been requested)    document.menuform.choice.selectedIndex = 2;
jump2forone (document.menuform)
*/

function formreset() {
document.menuform.choice.selectedIndex = 0;
}

/*  The jump2forone function is called by a dropdown links menu and is used here to 
             separate the two link-target pairs of the pages to be loaded into two frames.  
    The jump function takes one link and splits off the desired display target.
    The land function displays the link in the target.

    (The jump and land functions were generated by an on-line tool at EchoEcho.com; see 
             the note on syntax below these three functions.)
   */

function jump2forone(menu)
{
ref1=menu.choice.options[menu.choice.selectedIndex].value;
splita=ref1.lastIndexOf("^");
f1src=ref1.substring(0,splita);
jump(f1src);
f2src=ref1.substring(splita+1,1000);
jump(f2src);
} 

function jump(ref)
{
splitc=ref.lastIndexOf("*");
target="";
if (splitc!=-1)
{loc='/'+ref.substring(0,splitc);
target=ref.substring(splitc+1,1000);}
else {loc=ref; target="_self";};
if (ref != "") {land(loc,target);}
}

function land(ref, target)
{
lowtarget=target.toLowerCase();
if (lowtarget=="_self") {window.location=ref;}
else {if (lowtarget=="_top") {top.location=ref;}
else {if (lowtarget=="_blank") {window.open(ref);}
else {if (lowtarget=="_parent") {parent.location=ref;}
else {frames[target].location=ref;};
}}}
}

/*  Syntax Note:        parent.frames[target]
The data set up for these functions consist of two link-target pairs separated by a "^".  Each link is separated from its target by a "*".  The target need not be specified and defaults to the current window.  (It is untested as regards both links using the default target; I would expect the first would begin to display only to be replaced by the second.)

PREDEFINED TARGETS ARE:

_blank : Open in new window
_parent : Open in superior frame
_top : Cancel all frames and open in current window

Examples: 
1)  To add the target "_blank", use this syntax:
      http://www.yahoo.com*_blank
2) A link-target pair
      http://www.yahoo.com*_blank^file.html*namedframe

    */
  -->