InlineEdit = function(prefix, url) {
    this.prefix = prefix;
    this.url = url;
    $$('[id^="' + prefix + '_a__"]').invoke('observe', 'click', this.onEdit.bind(this));
}

InlineEdit.prototype.onEdit = function(e) {
    this.id = e.element().id.substr(this.prefix.length + '_a__'.length);
    
    this.editor = $(this.prefix + '_a__'+ this.id);
    this.input = $(this.prefix + '_input__'+ this.id);
    
    this.editor.addClassName('hide');
    this.input.removeClassName('hide');
    
    this.input.value = this.editor.innerHTML;            
    this.input.focus();
    
    this.input.observe('keypress', this.onEvent.bind(this));
    this.input.observe('blur', this.onEnter.bind(this)); 
}

InlineEdit.prototype.onEvent = function(e) {
    var code;
    if (!e) e = window.event;    
    if (e.keyCode) code = e.keyCode;
        else if (e.which) code = e.which;     
    if (code == 13) e.element().blur();
}

InlineEdit.prototype.onEnter = function(e) {
    this.input.addClassName('hide');
    this.editor.removeClassName('hide');
    
    if(this.editor.innerHTML != this.input.value){
        this.editor.innerHTML = this.input.value;
        
        request(this.url, {id:this.id, name:this.input.name, value:this.input.value});
    }
}
