/**
 * @overview CommentManager.js
 * A collection of components for sending comments on content
 * @author Patrick Paul-Hus
 */


/**
 * BQ.widget.CommentForm
 * This is the actual form for commenting on content.
 * @author Patrick Paul-Hus
 */
BQ.widget.CommentForm = Ext.extend(Ext.form.FormPanel, {

    controller: '/json/BQAContent',
    actions: {
        add: 'addComment'
    },

    labelAlign:'top',
    border:false,
    buttonAlign:'left',
    /**
     * @cfg contentId {integer} the id of the content to comment on
     */
    contentId: null,

    /**
     * Constructor
     */
    initComponent: function() {
        /**
         * the 
         */
        this.items = [{
            xtype: 'textarea',
            name: 'text',
            fieldLabel: 'Commentaire',
            allowBlank: false,
            msgTarget: 'under',
            width:546,
            height:150,
            validateOnBlur: false,
            validationEvent:false,
            style:'padding:20px 5px 5px 5px',
            maxLength:1024,
            maxLengthText: App.t('comment_too_long'),
            grow:false
            //growMin:100,
            //growMax:300
        }];

        this.buttons = [{
            text: App.t('send'),
            handler: this.onSend,
            scope: this
        }];

        /**
         * beforeaction
         * put up a message box before submitting the form informing the user things are happening
         * @param {Ext.form} form
         * @param {Ext.form.Action} action
         */
        this.on('beforeaction', function(form, action) {
            Ext.MessageBox.wait(App.t('please_wait'), App.t('comment_title'));
        }, this);

        /**
         * actioncomplete
         * the form has been submitted and this is the good result
         * @param {Ext.form} form
         * @param {Ext.form.Action} action
         */
        this.on('actioncomplete', function(form, action) {
            Ext.MessageBox.hide();
            App.setAlert(App.STATUS_OK, App.t('comment_success'));
            this.form.reset();
        }, this);

        /**
         * actionfailed
         * something bad happened to the form submit and we should deal with it in a nice way
         * @param {Ext.form} form
         * @param {Ext.form.Action} action
         */
        this.on('actionfailed', function(form, action) {
            Ext.MessageBox.hide();
        }, this);

        BQ.widget.CommentForm.superclass.initComponent.call(this);
    },


    /**
     * This function is called when you press the send button.
     * It sends the comment.
     */
    onSend: function(btn, ev) {
        form = this.form;
        if (!form.isValid()) {
            return;
        }
        form.submit({
            url: this.controller + '/' + this.actions.add,
            method: 'POST',
            params: {
                id: this.contentId
            },
            scope: this
        });
    }
});