var AjaxForm = Class.create({
  initialize: function(container, form_container)
  {
    // alert('AjaxForm container=' + container + ' form_container=' + form_container);
    this.container = $(container);
    // console.log('AjaxForm container=' + container);
    // console.log('AjaxForm form_container=' + form_container);
    if (form_container) {      
      this.form_container = $(form_container);
      // console.log('AjaxForm this.form_container=' + this.form_container);
    }
    // alert('AjaxForm formHookup');
    this.formHookup();
  },
  
  formHookup: function()
  {
    // alert('AjaxForm formHookup entered=' + this.form_container);
    // console.log('AjaxForm formHookup entered=' + this.form_container);
    if (this.form_container) {
      this.form = this.form_container.down('form');
      // alert('AjaxForm (form_container) this.form.id=' + this.form.id);
      // console.log('AjaxForm (form_container) this.form.id=' + this.form.id);
    } else if (this.container && this.container.down('form')) {      
      this.form = this.container.down('form');
      // alert('AjaxForm this.form.id=' + this.form.id);
      // console.log('AjaxForm this.form.id=' + this.form.id);
    } else {
      // alert('this.form cannot be found');
      // console.log('this.form cannot be found');
    }
    // console.log('before formErrors');
    // alert('before formErrors');
    if (this.container && this.container.down('#formErrors')) {      
      this.formErrors = this.container.down('#formErrors');
      // alert('AjaxForm container=' + this.container + ' formErrors=' + this.formErrors.innerHTML);
      // console.log('AjaxForm container=' + this.container + ' formErrors=' + this.formErrors.innerHTML);
    } else if (this.form_container && this.form_container.down('#formErrors')) {      
      this.formErrors = this.form_container.down('#formErrors');
      // alert('AjaxForm form_container=' + this.form_container + ' formErrors=' + this.formErrors.innerHTML);
      // console.log('AjaxForm form_container=' + this.form_container + ' formErrors=' + this.formErrors.innerHTML);
    }
    if (this.form) {
      this.form.focusFirstElement();
      this.form.stopObserving();
      this.form.observe('submit', this._onFormSubmit.bindAsEventListener(this));
      // console.log('AjaxForm after observe submit');
    }
    this.accept = 'text/javascript';
    // console.log('AjaxForm leaving formHookup');
    // alert('AjaxForm leaving formHookup');
  },
  
  _onFormSubmit: function(e)
  {
    Event.stop(e);   
    var options = {
        method     : this.form.readAttribute('method'),
        parameters : this.form.serialize(true),
        Accept     : this.accept,
        onSuccess  : this._onSaveSuccess.bind(this),
        onFailure  : this._onSaveFailure.bind(this)
    }
    new Ajax.Request(this.form.readAttribute('action'), options);
    if (this.form) {
      this.form.focusFirstElement();
    }
  },
  
  _onSaveSuccess: function(transport)
  {
    Lightview.hide();
  },

  _onSaveFailure: function(transport)
  {
    this.formErrors.update(transport.responseText);
    new Effect.Highlight(this.formErrors, { startcolor: '#ffff99', endcolor: '#ffffff' });
    this.formHookup();
  },
   
  _onRemove: function(e)
  {
    Event.stop(e);
    var response = confirm("Are you sure you want to delete this?"); 
    if (response) {
      var options = {
          method        : 'DELETE',
          // parameters    : '_method=delete',
          Accept        : 'text/javascript',
          onSuccess     : this._onRemoveSuccess.bind(this),
          onFailure     : this._onRemoveFailure.bind(this)
      }
      var ele = Event.element(e);
      new Ajax.Request(ele.href, options);        
    }
  },
  
  _onRemoveSuccess: function(transport) 
  {
    var data = transport.responseText.evalJSON();
    $(data.id).remove();         
  },
  
  _onRemoveFailure: function(transport)
  {
    var data = transport.responseText.evalJSON();
    alert("Removing item failed.\n\n" + data);
  }
});
