var MessageComponent = klass.create();
Object.extend(MessageComponent.prototype, {
  initialize: function(username, options) {
    this.username = username;
    this.recipient = username;
    this.subject = '';
    if (options) this.subject = options.subject || '';

    this.overlay = new Overlay({backgroundColor: 'black',
                                opacityMax: 30,
                                color: 'white'});
    this.initializeComponents();
    this.overlay.fadeIn();
  },

  initializeComponents: function() {
    this.recipientText = $T('Sending a message to ' + this.username);
    this.overlay.container.appendChild(this.recipientText);
    this.overlay.container.appendChild($E('br'));

    this.overlay.container.appendChild($T('Subject:'));
    this.overlay.container.appendChild($E('br'));

    this.subjectInput = $E('input');
    this.subjectInput.setAttribute('type', 'edit');
    this.subjectInput.setAttribute('size', '43');
    this.subjectInput.setAttribute('value', this.subject);
    this.subjectInput.className = 'editinput';
    this.overlay.container.appendChild(this.subjectInput);
    this.overlay.container.appendChild($E('br'));

    this.overlay.container.appendChild($T('Message:'));
    this.overlay.container.appendChild($E('br'));

    this.messageBox = $E('textarea');
    this.messageBox.style.border = '1px solid black';
    this.messageBox.setAttribute('cols', '43');
    this.messageBox.setAttribute('rows', '6');
    this.messageBox.style.fontSize = '9pt';
    this.messageBox.style.fontFamily = 'Arial,Helvetica,sans-serif';
    this.overlay.container.appendChild(this.messageBox);
    this.overlay.container.appendChild($E('br'));

    this.sendButton = $E('input');
    this.sendButton.setAttribute('type', 'button');
    this.sendButton.setAttribute('value', 'Send');
    this.sendButton.className = 'smallbutton'
    this.overlay.container.appendChild(this.sendButton);

    this.closeButton = $E('input');
    this.closeButton.setAttribute('type', 'button');
    this.closeButton.setAttribute('value', 'Cancel');
    this.closeButton.className = 'smallbutton'
    this.overlay.container.appendChild(this.closeButton);

    this.overlay.container.style.padding = '15px';

    Event.observe(this.closeButton, 'click',
                  this.cancel.bind(this));
    Event.observe(this.sendButton, 'click',
                  this.send.bind(this));
  },

  destroy: function() {
    this.overlay.hide();
    this.overlay.destroy();
    Event.stopObserving(this.sendButton);
    Event.stopObserving(this.closeButton);
  },

  cancel: function(evt) {
    this.destroy();
  },

  sendMessageResult: function(req, json, extra) {
    if (req.status != 200) return;
    var response = xrpcBind(req.responseXML);
    if (!response.ok) {
      alert('Could not send message');
    }
    this.destroy();
  },

  sendMessage: function() {
    Global.call(this.sendMessageResult.bind(this),
                'sendMessage', this.recipient, this.subject, this.message);
  },

  send: function(evt) {
    if (this.subjectInput.value == '') {
      alert('Subject required');
      return;
    }
    this.subject = this.subjectInput.value;

    if (this.messageBox.value == '') {
      alert('Please write a short message');
      return;
    }
    this.message = this.messageBox.value;
    this.sendMessage();
  }
});
// vim: set sts=2 sw=2 expandtab:
