/*!
 * jQuery thorRowEle plugin: jQuery vertical centering
 * Examples and documentation at: 
 * version 1.0.0 (05/03/2009)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 */

/**
 *  .thorRowEle()をつけたやつの子の要素の高さを揃えますよ。
 *  ex.<div id='footer'><div class='footnavi1'>navi 1</div><div class='footnavi2'>navi 2</div><div class='footnavi3'>navi 3</div></div>
 *  で、$('#footer').thorRowEle();
 *  ってやれば、.footnavi(n)が、一番高さのあるやつと同じ高さになります。
 *  勝手にpadding-bottomとmargin-bottomを0にするあたりが凶悪ですね。
 *  凶悪ついでに、基準になる要素のpadding-bottomの高さまで変えやがります。コレは酷い。
 * @name thorRowEle
 * @type jQuery
 * @cat Plugins/
 * @return jQuery
 * @author psychedesire (http://www.psychedesire.org/)
 */
(function($){
	$.fn.thorRowEle = function(){
		var elemSets = new Array();
		var childCount = $(this).children().length;
		var targetChild = $(this).children();
		for(i = 0;i < childCount;i ++){
			var tCHeight = targetChild.height();
			var tCMarginT = targetChild.css('margin-top').split('p');
			var tCPaddingT = targetChild.css('padding-top').split('p');
			var tCMT = tCMarginT[0];
			var tCPT = tCPaddingT[0];
			tCMT -= 0;
			tCPT -= 0;
			var tCMarginB = targetChild.css('margin-bottom').split('p');
			var tCPaddingB = targetChild.css('padding-bottom').split('p');
			var tCMB = tCMarginB[0];
			var tCPB = tCPaddingB[0];
			tCMB -= 0;
			tCPB -= 0;
			var tAllH = tCHeight + tCMT + tCPT + tCMB + tCPB;
			elemSets[i] = tAllH;
			targetChild = targetChild.next();
		}
		var sumMod = (childCount * (childCount-1)/2);
		var tryCount = 1;
		var x = 0;
		var parentSets = 0;
		var resFlag = true;
		while(tryCount < childCount){
			parentSets = elemSets[x]
			if(parentSets > elemSets[tryCount]){
				tryCount ++;
				resFlag = true;
			}else{
				x = tryCount;
				tryCount ++;
				resFlag = false;
			}
		}
		if(!resFlag){
			parentSets = elemSets[childCount-1];
		}
		var targetChild = $(this).children();
		for(i = 0;i < childCount;i ++){
			targetChild.css({
				"margin-bottom":"0",
				"padding-bottom":"0",
				"height":parentSets  + "px"
			})
			targetChild = targetChild.next();
		}
		$(this).css("padding-bottom",parentSets);
	}

})(jQuery);