/* inlineEdit 사용법
<script type="text/javascript" src="InlineEditor.js"></script>
<script type="text/javascript">
var editor = createEditor(callback);

function callback(elementByCursor){ }
</script>
*/

function createEditor(callback){
	var editor = new Editor();
	editor.callback = callback;
	try{
	   document.addEventListener('click',clickEventHandler,false);
	} catch(e) {
	   document.attachEvent('onclick',clickEventHandler);
	}
	return editor;
}//

function clickEventHandler(evt){
	var evt = evt ? evt : ((window.event) ? window.event:null);
	var elementByCursor = evt.target ? evt.target:evt.srcElement;

	if(elementByCursor.getAttribute("editable"))
		editor.editing(elementByCursor);
}//

function Editor(){
	this.autosave = false;
	this.source = null;
	this.callback = null;
	this.container = null;
	this.frm = document.createElement('INPUT');
	this.frm.className = 'editor';
	this.frm.model = this;

	this.frm.onkeydown = function(evt){
		var evt = evt ? evt : ((window.event) ? window.event:null);
		if(evt.keyCode == 13){
			try{
				evt.preventDefault();
			} catch(ex) {
               evt.returnValue = false;
			}
			this.model.complete();
		}
	}

	this.frm.onblur = function(evt){
		if(this.model.autosave == true)
			this.model.complete();
		else
			this.model.cancel();
   }
}//

Editor.prototype.editing = function(elementByCursor){
	try{
		this.container = elementByCursor;

		if(!elementByCursor.hasChildNodes())
			elementByCursor.appendChild(document.createTextNode(''));

		this.source = elementByCursor.firstChild;
		this.frm.value = this.source.nodeValue;
		this.frm.style.width = '100%';

		if(this.container.style.textAlign != undefined && this.container.style.textAlign != '')
			this.frm.style.textAlign = this.container.style.textAlign;
        else if(this.container.align != undefined && this.container.align != '')
            this.frm.style.textAlign = this.container.align;
        else
            this.frm.style.textAlign = 'left';

		this.container.replaceChild(this.frm,this.source);
		this.frm.focus();
		this.frm.value += '';
   }catch(e){ }
}//

Editor.prototype.cancel = function(){
	this.finish();
}//

Editor.prototype.complete = function(){
	this.save();
	this.finish();
	if(this.callback instanceof Function)
		this.callback(this.container);
}//

Editor.prototype.finish = function(){
	this.container.replaceChild(this.source,this.frm);
}

Editor.prototype.save = function(){
	this.source = document.createTextNode(this.frm.value);
}
