
var timer;
var createInlineTextInput = function(id, text) {
	var lastCode = null;
	var shiftKey = false;
	var bbody = document.body;
	if (bbody == null) return;
	var container = document.createElement("div");
	container.setAttribute("id", id + "_container");
	container.style.position = "absolute";
	container.style.zIndex = 10000;
	// フォーカスがブラウザに取られてしまうのを防ぐためにダミーのInputTextを作成
	var dtextInput = document.createElement("input");
	dtextInput.setAttribute("type", "text");
	dtextInput.style.position = "absolute";
	dtextInput.style.top = -10000 + "px";
	dtextInput.style.left = 0 + "px";
	container.insertBefore(dtextInput, container.firstChild);
	var textInput = document.createElement("input");
	textInput.setAttribute("id", id + "_textInput");
	textInput.setAttribute("type", "text");
	textInput.value = text;
	container.insertBefore(textInput, container.firstChild);
	
	
	Event.observe(textInput, 'focus', function() {
		clearInterval(timer);
	});
	
	Event.observe(textInput, 'blur', function() {
		Element.remove(container);
		$("main").focus();
		if (lastCode == 9) {
			if (!shiftKey) {
				$("main")['inlineTextInputTab' + id](textInput.value, "forward");
			} else {
				$("main")['inlineTextInputTab' + id](textInput.value, "backward");
			}
		} else {
			$("main")['inlineTextInputBlur' + id](textInput.value);
		}
	});
	
	Event.observe(textInput, 'keydown', function(event) {
		event = event || window.event;
		lastCode = event.keyCode || event.which;
		shiftKey = event.shiftKey;
	});
	
	Event.observe(textInput, 'keyup', function() {
		// こないかもしれない・・・
		lastCode = null;
	});
	
	Event.observe(textInput, 'change', function() {
		$("main")['inlineTextInputChange' + id](textInput.value);
	});
	
	bbody.insertBefore(container, bbody.lastChild);
	
	timer = setInterval(function(){
		if (textInput) {
			textInput.focus();
		}
	}, 100); 
	
};

var updateInlineTextInputBounds = function (id, x, y, w, h) {
	var container = $(id + "_container");
	var textInput = $(id + "_textInput");
	container.style.top = y + "px";
	container.style.left = x + "px";
	textInput.style.width = w + "px";
	textInput.style.height = h + "px";
}
