/**
 * Equal Heights Plugin
 * Equalize the heights of elements. Great for columns or any elements
 * that need to be the same size (floats, etc).
 * 
 * Version 1.0
 * Updated 12/10/2008
 *
 * Copyright (c) 2008 Rob Glazebrook (cssnewbie.com) 
 *
 * Richard A. Walenga,
 * Added the capability to equalize heights by row id set.
 * Added the capability to equalize heights irrespective of padding and optionally margins.
 * Usage: $(object).equalHeights({minHeight:0, maxHeight: 400, includeMargins: false});
 * 
 * Example 1: $(".cols").equalHeights(); Sets all columns to the same height.
 * Example 2: $(".cols").equalHeights({minHeight:400}); Sets all cols to at least 400px tall.
 * Example 3: $(".cols").equalHeights({minHeight:100,maxHeight:300}); Cols are at least 100 but no more
 * than 300 pixels tall. Elements with too much content will gain a scrollbar.
 * 
 */

(function($) {
    $.fn.equalHeights = function(params) {
    	params = jQuery.extend({minHeight:0,includeMargins:true},params);
        var tallest = [];
        this.each(function() {
            var $self = $(this);
            var height = $self.outerHeight(params.includeMargins);
            var matches = $self.attr("id").match(/(\d+)_\d+$/)
            var row = Number(matches[1]);
            if (row > tallest.length) {
                tallest.push(params.minHeight);
            }
            if (height > tallest[row - 1]) {
                tallest[row - 1] = height;
            }
        });
        if (params.maxHeight) {
            for (var rowHeight in tallest) {
                if (rowHeight > params.maxHeight) {
                    rowHeight = params.maxHeight;
                }
            }
        }
        return this.each(function() {
            var $self = $(this);
            var row_index = Number($self.attr("id").match(/(\d+)_\d+$/)[1]) - 1;
            var diff = $self.outerHeight(params.includeMargins) - $self.height();
            $self.height(tallest[row_index] - diff).css("overflow", "auto");
        });
    }
})(jQuery);
