Ext.ns('Ext.pv.inbox');
Ext.pv.inbox.InboxOptionsFormDialog = Ext.extend(Ext.Window, {
	width: 475,
	height: 150,
	modal: true,
	layout: 'fit',
	resizable: false,
	
	// Localized strings
	title: 'Inbox Options',
	okButtonLabel : 'OK',
	cancelButtonLabel : 'Cancel',
	alertTitle : 'Message',
	failureMessage : 'Input is invalid.',
	
	direction : 'ltr',
	contextPath : null,
    personId : 0,
    record : null,
    isInboxManagerEnabled : false,


    /**
     * initComponent
     * @protected
     */
    initComponent : function() {
	
		if (this.direction=='rtl') {
			this.style = 'direction:rtl';
		}
	
		// Initialize the form fields
        this.form = new Ext.pv.inbox.InboxOptionsForm({
        	contextPath : this.contextPath,
        	personId: this.personId,
        	record: this.record,
        	isInboxManagerEnabled : this.isInboxManagerEnabled
        });
        this.items = this.form;
        
        this.buttons = new Array();
        this.buttons[this.buttons.length] = {
        	text: this.okButtonLabel,
        	handler: this.onOk,
        	scope: this
        };
        this.buttons[this.buttons.length] = {
        	text: this.cancelButtonLabel,
        	handler: this.onCancel,
        	scope: this
        };
        
        this.on(
        	'show',
        	function() {
        		// Initialize form data
        		this.form.load(this.personId);
        	},
        	this
        );
        
        this.addEvents({
            /**
             * @event saved
             * Fires when a record is successfully saved
             * @param {Record} the forms record object
             */
            saved : true
        });
        
        // super
        Ext.pv.inbox.InboxOptionsFormDialog.superclass.initComponent.call(this);
    },
    
    onOk : function() {
    	this.saveRecord();
    },
    
    onCancel : function() {
    	this.close();
    },
    
    /**
     * saveRecord
     */
    saveRecord : function() {
    	
        if (!this.form.getForm().isValid()) {
        	Ext.Msg.alert(this.alertTitle,this.failureMessage); 
            return false;
        }

        var myThis = this;
        this.form.getForm().submit({
            clientValidation: true,
            url: this.contextPath + '/siteAdmin/inbox/updateSiteOptionsJSON.inboxAction',
            success: function(form, action) {
	        	myThis.fireEvent('saved');
	        	myThis.close();
            },
            failure: function(form, action) {
            	Ext.Msg.alert(myThis.alertTitle,action.result.error);
            }
        });
    }

});
