// Create user extension namespace (Application)
Ext.namespace('Application');

/**
  *  Application.guestLoginWindow Extension class
  * 
  *  @author Seabor
  *  @version 1.0
  *  
  *  @class Application.guestLoginWindow
  *  @extends Ext.Window
  *  @constructor
  *  @param {Object} config Configuration options
  */

Application.guestLoginWindow = function(config) {

    // call parent constructor
    Application.guestLoginWindow.superclass.constructor.call(this, config);

}; // end of Application.guestLoginWindow constructor

Ext.extend(Application.guestLoginWindow, Ext.Window, {

    // configurables
    // anything what is here can be configured from outside
    title: 'Вход'
    ,width: 240
    ,height: 150
    ,layout: 'fit'
    ,plain: true
    ,bodyStyle: 'padding:10px'
    ,buttonAlign: 'center'
    ,resizable: false
    ,draggable: false
    ,closable: true
    ,modal: true
    // initComponent {{{
    ,initComponent: function(){
    	var that = this;
        // Ext.apply {{{
    	Ext.apply(this, {
    		items: new Ext.FormPanel({
    		    labelWidth: 75
                ,baseCls: 'x-plain'
                ,defaults: {width: 100}
                ,defaultType: 'textfield'
                ,url:'/login'
                ,monitorValid:true
                ,keys: {
                    key: Ext.EventObject.ENTER
                    ,fn: function(){
                    	if (!that.submitBtn.disabled)
                            that.submitFn();
                    }
                }
                ,items: [{
                    fieldLabel: 'Логин'
                    ,id: 'loginField'
                    ,name: 'username'
                    ,allowBlank: false                                                                                       
                },{
                    fieldLabel: 'Пароль'
                    ,name: 'password'
                    ,inputType:'password'
                    ,allowBlank:false
                }]
    		})
    		,buttons: [{
    		    text:'Войти'
                ,disabled:true
                ,minWidth:70
                ,type: 'submit'
    		}]
    	}); // }}} Ext.apply
    	
    	// call parent
    	Application.guestLoginWindow.superclass.initComponent.apply(this, arguments);
    	
    	this.submitBtn = this.buttons[0];
    	this.loginForm = this.items.itemAt(0);
    	this.loginField = this.items.itemAt(0).items.itemAt(0);
    	
    	this.on({
    	   scope: this
    	   ,render: function() {
    	       this.submitBtn.on({
    	           scope: this
    	           ,click: function() {
    	               this.submitFn();
    	           }
    	       });
    	       this.loginForm.on({
    	           scope: this
    	           ,clientvalidation: function(x, y){
    	               if (y) {this.submitBtn.enable()}
    	               else {this.submitBtn.disable()}
    	           }
    	       });
    	       this.on({
    	           scope: this
    	           ,afterLayout: function() {
    	               this.loginField.focus(false, 600);
    	           }
    	       });
    	   }
    	});
    	
    }// }}} initComponent
   /**
     * loginForm form submit function
     */
    ,submitFn: function(){
        this.loginForm.getForm().submit({
            waitTitle:'Аутентификация'
            ,waitMsg:'Отправляются данные...'
            ,scope: this
            ,success: function(){
                var url = '/';
                window.location = url;
            }
            ,failure: function(form, action){
                if(action.failureType != 'client'){
                	if (action.result.msg) {
                		var msg = action.result.msg;
                	} else {
                		var msg = "Неверные логин/пароль";
                	}
                    Ext.Msg.alert('Ошибка!', msg);
                    this.loginForm.getForm().reset();
                }else{
                    Ext.Msg.alert('Ошибка!', 'Неправильно заполнена форма');
                }
            }
        })
    }
    
});
