/* jquery.setColHeight.js
 *
 * +jquery-1.4.2
 * 2010 Yuki.Imayoshi
 */
$(function(){
	$.fn.extend({
		'setColHeight' : function(_target,_num) {
			var settings = $.extend({
			                        	targ : _target,
			                        	num  : _num 
			}, $.fn.setColHeight.defaults);
			return this.each(function() {
				var $t = $(this);
				var o= $.metadata ? $.extend({}, settings, $t.metadata()) : settings;
				var i = 0;j = 0;
				var length;
				var newHeight = 0;
				$t.num = Number(Math.floor(o.num));
				
				var init = function() {
					length = $(o.targ,$t).length;
					if(!$t.num || $t.num <= 0) {
						$t.num = length;
					}
					$(o.targ,$t).css({height : 'auto'});
					setHeight(0);
				}
				
				var setHeight = function(count) {
					for(i=0; i<$t.num; i+=1) {
						var index = i + count * $t.num;
						if($(o.targ+':eq('+index+')',$t)) {
							var targetHeight = $(o.targ+':eq('+index+')',$t).height();
							if(newHeight < targetHeight){
								 newHeight = targetHeight;
							}
						}
					}
					for(j=0; j<$t.num; j+=1) {
						index = j + count * $t.num;
						if($(o.targ+':eq('+index+')',$t)) {
							$(o.targ+':eq('+index+')',$t).css({height : newHeight + 'px'});
						}
					}
					count += 1;
					newHeight = 0;
					if(count*$t.num < length) {
						setHeight(count);
					}
				}
					
				init();
			});
		}
	});
	$.fn.setColHeight.defaults = {}
});
