/**
 * @overview Send2Friend.js
 * A collection of components for sending a page to a friend.
 * @author Patrick Paul-Hus
 */

/**
 * BQ.widget.Send2Friend
 * A component for sending a page to a friend.
 * @author Patrick Paul-Hus
 */
BQ.widget.Send2Friend = Ext.extend(Ext.Window, {

  controller: '/json/BQAUser',
  actions: {
    send: 'sendToFriend'
  },
  frame: false,
  center: true,
  buttonAlign: 'right',
  modal: false,
  autoHeight: true,
  closeAction:'hide',
  draggable: false,
  resizable:false,
  /**
  * @cfg {string} btnId
  * the dom id of the button that will show the send to a friend form
  */
  btnId: null,
  /**
  * @cfg {integer} contentId
  * the id of the content that you want to send to a friend
  */
  contentId: null,

    initComponent: function() {
        this.items = this.build();
        var btn = Ext.get(this.btnId);
        if (btn && this.btnId != null) {
          btn.on('click', function(ev) {
            this.getForm().form.reset();
            this.show();
            this.el.alignTo(btn, "tr-br");
            ev.stopEvent();
          }, this);
        }
        else {
          throw new RExt.Error("The send to a friend button could not be found.");
        }
        BQ.widget.Send2Friend.superclass.initComponent.call(this);
    },

    /**
     * build
     * returns Ext.form.FormPanel, with all the needed form fields and validation
     */    
    build: function() {
        return {
            xtype: 'form',
            labelAlign: 'top',
            buttonAlign:'left',
            border: false,
            bodyStyle: 'padding: 8px',
            defaults: {
                anchor: '100%'
            },
            items: [{
                xtype: "textfield",
                name: "from",
                fieldLabel: App.t('sendtofriend_name'),
                allowBlank: false,
                msgTarget: 'under',
                validationEvent: 'blur'
            },{
                xtype: "textfield",
                vtype: "bqemail",
                name: "fromEmail",
                fieldLabel: App.t('sendtofriend_from_email'),
                allowBlank: false,
                msgTarget: 'under',
                validationEvent: 'blur'
            }, {
                xtype: "textfield",
                vtype: "bqemaillist",
                name: "email",
                fieldLabel: App.t('sendtofriend_email'),
                allowBlank: false,
                msgTarget: 'under',
                validationEvent: 'blur'
            }, {
                xtype: "textarea",
                name: "message",
                fieldLabel: App.t('sendtofriend_message'),
                allowBlank: true,
                msgTarget: 'under',
                validationEvent: 'blur'
            }],
            buttons: [{
                text: App.t("send"),
                handler: this.onSend,
                scope: this
            },{
                text: App.t("cancel"),
                handler: function() {
                    this.hide()
                },
                scope: this
            }],
            listeners: {
                // before we submit the form, present the User with a Message
                beforeaction: function(form, action) {
                    Ext.MessageBox.wait(BQJSLabels.sendtofriend_wait_message, BQJSLabels.sendtofriend_wait_title);
                },
                // after the submission has occurred, inform the User of the OK status
                actioncomplete: function(form, action) {
                    Ext.MessageBox.hide();
                    App.setAlert(App.STATUS_OK, action.result.msg);
                    this.hide();
                },
                // if something went terribly wrong, we inform the User
                actionfailed: function(form, action) {
                    Ext.MessageBox.hide();
                    App.setAlert(App.STATUS_ERROR, action.result.msg);
                    this.hide();
                },
                scope: this
            }
        };
    },

    /**
     * getForm
     * a convenience method providing the FormPanel to the Window
     */
    getForm: function() {
        return this.items.first();
    },

    /**
     * onSend
     * Event handler when the User presses the send button
     * @param {Ext.Button} btn
     * @param {Ext.EventObject} ev
     */
    onSend: function(btn, ev) {
        var form = this.getForm().form;
        if (!form.isValid()) {
            return App.setAlert(App.STATUS_ERROR, App.t("phrase_fill_required_fields"));
        }
        form.submit({
            url: this.controller + '/' + this.actions.send,
            method: 'POST',
            params: {
                contentId: this.contentId
            },
            scope: this
        });
    }
});