ShareForm = Class.create({
  element: null,

  initialize: function(element) {
    this.element = $(element);
    if (!this.element) return;

    this._wrapper = this.element.down('.form');
    this._form = this.element.down('form');
    this._message = this.element.down('.message');

    this.element.down('.cancel').observe('click', function(event) {event.stop(); Event.element(event).blur(); this.hide();}.bind(this));
    this.element.down('.send').observe('click', function(event) {event.stop(); Event.element(event).blur(); this.submit();}.bind(this));

    $$('a.share').each(function(anchor) {
      anchor.observe('click', function(event) {
        event.stop();
        this.toggle();
      }.bind(this));
    }.bind(this));
  },

  toggle: function() {
    return (this._visible) ? this.hide() : this.show();
  },

  show: function() {
    if (this._displayingMessage) {
      new Effect.Parallel([
        new Effect.BlindDown(this._wrapper),
        new Effect.BlindUp(this._message)
        ], {
        duration: .25
      });
    } else {
      new Effect.BlindDown(this.element, {
        duration: .15,
        transition: Effect.Transitions.easeFrom,
        afterFinish: function() {
          this._visible = true;
        }.bind(this)
      });
    }
  },

  hide: function() {
    new Effect.BlindUp(this.element, {
      duration: .15,
      transition: Effect.Transitions.easeFrom,
      afterFinish: function() {
        this._visible = false;
        this._displayingMessage = false;
        this._wrapper.show();
        this._message.hide();
      }.bind(this)
    });
  },

  submit: function() {
    this._form.select('.helper').each(function(helper) { if (helper.value == helper.title) helper.value = ''; });
    this._form.select('.invalid').each(function(invalid) { invalid.removeClassName('invalid'); });
    this._form.down('#share_url').value = window.location.href;
    new Ajax.Request(this._form.action + '&ajax=true', {
      method: 'post',
      parameters: this._form.serialize(true),
      onSuccess: function(transport) {
        var response = transport.responseJSON;
        if (!response) return;
        
        if (response['success']) {
          var anchor = new Element('a', {href: '#'}).update('Share Again');
          anchor.observe('click', function(event) {
            event.stop();
            this.show();
          }.bindAsEventListener(this));
          this._message.innerHTML = response['message'] + '<br/><br/>';
          this._message.appendChild(anchor);
          this._displayingMessage = true;
          new Effect.Parallel([
            new Effect.BlindUp(this._wrapper),
            new Effect.BlindDown(this._message)
            ], {
            duration: .25,
            afterFinish: function() {
              var to_email_address = this._form.down('#to_email_address');
              to_email_address.value = to_email_address.title;
              this._form.down('#message').value = '';
              this._form.down('#message_counter').innerHTML = '';
            }.bind(this)
          });
        } else {
          if (response['invalidFields']) {
            $H(response['invalidFields']).each(function(field) {
              var element = this._form.down('#' + field[0]);
              element.addClassName('invalid');
              if (element.hasClassName('helper') && element.value == '') {
                element.value = element.title;
              }
            }.bind(this));
          }
        }
      }.bind(this)
    });
  }
});
