jQuery.changeLetterSize = {
	handlers : [],
	interval : 1000,
	currentSize: 0
};

(function($) {

	var self = $.changeLetterSize;

	/* 文字の大きさを確認するためのins要素 */
	var ins = $('<ins>M</ins>').css({
		display: 'block',
		visibility: 'hidden',
		position: 'absolute',
		padding: '0',
		top: '0'
	});
	
	var timer = 0;
	/* 文字の大きさが変わったか */
	var isChanged = function() {
		ins.appendTo('body');
		var size = ins[0].offsetHeight;
		ins.remove();
		if (self.currentSize == size) return false;
		if (timer < 2) {
			timer +=1;
		} else {
			self.currentSize = size;
			timer = 0;
		};
		return true;
	};
	
	/* 文書を読み込んだ時点で
	   文字の大きさを確認しておく */
	$(isChanged);
	
	/* 文字の大きさが変わっていたら、
	   handlers中の関数を順に実行 */
	var observer = function() {
		if (!isChanged()) return;
		$.each(self.handlers, function(i, handler) {
			handler();
		});
	};

	/* ハンドラを登録し、
	   最初の登録であれば、定期処理を開始 */
	self.addHandler = function(func) {
		self.handlers.push(func);
		if (self.handlers.length == 1) {
			setInterval(observer, self.interval);
		}
	};

})(jQuery);

/*
======================================================================
	$(expr).flatHeights()
	$(expr)で選択した複数の要素について、それぞれ高さを
	一番高いものに揃える
======================================================================
*/

/* 対象となる要素群の集合 */
var FLAT_HEIGHT_SETS = [];
(function($) {

	/* 対象となる要素群の集合 */
//	var sets = [];

	/* 高さ揃えの処理本体 */
	var flatHeights = function(set) {
//	console.log('flatHeights')
		var maxHeight = 0;
		set.each(function(){
			var height = $(this).height();
//			var height = this.offsetHeight;
			if (height > maxHeight) maxHeight = height;
		});
		if (maxHeight > 0) {
			set.css('height', maxHeight + 'px');
		}
	};

	/* 要素群の高さを揃え、setsに追加 */
	jQuery.fn.flatHeights = function() {
//		if (initFlag) sets = [];
//	console.log('jQuery.fn.flatHeights')
//	console.log(FLAT_HEIGHT_SETS.length)
		if (this.length > 1) {
			flatHeights(this);
			FLAT_HEIGHT_SETS.push(this);
		}
		return this;
	};

	/* 文字の大きさが変わった時に、
	   setsに含まれる各要素群に対して高さ揃えを実行 */
	$.changeLetterSize.addHandler(function() {
		$.each(FLAT_HEIGHT_SETS, function() {
			this.height('auto');
			flatHeights(this);
		});
	});
	/*
	$(document).bind("click", function(){
		$.each(FLAT_HEIGHT_SETS, function() {
			this.height('auto');
			flatHeights(this);
		});
	});
	*/
})(jQuery);



function setFlatHeightElements() {
	/**
	* 【使い方】
	*
	* 1.親要素のclassに flatHeight-（1行の子要素数） を指定する。
	*   設定可能な親要素はdivとulのみ。
	*   例）<ul class="clearfix flatHeight-4">
	* 
	* 2.全子要素の高さを揃えたい場合は、
	*   親要素のclassに flatHeightParent を指定する。
	*   例）<ul class="clearfix flatHeightParent">
	*
	* 3.孫要素の高さを揃えたい場合は、
	*   親要素のclassに flatHeight-（1行の子要素数）C（孫要素のindex（0～）） を指定する。
	*   例）<ul class="clearfix flatHeight-4C1">
	*         <li>
	*           <div>                   </div>
	*           <div>このdivの高さが揃う</div>
	*           <div>                   </div>
	*
	* setFlatHeightElements()を実行することで要素リストが作成される。
	* ページがロードされた時、または、動的に作成された要素の高さを揃えたい場合に実行する。
	* 
	*/
	FLAT_HEIGHT_SETS = [];
	$('div, ul').filter(function (index) {
		return $(this).get(0).className.indexOf('flatHeight-') > -1
	}).each(function(i) {
		var clsList = $(this).get(0).className.split(' ');
		var splitChildLength;
		for (j=0;j<clsList.length;j++) {
			if (clsList[j].indexOf('flatHeight-') > -1) {
				splitChildLength = clsList[j].split('flatHeight-');
				break;
			}
		}
		var childIndex = splitChildLength[1].split('C');
		splitChildLength = parseInt(childIndex[0]);
		if (childIndex[1]) {
			childIndex = childIndex[1];
		} else {
			childIndex = undefined;
		}
		var sets = [], temp = [];
		$(this).children('div:visible, li:visible').each(function(i) {
			if (childIndex == undefined) {
				temp.push(this);
			} else {
				temp.push($('*:eq(' + childIndex + ')', this).get(0));
			}
			if (i % splitChildLength == (splitChildLength - 1)) {
				sets.push(temp);
				temp = [];
			}
		});
		if (temp.length) sets.push(temp);
		$.each(sets, function() {
			$(this).flatHeights();
		});
	});
	$('div, ul').filter(function (index) {
		return $(this).get(0).className.indexOf('flatHeightParent') > -1
	}).each(function(i) {
		var sets = [], temp = [];
		$(this).children().each(function(i) {
			temp.push(this);
		});
		if (temp.length) sets.push(temp);
		$.each(sets, function() {
			$(this).flatHeights();
		});
	});
}
