(function(){
    var FastLoginFormPanel = Ext.extend(Ext.FormPanel, {
        initComponent: function(){
            FastLoginFormPanel.superclass.initComponent.call(this);
            
            var proceed = function(success){
                document.location = document.baseURI + (success !== false ? 'dashboard' : 'user/login')
            }
            
            this.on({
                actioncomplete: function(form, action){
                    if (typeof action.result === 'undefined' || action.result.role !== 'student') {
                        proceed(true);
                        return;
                    }

                    // Cross-Login to Moodle
                    var loginData = this.getForm().getValues()

                    Ext.Ajax.request({
                        url: 'http://moodle.studyinteractive.org/login/index.php',
                        method: 'POST',
                        params: {
                            username: loginData.email,
                            password: loginData.password,
                            submit: 'Login',
                            testcookies: 1
                        },
                        callback: proceed
                    })
                },
                actionfailed: function(){
                    proceed(false)
                },
                scope: this
            });
            
            this.add({
                layout: 'column',
                style: 'background:transparent',
                defaults: {
                    width: 140,
                    layout: 'form'
                },
                items: [{
                    style:'background:transparent',
                    labelWidth:25,
                    items: [{
                        xtype:'textfield',
                        allowBlank:false,
                        msgTarget: 'title',
                        fieldLabel: 'Email',
                        labelSeparator:'',
                        name: 'email',
                        vtype:'email',
                        anchor:'95%',
                        listeners:{
                            'specialkey': {
                                fn:function(field,e){
                                    if (e.getKey() == Ext.EventObject.ENTER){
                                        Ext.getCmp('sign-in-button').handler.call(this)
                                    }
                                },
                                scope:this
                            }
                        }
                    }]
                },{
                    labelWidth:50,
                    items: [{
                        xtype:'textfield',
                        allowBlank:false,
                        msgTarget: 'title',
                        fieldLabel: 'Password',
                        labelSeparator:'',
                        name: 'password',
                        inputType:'password',
                        anchor:'95%',
                        listeners:{
                            'specialkey': {
                                fn:function(field,e){
                                    if (e.getKey() == Ext.EventObject.ENTER){
                                        Ext.getCmp('sign-in-button').handler.call(this)
                                    }
                                },
                                scope:this
                            }
                        }
                    }]
                },{
                    width: 60,
                    items:[{
                        xtype:'button',
                        text:'Sign In',
                        tooltip: 'Sign In',
                        id: 'sign-in-button',
                        handler: function(){
                            var form = this.getForm();
                            form.waitMsgTarget = Ext.DomQuery.select('.user')[0]; // todo: select by ID
    
                            if (form.isValid()){
                                form.submit({
                                    url: document.baseURI + 'user/login',
                                    waitMsg: 'Please wait a second...'
                                })
                            }
                        },
                        scope: this
                    }]
    
                }]
            });
        }
    });

    Ext.onReady(function(){
        var el = Ext.get('fast-login-form');
        
        if (el === null) {
            return;
        }
        
        var panel = new FastLoginFormPanel({
            style: 'background:transparent',
            renderTo: el
        });
    });
})();

