/*
 * lettercount.js
 * 
 * Copyright (c) Takao Tagawa (dounokouno.com)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Since:     2009-06-24
 * Modified:  2010-12-10
 *
 * jQuery 1.6.4
 * 
*/

(function($){
	$(function(){
		// 初期状態でテキストエリアにフォーカス
		$("#text").focus();
		
		// 初期動作でカウント
		$.lettercount.count();
		
		// テキストをクリアボタン
		$("#clear").click(function(){
			$("#text").val("");
			$("#text").focus();
			return false;
		});
		
		// カウントする
		setInterval($.lettercount.count, 500);
		$("#text").bind('keyup', $.lettercount.count);
		
	});
	
	// 処理
	$.lettercount={
		// 文字数をカウント
		count:function(){
			var t = $("#text").val();
			var c = 0;
			
			// 半角スペース、タブ、改行を除去にチェックが入っている場合
			if ($("#remove").attr("checked")) {
				t = t.replace(/[\n\r\s" "]/g, "");
			}
			
			// 文字数
			$("#len").text(t.length);
			
			//バイト数
			c = 0;
			for (i=0; i<t.length; i++) {
				var n = escape(t.charAt(i));
				if (n.length < 4)	{
					c++;
				} else {
					c += 2;
				}
			}
			$("#byte").text(c);
			
			return false;
		}
		
	};
})(jQuery);

