$(document).ready(function() {
	//問題文
	text = new Array;
	text[0] = "Just at this moment her head struck against the roof of the hall; in fact, she was now rather more than nine feet high, and she at once took up the little golden key and hurried off to the garden door.";
	text[1] = "As she said these words, her foot slipped, and in another moment, splash! she was up to her chin in salt-water. Her first idea was that she had somehow fallen into the sea. However, she soon made out that she was in the pool of tears which she had wept when she was nine feet high.";
	text[2] = "After awhile, finding that nothing more happened, she decided on going into the garden at once; but, alas for poor Alice! When she got to the door, she found she had forgotten the little golden key, and when she went back to the table for it, she found she could not possibly reach it: she could see it quite plainly through the glass and she tried her best to climb up one of the legs of the table, but it was too slippery, and when she had tired herself out with trying, the poor little thing sat down and cried."
	
	//空白置き換え
	function checkChars(nextChar) {
		if(nextChar == " ") {
			nbsp = "&nbsp;";
			nextChar = original.charAt(++charNum);
			spaces++;
		}
		return nextChar;
	}
	//計測
	function count() {
		timer++;
		$("#result").empty();
		$("#result").append("ミス"+miss+"/タイム"+Math.floor(timer*0.1));
	}
	
	//初期化
	var original = "";//問題文のオリジナル
	var charNum = 0;//現在の文字位置
	var type = 0;//打鍵回数
	var miss = 0;//入力ミスの回数
	var timer = 0;//タイマー
	var spaces  = 0;//スペースの数
	var start = 0;//スタートの判断
	var choosed = -1;//問題文の番号
	var endOfChar;//全文字数
	var counter;//カウンターのID
	$("#question").css({"border":"1px solid #999","padding":"10px;"});
	$("#result").css({"text-align":"center"});
	
	$("#choose").change(function() {
		clearInterval(counter);
		charNum = 0;//現在の文字位置
		type = 0;//打鍵回数
		miss = 0;//入力ミスの回数
		timer = 0;//タイマー
		spaces  = 0;//スペースの数
		start = 0;//スタートの判断
		choosed = $(this).val();
		if(start==0&&choosed!=-1) {
			original = text[choosed];
			$("#question").html(original);
			$("#question").css({"padding":"10px;"});
			endOfChar = original.length;
		} else {
			$("#question").html("<p>問題を選んでください</p>");
		}
	});
	
	//キー処理
	$(document).keypress(function(key) {
		var kCode = key.charCode || key.keyCode || 0;//キー取得
		if(kCode == 32)  {
			return false;
		}
		if(start==0&&choosed!=-1) {//初回キー押下かどうか
			start = 1;
			counter = setInterval(count,100);
		} else {//2回目以降なら
			type++;//タイプ回数加算
			var inputChar = String.fromCharCode(kCode);//キーコード取得
			var nextChar = original.charAt(charNum++);//文字抜き出し
			if(inputChar == nextChar) {
				nbsp = "";
				nextChar = checkChars(original.charAt(charNum));
				var firstHalf = original.substr(0,charNum);
				var lastHalf = original.substr(charNum+1,endOfChar);
				$("#question").html(firstHalf+"<span style='text-decoration:underline'>"+nextChar+"</span>"+lastHalf);
				if(charNum>=endOfChar) {
					clearInterval(counter);
					var result = "キーを打った回数:"+type+"回<br />"+"間違えた回数:"+miss+"回<br />"+"正打率:"+Math.round((endOfChar-spaces)/type*100)+"%<br />"+"かかった時間:"+Math.round(timer/10)+"秒";
					$("#result").empty();
					$("#result").html(result);
				}
			} else {
				miss++;
				charNum--;
			}
		}
	});
});
