ObjectController = Class.create(Controller, {
    prepareHTML: function(){
        pretty_form();
        if (!this.params.prefix) this.params.prefix = 'fs';
        
        if (this.params.prefix == 'comment') {
            this.form = new FormSender({
            url: this.params.form_url,
            name: this.params.prefix,
            before_send: this.beforeSave.bind(this),
            success: this.params.success
            });
        }        
        else {
            this.form = new FormSender({
            url: this.params.form_url,
            name: this.params.prefix,
            before_send: this.beforeSave.bind(this),
            success: this.onSave.bind(this)
            });          
        }
       
        if (this.form.form && this.form.form['idn']) {
            this.form_id = this.form.form['idn'].value.split('#')[1];
        }
      
        _$(this.params.prefix + '__delete').observe('click', this.deleteElement.bind(this));

        if (this.params.prepareForm) {
            this.params.prepareForm.bind(this)();
        };
    },
    
    save: function() {
        this.form.send();  
    },
    
    beforeSave: function() {
        this.form.form.select('.html').each(function(el) {
            HTMLEditor.updateValue(el);           
        });
    },
    
    onSave: function(response) {
        if (this.form_id == 0) {
            this.form_id = response.id;
            var new_id = this.form.form['idn'].value.split('#')[0] + '#' + response.id;
            $(this.params.prefix + '__form').select('[name="idn"]').invoke('writeAttribute', 'value', new_id);
            notify(this, 'new');
        }
        else {
            notify(this, 'save');
        }
    },
    
    deleteElement: function() {
       request(this.params.form_url, {'idn': this.form.form['idn'].value, 'delete': true}, this.onDelete.bind(this), true);
    },  
    
    onDelete: function(result) {
        this.params.container.update(result.message);
        notify(this, 'delete');
    }
});
