/*
Charset: UTF-8（京）
Version: 20090720
Programing by Masao Takahashi.
*/
function createAjaxNode(html)
{
	var node = document.createElement("div");
	node.style.position = "absolute";
	node.style.left = node.style.top = "0px";
	node.style.visibility = "hidden";
	node.innerHTML = html;
	return document.body.appendChild(node);
}
function HangulTranslation() {
	this.ajax = new MyAjaxObject();
}
HangulTranslation.prototype = {
onLoad : function()
{
	g_hangul.nForm = document.getElementById("js-translation");
	if (!g_hangul.nForm) {
		return;
	}
	g_hangul.nForm.onreset = g_hangul.doResetForm;
	g_hangul.nLoading = document.getElementById("js-loading");
	g_hangul.nHangul1 = document.getElementById("js-text-kr1");
	g_hangul.nHangul2 = document.getElementById("js-text-kr2");
	g_hangul.nHangul3 = document.getElementById("js-text-kr3");
	g_hangul.nHangul4 = document.getElementById("js-text-kr4");
	if (document.getElementById("js-keyboard")) {
		g_dlgMgr.registerButton("js-keyboard", "js-dialog-pos", 10, -50);
	}
	var list = getElementsByClass("js-ruby");
	for (var i=0; i<j; i++) {
		list[i].onclick = g_hangul.doRuby;
	}
	g_hangul.doRuby();//for firefox
},
doResetForm : function(e)
{
	g_hangul.nLoading.style.visibility = "hidden";
	g_hangul.nHangul2.style.display = "none";
	g_hangul.nHangul1.style.display = "block";
	g_hangul.nHangul1.value = "";
	g_hangul.nHangul1.readOnly = "";
	if (g_hangul.nHangul3) {
		g_hangul.nHangul4.style.display = "none";
		g_hangul.nHangul3.style.display = "block";
		g_hangul.nHangul3.value = "";
		g_hangul.nHangul3.readOnly = "";
	}
	var node = document.getElementById("js-text-jp");
	if (node) node.value = "";
	var node = document.getElementById("js-text-jp2");
	if (node) node.value = "";

	var list = getElementsByClass("js-ruby");
	list[0].checked = "checked";
	return false;
},
doRuby : function(e)
{
	g_hangul.changeText(0);
	if (g_hangul.nHangul3) g_hangul.changeText(1);
	return true;
},
getRubyValue : function()
{
	var list = getElementsByClass("js-ruby");
	for (var i=0; i<j; i++) {
		if (list[i].checked) {
			return list[i].value;
		}
	}
	return "";
},
transferHangul : function(hangul)
{
	g_hangul.nHangul1.value += hangul;
	g_hangul.changeText(0);
},
changeText : function(param)
{
	var ruby = g_hangul.getRubyValue();
	if (ruby) {
		var node = param ? g_hangul.nHangul3 : g_hangul.nHangul1;
		g_hangul.nLoading.style.visibility = "visible";
		node.readOnly = "readOnly";
		var url = "ajax_translation.php";
		var postdata = new Array();
		postdata['ruby'] = ruby;
		postdata['hangul'] = encodeURIComponent(node.value);
		g_hangul.ajax.send(url, postdata, g_hangul.changeText_cb, param);
	}
	else {
		g_hangul.nLoading.style.visibility = "hidden";
		var node1 = param ? g_hangul.nHangul3 : g_hangul.nHangul1;
		var node2 = param ? g_hangul.nHangul4 : g_hangul.nHangul2;
		if (node2) {
			node2.style.display = "none";
		}
		if (node1) {
			node1.style.display = "block";
			node1.readOnly = "";
		}
	}
},
changeText_cb : function(httpReq, postdata, param)
{
	var node = createAjaxNode(httpReq.responseText);
	var arr = getElementsByClass("ajax-split",node);
	var html = arr[0].innerHTML;
	document.body.removeChild(node);

	g_hangul.nLoading.style.visibility = "hidden";
	if (param) {
		g_hangul.nHangul4.innerHTML = html;
		g_hangul.nHangul3.style.display = "none";
		g_hangul.nHangul4.style.display = "block";
	}
	else {
		g_hangul.nHangul2.innerHTML = html;
		g_hangul.nHangul1.style.display = "none";
		g_hangul.nHangul2.style.display = "block";
	}
}
};
var g_hangul = new HangulTranslation();
g_common.registInterface(g_hangul);
