(function($){
	$.fn.stretchToMatch = function(settings) {
		settings = $.extend({
				matchElement: null,
				stretchElement: null
			}, settings);

		var matches = this;

		function _initialize() {
			if (matches.length <= 0)
				return;
			if (null == settings.matchElement)
				return;

			var matchElement = $(settings.matchElement);
			if (matchElement.length <= 0)
				return;

			var height = matchElement.height();
			matches.each(function(i, el) {
					var $el = $(el);
					var elHeight = $el.outerHeight(true);
					if (elHeight < height) {
						if (null != settings.stretchElement) {
							var stretchElement = $(settings.stretchElement, $el);
							if (stretchElement.length > 0) {
								var hdiff = height - elHeight;
								var perElement = Math.floor(hdiff / stretchElement.length);
								var fraction = hdiff % stretchElement.length;
								stretchElement.each(function(j, sel) {
									$stretch = $(sel);
									$stretch.height(perElement + (j < fraction ? 1 : 0) + $stretch.height());
								});
								return;
							}
						}
						$el.height(height);						
					}
				});
		}
		
		_initialize();
		return this;
	}
})(jQuery);
