/*
 * jquery-auto-height.js
 *
 * Copyright (c) 2010 Tomohiro Okuwaki (http://www.tinybeans.net/blog/)
 * Licensed under MIT Lisence:
 * http://www.opensource.org/licenses/mit-license.php
 * http://sourceforge.jp/projects/opensource/wiki/licenses%2FMIT_license
 *
 * Since:   2010-04-19
 * Update:  2010-04-19
 * Comment: 
 *
 * jQuery 1.2 later
 * 
 */
 
 (function($){
    $.fn.autoHeight = function(line){
        var self = $(this);

        function descend (a,b){ return b-a; }

        var n = 0,
            hMax,
            hList = new Array(),
            hListLine = new Array();
            hListLine[n] = 0;

        // 要素の高さを取得
        self.each(function(i){
            var h = $(this).height();
            hList[i] = h;
            if (line > 1) {
                // lineごとの最大値を格納していく
                if (h > hListLine[n]) {
                    hListLine[n] = h;
                }
                if ( (i > 0) && (((i+1) % line) == 0) ) {
                    n++;
                    hListLine[n] = 0;
                };
            }
        });

        // 取得した高さの数値を降順に並べ替え
        hList = hList.sort(descend);
        hMax = hList[0];
        
        // 高さの最大値を要素に適用
        var browser = $.browser.version;
        if (line > 1) {
            for (var j=0; j<hListLine.length; j++) {
                for (var k=0; k<line; k++) {
                    if (browser == '6.0') {
                        self.eq(j*line+k).height(hListLine[j]);
                    } else {
                        self.eq(j*line+k).css('minHeight',hListLine[j]);
                    }
                }
            }
        } else {
            if (browser == '6.0') {
                self.height(hMax);
            } else {
                self.css('minHeight',hMax);
            }
        }
    };
})(jQuery);


jQuery(function($){
    $('.flat01').autoHeight({column:5});
    $('.flat02').autoHeight({column:5});
    $('.flat03').autoHeight({column:5});
    $('.flat04').autoHeight({column:5});
    $('.flat05').autoHeight({column:3});
});


