/*
//   Hangul IME lib by Colspan (Miyoshi)   //

・バージョン履歴
	Version 2.0 (2006/09/02)
		クラス化・ライブラリ化

	Version 1.4 (2006/05/21)
		ライブラリの汎用化
		Ctrl/Shiftキーの挙動を修正

	Version 1.03 (2006/02/07)
		古めのMozillaやFirefox 1.0.xxに対応

	Version 1.02 (2006/02/07)
		関係ないキーが化ける問題を修正

	Version 1.01 (2006/02/06)
		BS周りのバグ修正
		Firefox 1.5.xxに対応

	Version 1.00 (2006/02/06)
		IE6で実装完了
		Sigmarion3のIEで動作確認

・謝辞
	johab.jsを開発されたchiyuさんに深く感謝いたします。
*/

//無動作関数
function no_action(){}


var hangulIME = Class.create();
hangulIME.prototype = {
	baseDiv : null,
	baseForm : null,
	hangulBox : null,
	isEnabled : false,//現在の入力モードを記憶する
	eventChecker : false,
	tempText : "",

	initialize : function(id){
		this.baseDiv	= document.getElementById(id);
		var _this = this;

		var breakLine = document.createElement("br");

		//base form
		this.baseForm = document.createElement("form");
		this.baseForm.onsubmit = function(){//親のformをsubmitしない
			return false;
		}

		//hangul Box
		this.hangulBox = document.createElement("textarea");
//		this.hangulBox.id = "hangulbox";
//		this.hangulBox.value = "Type here.";
		this.hangulBox.style.width = "400px";
		this.hangulBox.style.height = "300px";
		this.hangulBox.onkeydown = function(event){//打鍵
			if( _this.isEnabled ){
				var status = _this.imeCombine(event);
				_this.eventChecker = status;
				return !status;
			}
			else return true;
		}
		this.hangulBox.onkeyup = function(event){//打鍵を放す
			if( _this.isEnabled ){
				return false;
			}
			else return true;
		}
		this.hangulBox.onkeypress = function(event){//打鍵(onkeydownと同じ値を返す)
			if( _this.isEnabled ){
				var returnValue = !_this.eventChecker;
				_this.eventChecker = false;
				return returnValue;
			}
			else return true;
		}
		this.hangulBox.onmousedown = function(){
			if( _this.isEnabled ){
				_this.imeAccept();
			}
		}
/*
		//mode
		var modeSelectorEng = document.createElement("input");
		modeSelectorEng.type = "radio";
		modeSelectorEng.name = "imemode";
		modeSelectorEng.value = "ascii";
		modeSelectorEng.checked = true;
		modeSelectorEng.onclick = function(){

		}

		var modeSelectorKor = document.createElement("input");
		modeSelectorKor.type = "radio";
		modeSelectorKor.name = "imemode";
		modeSelectorKor.value = "hangul";
*/
		//描画
		this.baseDiv.appendChild(this.hangulBox);
/*		this.baseForm.appendChild(breakLine);
		this.baseForm.innerHTML += "Mode : ";
		this.baseForm.appendChild( modeSelectorEng );
		this.baseForm.innerHTML += "English";
		this.baseForm.appendChild( modeSelectorKor );
		this.baseForm.innerHTML += "Korean";
		this.baseDiv.appendChild(this.baseForm);
*/
		//execute
//		this.hangulBox.focus();
		this.enableIME();

	},
	enableIME : function(){
		this.isEnabled = true;
		this.eventChecker = false; //keydownとkeypressが重複しないように
	},
	disableIME : function(){
		this.isEnabled = false;
		this.tempText = "";
		this.hangulBox.parentNode.imemode[0].checked = true;
		this.hangulBox.onkeydown = no_action;
		this.hangulBox.onclick = no_action;
		this.hangulBox.onkeypress = no_action;
		this.hangulBox.parentNode.onsubmit = no_action;
		this.hangulBox.focus();
	},
	imeCombine : function(eventObj){
		/* IME処理関数
			処理したらtrue
			処理せず無視したらfalse
		*/
		if( !this.isEnabled ) return false;	// IMEが無効になっている場合
		if( isMoz ) event = eventObj; // Mozilla系列の場合

		//入力制御
		var inputCode = event.keyCode;
		var inputChar = String.fromCharCode(inputCode);

		//extention 漢字変換など
	//	if( hangulIME_extension( event ) ) return false;

		//関係ないキーは除外
		if( event.keyCode == 8 ){//BS
			return this.imeBS();
		}
		else if( event.keyCode == 16 ){//Shift
			return false;
		}
		else if( event.keyCode == 17 ){//Ctrl
			return false;
		}
		else if( event.ctrlKey ){//Ctrlキーが押されているとき
			if( keyTable.is_alphabet( inputCode ) ){
				//特殊キーの組み合わせでは確定してスルー
				this.imeAccept();
				return false;
			}
		}
		else if( event.altKey ) return false;//altキーは無視
		else if( ! keyTable.is_alphabet(inputCode) ){//アルファベットではない
			this.imeAccept();
			return false;
		}

		//Shiftキーの処理
		if( event.shiftKey && (("abcdfghijklmnsuvxyz").search(inputChar.toLowerCase()) == -1) ) inputChar = inputChar.toUpperCase();
		else inputChar = inputChar.toLowerCase();

		//変換開始
		var mytext = this.tempText;
		var mykey = inputChar;
		if(this.tempText.length != 0 ) selecter_backspace(this.hangulBox);//作業文字列がからでないときには1文字消す
		lastChar = mytext.substring(mytext.length - 1, mytext.length);//結合対象文字列

		if(isJasoKey(mykey)){//作業文字列に入力文字を演算
			this.tempText = strPlusJasoKey(lastChar, mykey);
		}
		if(this.tempText.length == 2){//文字が確定した
			selecter_insert(this.hangulBox,this.tempText.substring(0, 1));//テキストボックスに確定文字を挿入
			this.tempText = this.tempText.substring(1,2);
		}
		selecter_insert(this.hangulBox,this.tempText);//テキストボックスに作業文字列を挿入
		return true;
	},
	imeBS : function(){
		var mystr = this.tempText;
		if(mystr.length!=0){
			this.tempText = strDeleteOneJaso(mystr);
			selecter_backspace(this.hangulBox);
			if( this.tempText.length != 0 ) selecter_insert(this.hangulBox,this.tempText);//テキストボックスに挿入
			return true;
		}
		else{
			return false;
	//		selecter_backspace(this.hangulBox);
		}
	},
	//処理文字列を確定する
	imeAccept :  function(){
		if(this.tempText.length != 0 ){
			selecter_backspace(this.hangulBox);
			selecter_insert(this.hangulBox,this.tempText.substring(0, 1));//テキストボックスに挿入
			this.tempText = "";
		}
	}
}


//テキストボックスを全部選択する
function hangulIME_selectAll(){
	imeAccept();
	this.hangulBox.focus();
	this.hangulBox.select();
}
//テキストボックスを消す
function hangulIME_clear() {
	this.tempText = ""
	this.hangulBox.focus();
	this.hangulBox.value = "";
}



//debug
function dump( dmpObj ){
	/*	event = dmpObj;
	document.write("<table border=1>");
	for(i in event){
		document.write("<tr><td>"+i+"</td><td>"+ event[i]+"</td><td>"+typeof(event[i])+"</td></tr>");
	}
	document.write("</table>");*/
	//	alert(event.toElement);
	var dumpStr = "<table border=1>";
	for(i in dmpObj){
		dumpStr+="<tr><td>"+i+"</td><td>"+ dmpObj[i]+"</td><td>"+typeof(dmpObj[i])+"</td></tr>";
	}
	dumpStr+="</table>";
	document.getElementById("debug").innerHTML = dumpStr;
}


