(function($) {
$.lmsAuth = {
    login: function(response) {
        window.location = response.location;
    },
    _resetPassword: function() {
        $('#recoverPasswordDialog').dialog('close');
        $.error.show('#recoverPassword', 'Your request has been submitted. Please wait to receive the email with your new password.');
    },
    init: function() {
        $('#loginForm').populateForm({
            form: 'login',
            success: function(resp) {
                $.lmsAuth.login(resp);
            },
            validate: true,
            fetch: false
        });
        $('#username').focus();

        $('#loginForm ol').append($('<li/>').html($('<a/>').attr({
            id: 'recoverPassword',
            href: '#recoverPasswordDialog',
            'rel': 'dialog'
        }).html('Forgot your password?').addClass('recoverPassword')));
        $('#recoverPassword').click(function(e) {
            e.preventDefault();
            $('#recoverPasswordDialog').dialog({
                modal: true,
                title: 'Reset Password',
                width: 355,
                buttons: {
                    'Submit': function(e) {
                        e.preventDefault();
                        $('#recoverPasswordForm').submit();
                    }
                }
            });
            $('#recoverPasswordForm input[name=email]').focus();
        });
        $('#recoverPasswordForm').populateForm({
            form: 'recoverPassword',
            success: function(resp) {
                $.lmsAuth._resetPassword();
            },
            validate: true,
            fetch: false
        });
    }
};
})(jQuery);

