(function ($) {
	var otherChildren,
		getHighest,
		getLowestOffset,
		getPadding,
		haveSameParent,
		isWindowLoaded = false;
	
	$(window).load(function () {
		isWindowLoaded = true;
	});
			
	otherChildren = function (subSelector) {
			var totalHeight = 0;
			if (subSelector === undefined) {
				subSelector = '';
			}
			$(this).siblings().not(subSelector).each(function () {
				totalHeight += $(this).outerHeight(true);
			});
			return totalHeight;
	};
	
	getHighest = function (returnRef) {
		var highest = 0,
			highest_ref;
		$(this).each(function () {
			if ($(this).outerHeight(true) > highest) {
				highest = $(this).outerHeight(true);
				highest_ref = this;
			}
		});
		return (returnRef === true ? highest_ref : highest);
	};
	
	getLowestOffset = function (returnRef) {
		var highest = 0,
			highest_ref;
		
		$(this).each(function () {
			if ($(this).offset().top > highest) {
				highest = $(this).offset().top;
				highest_ref = this;
			}
		});
		return (returnRef === true ? highest_ref : highest);
	};
	
	getPadding = function (elem, withMargin) {
		var totalPad = 0
			withMargin = withMargin || false;
		$(elem).each(function () {
			totalPad += $(this).outerHeight(withMargin) - $(this).height();
		});
		return totalPad;
	}
	
	haveSameParent = function (obj) {
		var test = true,
			lastObj = [];
		$.each(obj, function (i, v) {
			if (i > 0 && $(v).parent()[0] !== lastObj[0]) {
				test = false;
				return false;
			}
			lastObj = $(v).parent();
		});
		return test;
	};
	$.fn.makeEqual = function (opts) {
		var highestHeight = 0,
			subs,
			subHeight = 0,
			differentParent = $(),
			lastSubParent,
			farthestDown,
			subParents = [],
			activeParents = [],
			matched = this,
			defaults = {
				subContainers: null,
				subIsSum: false,
				subGrowth: 'even',
			},
			activeOptions,
			Main;
		activeOptions = $.extend({}, defaults, opts);
		Main = function () {
			highestHeight = getHighest.call(matched);
			farthestDown = getLowestOffset.call(matched);
			matched.each(function () {
				var newHeight = 0,
					offsetDifference,
					final = highestHeight,
					t;
				
				if ($(this).height() < highestHeight) {
					final -= getPadding(this, false);
				}
				offsetDifference = farthestDown - $(this).offset().top;
				final += offsetDifference;
				//console.log(offsetDifference);
				$(this).height(final);
				
			});
			
			if (activeOptions.subContainers !== null) {
				subs = matched.children().filter(activeOptions.subContainers);
				
				subs.each(function () {
					if ($.inArray($(this).parent()[0], activeParents) === -1) {
						activeParents.push($(this).parent()[0]);
					}
				});
				$.each(activeParents, function (i, v) {
						var theChildren = $(v).children().filter(activeOptions.subContainers),
							currentParentOffset = farthestDown - $(v).offset().top,
							tallestChild = getHighest.call(theChildren, true),
							remainingChildren = theChildren.not(tallestChild),
							remainingHeight = highestHeight + currentParentOffset - otherChildren.call(theChildren, activeOptions.subContainers),
							distributeHeight = highestHeight + currentParentOffset - otherChildren.call(theChildren, remainingChildren),
							currentParentPadding = getPadding(v, true),
							childPadding = getPadding(theChildren, true),
							temp;
							//console.log((remainingHeight + currentParentOffset) - currentParentPadding);
						//console.log(getPadding(remainingChildren));
						if (activeOptions.subIsSum === true) {
							temp = distributeHeight / remainingChildren.length;
							remainingChildren.height(temp);
						} else {
							theChildren.height((remainingHeight) - currentParentPadding - childPadding);
						}
					});
				}
			};
		if (isWindowLoaded === true) {
			Main();
		} else {
			$(window).load(function () {
				Main();
			});
		}
		return this;
	};
}(jQuery));

