var TypingTest = {

	create: function() {
		this.dbConnectManager = new DBConnectManager();
		sessionStorageManager.setRecord(0);

		var self = this;

		this.initTypingData();
		sessionStorageManager.setIsNewAppHighestRecord(false);

		var experienceAppTimer = new ExperienceAppTimer();
		experienceAppTimer.setTimeOverListener(
			(function() { this.timeOver(); }).bind(this)
		);

		game.stage.backgroundColor = '#4d4d4d';

		/*
		// keyboard shortcut - useless. this.keyboard is chargning for ESC event
		this.keyboardShortcut = new KeyboardShortcut();
		this.keyboardShortcut.addCallback(
			Phaser.KeyCode.ESC,	// keyCode
			null,	// keyDownHandler
			(function() { this.back(); }).bind(this),	// keyUpHandler
			"keyboard_test"	// callerClassName
		);
		*/

		// top ui
		var screenTopUI = new ScreenTopUI();
		screenTopUI.makeBackButton( (function() { this.back(); }).bind(this) );
		screenTopUI.makeFullScreenButton();


		this.averageTypingSpeedText = new AverageTypingSpeed();
		this.contentProgressText = new ContentProgress();


		// typing content
		var typingContentBG = new TypingContentBG();
		typingContentBG.makeTestContentBG();

		this.animalList = new AnimalList(90);


		var TYPING_CONTENT_Y = 230;	// 320;

		if( (isKoreanTypingApp() && isTypingSentenceApp())
			|| (isEnglishTypingApp() && isTypingSentenceApp()) )
			textStyleBasic.font = "bold 48px Arial";
		else
			textStyleBasic.font = "bold 52px Arial";

		this.textTypingContent = game.add.text(
			game.world.centerX, TYPING_CONTENT_Y,
			"", textStyleBasic
		)
			.setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2)
			.addColor(TypingTest.COLOR_CONTENT_INPUT, 0);
		this.textTypingContent.anchor.set(0.5);

		var TYPING_OFFSET_Y = 55;
		var TYPING_TEXT_OFFSET_Y = 35;
		textStyleBasic.font = "28px Arial";

		var textDoneColor = [ '#99994d', '#77774d', '#66664d' ];
		this.textTypingContentsDone = [];
		this.textTypingRecordsDone = [];
		for(var i = 0; i < TypingTest.TYPING_CONTENT_DONE_COUNT; i++) {
			this.textTypingContentsDone[i] = game.add.text(
				game.world.centerX, TYPING_CONTENT_Y + TYPING_OFFSET_Y + i * TYPING_TEXT_OFFSET_Y,
				"", textStyleBasic
			)
				// .setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2)
				.addColor(textDoneColor[i], 0);
			this.textTypingContentsDone[i].anchor.set(0.5);

			var SCORE_POSITION_X = 900;
			if(isTypingWordApp())
				SCORE_POSITION_X = 700;

			this.textTypingRecordsDone[i] = game.add.text(
				SCORE_POSITION_X, TYPING_CONTENT_Y + TYPING_OFFSET_Y + i * TYPING_TEXT_OFFSET_Y,
				"", textStyleBasic
			)
				// .setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2)
				.addColor(textDoneColor[i], 0);
			this.textTypingRecordsDone[i].anchor.set(0.5);
		}

		var textPreviewColor = [ '#999999', '#888888', '#777777' ];
		this.textTypingContentPreview = [];
		for(var i = 0; i < TypingTest.TYPING_CONTENT_PREVIEW_COUNT; i++) {
			this.textTypingContentPreview[i] = game.add.text(
				game.world.centerX, TYPING_CONTENT_Y - TYPING_OFFSET_Y - i * TYPING_TEXT_OFFSET_Y,
				"", textStyleBasic
			)
				// .setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2)
				.addColor(textPreviewColor[i], 0);
			this.textTypingContentPreview[i].anchor.set(0.5);
		}


		// input text
		this.inputTextContent = new InputTypeText(game.world.centerX, TYPING_CONTENT_Y + 120);
		this.inputTextContent.anchor.set(0.5);
		this.inputTextContent.canvasInput.value('');
		this.inputTextContent.canvasInput.focus();
		this.inputTextContent.canvasInput.fontSize(26);
		this.inputTextContent.canvasInput.placeHolder("입력 후 Space, Enter키를 누르세요");
		// inputTextContent.canvasInput._onkeydown = this.checkInputText;
		// inputTextContent.canvasInput._onkeyup = function() {
		this.inputTextContent.canvasInput._onkeyup = function() {
			self.checkTypingContents(event);
			// warning
			// 	 : checkTypingContents is called onkeyup and onkeydown
		}


		// keyboard
		this.keyMapper = new KeyMapper();
		this.keyboard = null;
		if(sessionStorageManager.getPlayingAppName().indexOf("korean") > 0)
			this.keyboard = new Keyboard(this.keyMapper, Keyboard.KOREAN, Keyboard.NO_OFFSET_POS_Y);
		else
			this.keyboard = new Keyboard(this.keyMapper, Keyboard.ENGLISH, Keyboard.NO_OFFSET_POS_Y);
		this.keyboard.addCallback(
			Phaser.KeyCode.ESC,	// keyCode
			null,	// keyDownHandler
			(function() { this.back(); }).bind(this),	// keyUpHandler
			"keyboard_test"	// callerClassName
		);

		// hands
		this.leftHand = new LeftHand(this.keyMapper);
		this.rightHand = new RightHand(this.keyMapper);



		// bottom ui
		var screenBottomUI = new ScreenBottomUI();
		screenBottomUI.printLeftTextWithAppHighestRecord(sessionStorageManager.getAppHighestRecord());
		screenBottomUI.printCenterText(sessionStorageManager.getPlayingAppKoreanName());
		screenBottomUI.printRightText(sessionStorageManager.getPlayerName());


		this.startGame();
		// this.countDown();
	},

	/*
	countDown() {
		var style = { font: "bold 200px Arial", fill: "#fff", boundsAlignH: "center", boundsAlignV: "middle" };
		this.countDownText = game.add.text(0, 0, "", style);
		this.countDownText.setTextBounds(0, 0, game.world.width, game.world.height);
		this.countDownText.stroke = "#333";
		this.countDownText.strokeThickness = 50;

		this.countDownNumber = 3;
		if(isDebugMode())
			this.countDownNumber = 1;
		this.tweenCountDown();
	}

	tweenCountDown() {
		if(this.countDownNumber === 0) {
			this.startGame();
			return;
		}

		this.countDownText.text = this.countDownNumber.toString();
		this.countDownText.alpha = 1;

		var countDownTween = game.add.tween(this.countDownText);
		countDownTween.to( { alpha: 0 }, GAME_SCREEN_SIZE.x, Phaser.Easing.Linear.None, true);
		countDownTween.onComplete.add(this.tweenCountDown, this);

		this.countDownNumber--;
	}
	*/


	back: function() {
		sessionStorageManager.resetPlayingAppData();
		location.href = '../../web/client/main_menu.html';
	},

	startGame: function() {
		this.averageTypingSpeedText.print(0);

		this.showTypingTestContents();
		this.showPlayingWordNumber();
		this.updateKeyboard();
	},

	timeOver: function() {
		var timeOverText = new TimeOverText();
		this.stopAndGoResult();
	},

	gameOver: function() {
		var missionClearText = new MissionClearText();
		this.stopAndGoResult();
	},

	stopAndGoResult: function() {
		this.animalList.stopAnimation();

		sessionStorageManager.setRecord(this.typingRecordTotal);
		if(!isExperienceMaestroAccount())
			this.updateResultRecord();

		game.time.events.add(Phaser.Timer.SECOND * 2, this.goResult, this);
	},

	updateResultRecord: function() {
		if(sessionStorageManager.getMaestroAccountType() < 100) {	// experience account
			this.dbConnectManager.updateResultRecord(
				sessionStorageManager.getMaestroID(),
				sessionStorageManager.getPlayerID(),
				sessionStorageManager.getPlayingAppID(),
				sessionStorageManager.getRecord()
			);
		}
	},

	goResult: function() {
		location.href = '../../web/client/result.html';
	},


	initTypingData: function() {
		var typingTextMan = new TypingTextManager();

		var typingPracticeContents = this.getTypingTestContents();
		// var typingPracticeContents = [ "십년이면 강산도 변한다." , "십년이면 강산도 변한다.", "십년이면 강산도 변한다." ];	// for debug
		// var typingPracticeContents = [ "꿰뚫다" , "워머신", "홧김에" ];	// for debug
		// console.log(typingPracticeContents);
		typingTextMan.makeTestContents(typingPracticeContents);
		var wordCountForStage = TypingTest.WORD_COUNT_FOR_STAGE;
		// if(isDebugMode())
		// 	wordCountForStage = 3;
		typingTextMan.slice(0, wordCountForStage);
		this.typingRandomContents = typingTextMan.getContents();
		// console.log(this.typingRandomContents);
		this.typingContentLength = this.typingRandomContents.length;
		this.typingIndex = 0;


		this.isGameOver = false;
		this.isTyping = false;
		this.timeTypingStart = 0;
		this.timeTypingEnd = 0;

		this.typingLetterCountTotal = 0;
		this.typingRecordTotal = 0;
	},


	getTypingTestContents: function() {
		var typingTestContents = this.loadTestContent();
		// console.log('playingStageData.language : ' + playingStageData.language);
		// console.log('playingStageData.level : ' + playingStageData.level);
		// console.log('typingTestContents : ' + typingTestContents);

		this.typingRecordForLines = [];
		this.typingElapsedTime = [];
		for(var i = 0; i < typingTestContents.length; i++) {
			this.typingRecordForLines[i] = 0;
			this.typingElapsedTime[i] = 0;
		}

		return typingTestContents;
	},

	loadTestContent: function() {
		var appName = "";
		if(isTypingTestApp())
			appName = sessionStorageManager.getPlayingAppName().substring(5);
		else if(isTypingPracticeApp())
			appName = sessionStorageManager.getPlayingAppName().substring(9);

		var testContent = [];
		switch(appName) {
			case "korean_basic":
			testContent = koreanBasicWordList;
			break;
			case "korean_left_upper":
			testContent = koreanLeftUpperWordList;
			break;
			case "korean_second_finger":
			testContent = koreanSecondFingerWordList;
			break;
			case "korean_right_upper":
			testContent = koreanRightUpperWordList;
			break;
			case "korean_left_lower":
			testContent = koreanLeftLowerWordList;
			break;
			case "korean_right_lower":
			testContent = koreanRightLowerWordList;
			break;
			case "korean_left_upper_double":
			testContent = koreanLeftUpperDoubleWordList;
			break;
			case "korean_right_upper_double":
			testContent = koreanRightUpperDoubleWordList;
			break;
			case "korean_word":
			testContent = koreanWordList;
			break;
			case "korean_sentence":
			testContent = koreanSentenceList;
			break;

			case "english_basic":
			testContent = englishBasicWordList;
			break;
			case "english_left_upper":
			testContent = englishLeftUpperWordList;
			break;
			case "english_second_finger":
			testContent = englishSecondFingerWordList;
			break;
			case "english_right_upper":
			testContent = englishRightUpperWordList;
			break;
			case "english_left_lower":
			testContent = englishLeftLowerWordList;
			break;
			case "english_right_lower":
			testContent = englishRightLowerWordList;
			break;
			case "english_word":
			testContent = englishWordList;
			break;
			case "english_sentence":
			testContent = englishSentenceList;
			break;
		}

		return testContent;
	},

	showTypingTestContents: function() {
		for(var i = 0; i < TypingTest.TYPING_CONTENT_DONE_COUNT; i++) {
			var doneIndex = this.typingIndex - i - 1;
			if(doneIndex < 0) {
				this.textTypingContentsDone[i].text = "";
				this.textTypingRecordsDone[i].text = "";
			} else {
				this.textTypingContentsDone[i].text = this.typingRandomContents[doneIndex];
				this.textTypingRecordsDone[i].text = Math.floor(this.typingRecordForLines[doneIndex]);
			}
		}

		if(this.typingIndex == this.typingContentLength) {
			this.textTypingContent.text = "";
			return;
		}

		this.textTypingContent.text = this.typingRandomContents[this.typingIndex];

		for(var i = 0; i < TypingTest.TYPING_CONTENT_PREVIEW_COUNT; i++) {
			var previewIndex = this.typingIndex + i + 1;
			if(previewIndex < this.typingRandomContents.length)
				this.textTypingContentPreview[i].text = this.typingRandomContents[previewIndex];
			else
				this.textTypingContentPreview[i].text = "";
		}
	},

	resetTypingContent: function() {
		this.textTypingContent.clearColors();
		this.textTypingContent.addColor(TypingTest.COLOR_CONTENT_INPUT, 0);
		this.inputTextContent.canvasInput.value('');
	},

	checkTypingContents: function(event) {
		if(this.isGameOver)
			return;

		var inputContent = this.inputTextContent.canvasInput.value().trim();
		var typingContent = this.typingRandomContents[this.typingIndex];

		if(event.keyCode == Phaser.Keyboard.ENTER ||
			event.keyCode == Phaser.Keyboard.SPACEBAR) {
			// console.log("### enter ###");
			// console.log("this.typingIndex : " + this.typingIndex);
			// console.log("inputContent : " + inputContent);
			// console.log("typingContent : " + typingContent);

			if(inputContent === typingContent) {
				this.calculateTypingRecord(typingContent);

				// console.log("this.typingIndex : " + this.typingIndex);
				// console.log("this.typingContentLength : " + this.typingContentLength);
				if(this.typingIndex == this.typingContentLength - 1) {
					this.isGameOver = true;
					this.gameOver();
					return;
				}

				this.playNextContent();
				return;
			}
		}

		if(this.isTyping == false) {
			// console.log(event);
			this.setTimeTypingStart();
		}

		this.showTypingContentHighlight(inputContent, typingContent);
		this.updateKeyboard();
	},

	showTypingContentHighlight: function(inputContent, typingContent) {
		this.textTypingContent.clearColors();
		this.textTypingContent.addColor(TypingTest.COLOR_CONTENT_RIGHT, 0);

		if(typingContent == null)
			return;

		var typingContentLength = typingContent.length;
		for(var i = 0; i < typingContentLength; i++) {
			// console.log("typingContent.charAt(i) : " + typingContent.charAt(i));
			// console.log("inputContent.charAt(i) : " + inputContent.charAt(i));

			if(typingContent.charAt(i) != inputContent.charAt(i)) {
				this.textTypingContent.addColor(TypingTest.COLOR_CONTENT_INPUT, i);
				return;
			}
		}
	},

	updateKeyboard: function() {
		var inputContent = this.inputTextContent.canvasInput.value();
		var typingContent = this.typingRandomContents[this.typingIndex];

		this.hideHighlightKey(this.highlightKey);
		this.highlightKey = this.getHighlioghtKey(inputContent, typingContent);
		// console.log(this.highlightKey);
		this.showHighlightKey(this.highlightKey);
		this.moveHands(this.highlightKey);
	},

	getHighlioghtKey: function(inputContent, typingContent) {
		if(typingContent == undefined)
			return undefined;

		var highlightKey = "";
		var inputIndex = inputContent.length - 1;
		var inputLength = inputContent.length;
		var typingLength = typingContent.length;
		// console.log("inputLength : " + inputLength + " / " + inputContent);
		// console.log("typingLength : " + typingLength + " / " + typingContent);
		var misspelledIndex = -1;
		for(var i = 0; i < typingLength; i++) {
			// console.log("inputContent[i] : " + inputContent[i] + ", typingContent[i] : " + typingContent[i]);
			if(inputContent[i] != typingContent[i]) {
				misspelledIndex = i;
				break;
			}
		}

		// over typing
		if(inputLength > typingLength) {
			highlightKey = "Backspace";
		}

		// complete typing
		else if(inputContent == typingContent) {
			highlightKey = "Enter";
		}

		// english typing test
		else if(isEnglishTypingApp()) {
			// console.log("===== english");
			// console.log("misspelledIndex : " + misspelledIndex);
			// console.log("inputIndex : " + inputIndex);
			if(misspelledIndex > inputIndex)
				highlightKey = typingContent[misspelledIndex];
			else
				highlightKey = "Backspace";
		}
		// korean typing test
		else {
			// console.log(misspelledIndex + " / " + typingContent[misspelledIndex]);
			if(typingContent[misspelledIndex] == " ")
				highlightKey = " ";
			else {
				var inputLetter = "";
				var typingLetter = "";
				var typingNextLetter = "";

				// console.log("-----");
				// console.log("inputConent : '" + inputContent + "'");
				// console.log("inputIndex : " + inputIndex);
				// console.log("typingLength : " + typingLength);
				// console.log("misspelledIndex : " + misspelledIndex);

				if(inputIndex < 0)
					inputLetter = "";
				else if(misspelledIndex > inputIndex)
					inputLetter = "";
				else
					inputLetter = inputContent[misspelledIndex];
				typingLetter = typingContent[misspelledIndex];
				if(misspelledIndex <= typingContent.length)
					typingNextLetter = typingContent[misspelledIndex + 1];

				// console.log("inputLetter : '" + inputLetter + "'");
				// console.log("typingLetter : '" + typingLetter + "'");
				// console.log("typingNextLetter : '" + typingNextLetter + "'");

				// var typingAlphabet = "";
				var inputLetterAlphabets = StringUtil.toKoreanAlphabets(inputLetter);
				// var typingLetterAlphabets = StringUtil.toKoreanAlphabets(typingLetter);
				// var typingNextLetterAlphabets = StringUtil.toKoreanAlphabets(typingNextLetter);
				// console.log("inputLetterAlphabets : " + inputLetterAlphabets);
				// console.log("typingLetterAlphabets : " + typingLetterAlphabets);
				// console.log("typingNextLetterAlphabets : " + typingNextLetterAlphabets);

				if(misspelledIndex < inputIndex) {
					highlightKey = "Backspace";
				}

				// typing nothing yet
				else if(inputLetterAlphabets.length == 0) {
					// highlightKey = typingLetterAlphabets[0];
					highlightKey = StringUtil.chosung(typingLetter);
				}

				// after typing chosung
				else if(inputLetterAlphabets.length == 1) {
					if(StringUtil.chosung(inputLetter) != StringUtil.chosung(typingLetter))
						highlightKey = "Backspace";
					else {
						if(StringUtil.isMultiAlphabetJungsung(StringUtil.jungsung(typingLetter))) {
							// console.log("* multi jungsung");
							var jungsungLetter = StringUtil.jungsung(typingLetter);
							// console.log("jungsungLetter : " + jungsungLetter);
							jungsungAlphabets = StringUtil.getMultiAlphabetFromJungsung(jungsungLetter);
							// console.log("jungsungAlphabets : " + jungsungAlphabets);
							highlightKey = jungsungAlphabets[0];
							// console.log(highlightKey);
						} else {
							// console.log("* single jungsung");
							highlightKey = StringUtil.jungsung(typingLetter);
							// console.log(highlightKey);
						}
					}
				}

				// after typing jungsung
				else if(inputLetterAlphabets.length == 2) {
					var typingLetterJungsung = StringUtil.jungsung(typingLetter);
					// console.log("* 222");
					// inputLetter jungsung == typingLetter jungsung

					// inputLetter chosung != typingLetter chosung
					if(StringUtil.chosung(inputLetter) != StringUtil.chosung(typingLetter))
						highlightKey = "Backspace";

					else if(StringUtil.jungsung(inputLetter) == StringUtil.jungsung(typingLetter)) {
						if(StringUtil.hasJongsung(typingLetter)) {
							// highlightKey = StringUtil.jongsung(typingLetter);
							var typingLetterJongsung = StringUtil.jongsung(typingLetter);
							// typingLetter jongsung is multi alphabet
							if(StringUtil.isMultiAlphabetJongsung(typingLetterJongsung)) {
								var typingJongsungAlphabets = StringUtil.getMultiAlphabetFromJongsung(StringUtil.jongsung(typingLetter));
								highlightKey = typingJongsungAlphabets[0];
							}
							// typingLetter jongsung is single alphabet
							else {
								highlightKey = typingLetterJongsung;
							}
						} else
							highlightKey = StringUtil.chosung(typingNextLetter);
					}
					// inputLetter jungsung != typingLetter jungsung
					else {
						if(StringUtil.isMultiAlphabetJungsung(typingLetterJungsung)) {
							// console.log("* multi jungsung");
							var jungsungAlphabets = StringUtil.getMultiAlphabetFromJungsung(typingLetterJungsung);
							// console.log("  * jungsungAlphabets : " + jungsungAlphabets);
							if(StringUtil.jungsung(inputLetter) == jungsungAlphabets[0])
								highlightKey = jungsungAlphabets[1];
							else
								highlightKey = "Backspace";
						}
						// single letter jungsung
						else {
							// console.log("* single jungsung");
							if(StringUtil.jungsung(inputLetter) != typingLetterJungsung)
								highlightKey = "Backspace";
							else {
								if(StringUtil.hasJongsung(typingLetter)) {
									// highlightKey = StringUtil.jongsung(typingLetter);
									var typingLetterJongsung = StringUtil.jongsung(typingLetter);
									if(StringUtil.isMultiAlphabetJongsung(typingLetterJongsung)) {
										var jongsungAlphabets = StringUtil.getMultiAlphabetFromJongsung(StringUtil.jongsung(inputLetter));
										if(StringUtil.jongsung(typingLetter) == jongsungAlphabets[0] && StringUtil.chosung(typingNextLetter) == jongsungAlphabets[1])
											highlightKey = StringUtil.jungsung(typingNextLetter);
										else if(StringUtil.jongsung(inputLetter) == jongsungAlphabets[0])
											highlightKey = jongsungAlphabets[1];
										else
											highlightKey = "Backspace";
									}
								} else
									highlightKey = StringUtil.chosung(typingNextLetter);
							}
						}
					}
				}

				// after typing jongsung
				else if(inputLetterAlphabets.length == 3) {
					// inputLetter chosung != typingLetter chosung
					if(StringUtil.chosung(inputLetter) != StringUtil.chosung(typingLetter))
						highlightKey = "Backspace";
					// inputLetter jungsung != typingLetter jungsung
					else if(StringUtil.jungsung(inputLetter) != StringUtil.jungsung(typingLetter))
						highlightKey = "Backspace";

					// 입력할 글자에 종성이 있을 경우
					else if(StringUtil.hasJongsung(typingLetter)) {
						// console.log("has jongsung");
						var typingLetterJongsung = StringUtil.jongsung(typingLetter);

						// 입력한 글자의 종성과 입력할 글자의 종성이 같은 경우
						if(StringUtil.jongsung(inputLetter) == typingLetterJongsung) {
							if(inputIndex == typingLength - 1)
								highlightKey = "Enter";
							else
								highlightKey = StringUtil.chosung(typingNextLetter);
						}
						// 입력한 글자의 종성이 입력할 글자와 다를 경우
						else {
							if(inputIndex == typingLength - 1)
								highlightKey = "Backspace";
							// 입력할 글자의 종성이 합성자음일 경우
							else if(StringUtil.isMultiAlphabetJongsung(typingLetterJongsung)) {
								var inputJongsungAlphabets = StringUtil.getMultiAlphabetFromJongsung(StringUtil.jongsung(inputLetter));
								var typingJongsungAlphabets = StringUtil.getMultiAlphabetFromJongsung(StringUtil.jongsung(typingLetter));

								// 입력할 글자의 종성과 입력한 글자의 종성이 같은 경우
								if(typingLetterJongsung == StringUtil.jongsung(inputLetter))
									highlightKey = StringUtil.chosung(typingNextLetter);
								else if(StringUtil.jongsung(inputLetter) == typingJongsungAlphabets[0])
									highlightKey = typingJongsungAlphabets[1];
								else
									highlightKey = "Backspace";
							}
							// 입력할 글자의 종성이 단자음일 경우 : 알바 -> 앏
							else {
								var inputLetterJongsung = StringUtil.jongsung(inputLetter);
								// console.log("*** single jongsung");
								// console.log("inputLetter : " + inputLetter);
								// console.log("StringUtil.jongsung(inputLetter) : " + StringUtil.jongsung(inputLetter));
								// console.log("inputLetterJongsung : " + inputLetterJongsung);

								if(StringUtil.isMultiAlphabetJongsung(inputLetterJongsung)) {
									var inputJongsungAlphabets = StringUtil.getMultiAlphabetFromJongsung(StringUtil.jongsung(inputLetter));
									// console.log("inputJongsungAlphabets : " + inputJongsungAlphabets);
									if(StringUtil.jongsung(typingLetter) == inputJongsungAlphabets[0] && StringUtil.chosung(typingNextLetter) == inputJongsungAlphabets[1])
										highlightKey = StringUtil.jungsung(typingNextLetter);
									else
										highlightKey = "Backspace";
								}
								else {
									highlightKey = StringUtil.chosung(typingNextLetter);
								}
							}
						}
					}
					// 입력할 글자에 종성이 없을 경우
					else {
						// console.log("inputLetter jongsung : " + StringUtil.jongsung(inputLetter))
						// console.log("typingNextLetter chosung : " + StringUtil.chosung(typingNextLetter))
						if(StringUtil.hasJongsung(inputLetter)) {
							// console.log("### has jongsung");
							if(inputIndex == typingLength - 1)
								highlightKey = "Backspace";
							else {
								if(StringUtil.jongsung(inputLetter) == StringUtil.chosung(typingNextLetter)) {
									// highlightKey = StringUtil.jungsung(typingNextLetter);
									if(StringUtil.isMultiAlphabetJungsung(StringUtil.jungsung(typingNextLetter))) {
										// console.log("* multi jungsung");
										jungsungAlphabets = StringUtil.getMultiAlphabetFromJungsung(StringUtil.jungsung(typingNextLetter));
										highlightKey = jungsungAlphabets[0];
										// console.log(highlightKey);
									} else {
										// console.log("* single jungsung");
										highlightKey = StringUtil.jungsung(typingNextLetter);
										// console.log(highlightKey);
									}
								}
								else {
									highlightKey = "Backspace";
								}
							}
						}
						else
							highlightKey = "Backspace";
					}
				}
			}
		}

		if(highlightKey == " ")
			highlightKey = "Space";

		return highlightKey;
	},

	hideHighlightKey: function(highlightKey) {
		// var prevText = this.typingRandomContents[this.typingIndex];
		if(highlightKey === undefined)
			return;

		this.keyboard.hideHighlight(highlightKey);
	},

	showHighlightKey: function(highlightKey) {
		// var typingText = this.typingRandomContents[this.typingIndex];
		if(highlightKey === undefined)
			return;

		this.keyboard.showHighlight(highlightKey, this.leftHand, this.rightHand);
	},

	moveHands: function(highlightKey) {
		// var typingText = this.typingRandomContents[this.typingIndex];
		if(highlightKey === undefined)
			return;

		var key = this.keyboard.getKeyOfText(highlightKey);
		var handSide = key.handSide;
		if(handSide === KeyButton.LEFT_HAND) {
			this.leftHand.moveTo(key);

			if(this.keyMapper.isShiftText(highlightKey)) {
				var shiftRightKey = this.keyboard.getKey("ShiftRight");
				shiftRightKey.showHighlight();
				this.rightHand.moveTo(shiftRightKey);
			} else {
				this.rightHand.moveToDefaultPosition();
			}
		} else {
			this.rightHand.moveTo(key);

			if(this.keyMapper.isShiftText(highlightKey)) {
				var shiftLeftKey = this.keyboard.getKey("ShiftLeft");
				shiftLeftKey.showHighlight();
				this.leftHand.moveTo(shiftLeftKey);
			} else {
				this.leftHand.moveToDefaultPosition();
			}
		}
	},

	calculateTypingRecord: function(typingContent) {
		// console.log('입력 단어 : ' + this.typingRandomContents[this.typingIndex]);

		this.isTyping = false;
		this.timeTypingEnd = game.time.elapsedSince(0);
		// console.log('this.timeTypingEnd : ' + this.timeTypingEnd);

		var elapsedTimeMS = this.timeTypingEnd - this.timeTypingStart;
		// console.log('elapsedTimeMS : ' + elapsedTimeMS);

		var typingContentLength = StringUtil.getUnicodeAlphabetCount(typingContent);
		// console.log('typingContentLength : ' + typingContentLength);
		var typingContentPlusEnterCount = typingContentLength + TypingTest.TYPING_COUNT_PLUS_ENTER;

		var typingRecord = (typingContentPlusEnterCount + TypingTest.TYPING_COUNT_PLUS_ENTER) * 60000 / elapsedTimeMS;

		this.typingElapsedTime[this.typingIndex] = elapsedTimeMS;
		this.typingLetterCountTotal += typingContentPlusEnterCount;
		this.typingRecordForLines[this.typingIndex] = typingRecord;
		// console.log(this.typingRecordForLines[this.typingIndex]);

		var typingElapsedTimeTotal = 0;
		for(var i = 0; i <= this.typingIndex; i++) {
			// console.log('i : ' + i + ' - time : ' + this.typingElapsedTime[i]);
			typingElapsedTimeTotal += this.typingElapsedTime[i];
		}
		this.typingRecordTotal = this.typingLetterCountTotal * 60000 / typingElapsedTimeTotal;
		// console.log(this.typingElapsedTime);
		// console.log(this.typingLetterCountTotal);
		// console.log(typingElapsedTimeTotal);
		// console.log(typingRecordTotal);

		var typingRecordTotalInteger = Math.floor(this.typingRecordTotal);
		this.averageTypingSpeedText.print(typingRecordTotalInteger);
		// console.log('-------------- total --------------');
		// console.log('typingLetterCountTotal : ' + this.typingLetterCountTotal);
		// console.log('typingElapsedTimeTotal : ' + typingElapsedTimeTotal);
		// console.log('typingRecordTotal : ' + this.typingRecordTotal);
		// console.log('-----------------------------------');

		var animalLevelID = Animal.animalLevelIDByRecord(typingRecordTotalInteger, Animal.TYPE_TEST);
		this.animalList.activate(animalLevelID);
		this.animalList.tweenAnimation(Animal.ANIMATION_TYPE_CHANGE);
	},

	playNextContent: function() {
		this.typingIndex++;
		this.showTypingTestContents();
		this.showPlayingWordNumber();
		this.resetTypingContent();
		this.updateKeyboard();
	},

	showPlayingWordNumber: function() {
		this.contentProgressText.print(this.typingIndex + 1, this.typingContentLength);
	},

	setTimeTypingStart: function() {
		if(this.inputTextContent.canvasInput.value() == "")
			return;

		this.timeTypingStart = game.time.elapsedSince(0);
		var timeGap = this.timeTypingStart - this.timeTypingEnd;

		if(timeGap < 50)
			return;

		this.isTyping = true;
		// console.log('last End : ' + this.timeTypingEnd);
		// console.log('this.timeTypingStart : ' + this.timeTypingStart);

		// console.log('time gap : ' + (this.timeTypingStart - this.timeTypingEnd));
	}

}


TypingTest.TYPING_CONTENT_PREVIEW_COUNT	= 1;	// 3;
TypingTest.TYPING_CONTENT_DONE_COUNT	= 1;	// 3;
TypingTest.TYPING_COUNT_PLUS_ENTER		= 1;

TypingTest.OFFSET_RIGHT_ALIGN			= 10;

TypingTest.WORD_COUNT_FOR_STAGE			= 10;

TypingTest.COLOR_CONTENT_INPUT			= "#dddddd";
TypingTest.COLOR_CONTENT_RIGHT			= "#ffff4d";