/**
 * Resizes floated image containers to the size of the image
 */
$.fn.imageWidth = function(threshold) {
	/**
	 * Function takes a jquery object and a css property (called dimension) and determines how many pixels the item is
	 * @param {jQuery Object} $item The jQuery object we're looking at
	 * @param {String} dimension The CSS property name to look for
	 * @returns The width or height of the CSS property with 'px' removed
	 * @type Number
	 */
	var	determineDimension = function($item, dimension) {
		$item = $($item);
		if ($item.css(dimension)) {
			return parseInt($item.css(dimension).replace('px', ''), 10);
		} else {
			return 0;
		};
		return false;
	};
	
	var	resizeImage = function($image, $parent, $container) {
		// Determine the width of the image along with borders and padding
		var imageWidth = $image.width();
		var paddingLeft = determineDimension($image, 'padding-left');
		var paddingRight = determineDimension($image, 'padding-right');
		var borderLeft = determineDimension($image, 'border-left-width');
		var borderRight = determineDimension($image, 'border-right-width');

		// Calculate total edge (padding and border) width and the total width (edge and image)
		var edgeWidth = paddingLeft + paddingRight + borderLeft + borderRight;
		var totalWidth = imageWidth + edgeWidth;

		// Determine parent width
		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);
			$container.width(parseInt(revisedWidth, 10));
		} else {
			$container.width(totalWidth);
		};
	};

	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;

		// Find image within div
		var $image = $('img', $(this));
		var $parent = $(this).parent();
		var $container = $(this);
		$image.each(function(index) {
			resizeImage($image, $parent, $container);
			$(this).load(function() {			
				resizeImage($image, $parent, $container);
			});
		});
	});		
};

/**
 * 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 = '… ' + contents;
		};
		
		var lastCharacter = contents.charAt(contents.length - 1);
		if ("?!.".search(lastCharacter) < 0) {
			contents = contents + ' …';
		};
		var $parent = $(this).parent();
		var $pullquote = $('<div>').attr('class', $(this).attr('class')).html(contents);
		$parent.before($pullquote);
	});		
};

/**
 * Makes all children with the selected container the same height.
 * Uses min-height, except for IE6 where height is used instead.
 */
$.fn.same_height = function() {
	return this.each(function() {
		tallest_height = 0;
		
		$(this).children().each(function(index) {
			tallest_height = ($(this).height() > tallest_height) ? $(this).height() : tallest_height;
		});
		
		if ($.browser.msie == true && $.browser.version <= 6) {
			$(this).children().css('height', tallest_height);
		} else {
			$(this).children().css('min-height', tallest_height);
		};
	});		
};

/**
 * Makes the placeholder attribute on input items useful by emulating the behavior
 */ 
var placeholder = function() {
	if ('placeholder' in document.createElement('input')) return this;
	
	$('input[placeholder]').each(function(index) {
		$(this).val($(this).attr('placeholder')).addClass('placeholder')
			.focus(function() {
				if ($(this).val() === $(this).attr('placeholder')) {
			        $(this).val('').removeClass('placeholder');
				}
			})
			.blur(function() {
				if ($(this).val() === ''){
					$(this).val($(this).attr('placeholder')).addClass('placeholder');
				}
			});
	});
};

/**
 * Adds the class 'last' to the last list items, and the last table item in a row. 
 * Also adds alt class to odd table rows.
 */
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();
};

$(window).ready(function() {
	
	/* If this is a landing page, we need to match the height of the
	   light blue bar to the height of the H2. */
	if($("#page-landing")) {
		var topHeight = $("#page-landing .sidebar h2").outerHeight();
		$("#page-landing").css("background-position","0 " + topHeight + "px");
	} 

});

$(window).load(function() {
	placeholder();
	$('div.image-left, div.image-right').imageWidth();
	markupPrep();
	
	$('.balance').same_height();
	
	$layoutDiv = $("#landing-subcontent");	
	
	if($layoutDiv.hasClass("about-layout")) {
		balanceAbout($layoutDiv);
	} else if($layoutDiv.hasClass("life-layout")) {
		balanceLife($layoutDiv);
	} else if($layoutDiv.hasClass("prospective-layout")) {
		balanceProspective($layoutDiv);
	} else if($layoutDiv.hasClass("news-layout")) {
		balanceNews($layoutDiv);
	} else if ($(".page-home").length == 1){
		balanceHome($(".page-home"));
	}
	
	if($(".flowplayer")) {
		$(".flowplayer").flowplayer("/media/yorkwebsite/styleassets/javascript/flowplayer-3.2.7.swf");
	}
});

function balanceHome($layout) {
	$cols = $layout.find(".home-left .aside .aside-canvas");
	tallestHeight = 0;
	$cols.each(function(index) {
		tallestHeight = ($(this).height() > tallestHeight) ? $(this).height() : tallestHeight;
	});
	
	$cols.each(function(index) {
		changeHeight($(this),tallestHeight);
	});
}

function balanceProspective($layout) {
	leftSize = $layout.children("#landing-left").height();
	rightSize = $layout.children("#landing-right").height();
	
	if(leftSize > rightSize) {
		$layout.children("#landing-left").find(".aside .aside-canvas").each(
			function(index) {
				if ($.browser.msie == true && $.browser.version <= 6) {
					$(this).css('height', leftSize);
				} else {
					$(this).css('min-height', leftSize);
				}
			}
		);
	} else {
		$layout.children("#landing-left").find(".aside .aside-canvas").each(
			function(index) {
				if ($.browser.msie == true && $.browser.version <= 6) {
					$(this).css('height', rightSize-3);
				} else {
					$(this).css('min-height', rightSize-3); 
				}
			}
		);
	}
}

function balanceAbout($layout) {
	$cols = $layout.find("#landing-left .colgroup .aside .aside-canvas");
	tallestHeight = 0;
	$cols.each(function(index) {
		tallestHeight = ($(this).height() > tallestHeight) ? $(this).height() : tallestHeight;
	});
	
	$cols.each(function(index) {
		changeHeight($(this),tallestHeight);
	});
}

function balanceNews($layout) {
	$cols = $layout.find("#landing-left .aside .aside-canvas");
	leftHeight = $cols.eq(0).height();
	rightHeight = $cols.eq(1).height();
	if(leftHeight > rightHeight) changeHeight($cols.eq(1),leftHeight);
	else changeHeight($cols.eq(0),rightHeight);
	leftHeight = $cols.eq(0).height();
	rightHeight = $cols.eq(1).height();
}

/**
 * Column balancing functions for different landing page layouts.
 */

function balanceLife($layout) {
	/* First, balance the top section divs */	
	$cols = $layout.children("#landing-left").find(".aside");
	
	$topSections = $cols.find('.section').not('.lower');
	$bottomSections = $cols.find('.section.lower');
	
	tallestHeight = 0;
	
	$topSections.each(function(index) {
		tallestHeight = ($(this).height() > tallestHeight) ? $(this).height() : tallestHeight;
	});
	
	$topSections.each(function(index) {
		changeHeight($(this),tallestHeight);
	});
	
	tallestHeight = 0;
	
	$bottomSections.each(function(index) {
		tallestHeight = ($(this).height() > tallestHeight) ? $(this).height() : tallestHeight;
	});
	
	$bottomSections.each(function(index) {
		changeHeight($(this),tallestHeight);
	});
	
}

function changeHeight($item,newHeight) {
	property = ($.browser.msie == true && $.browser.version <= 6) ? 'height' : 'min-height';
	$item.css(property,newHeight);
}
	
