/**
 *
 * @param e
 */
function login_press(e){
    code= (e.keyCode ? e.keyCode : e.which);
    if (code == 13)
    {
        login();
    }
}

/**
 *
 */
function login()
{
    $.ajax({
        type:   'POST',
        url:    _URL_BASE_+'login/login',
        data:   {
            data: $('#formLogin').serialize()
        },
        dataType: 'json',
        success: function(data) {
            if (data.valido == 'OK') {
                window.location.href = './areadoatleta';
            } else {
                exibe_modal('Login', data.msg, 'sm');
            }
        }
    });
}

/**
 *
 * @param e
 */
function login_inscricao_press(e){
    code= (e.keyCode ? e.keyCode : e.which);
    if (code == 13)
    {
        login_inscricao();
    }
}

/**
 *
 */
function login_inscricao()
{
    $.ajax({
        type:   'POST',
        url:    _URL_BASE_+'login/login',
        data:   {
            data: $('#formLoginInscricao').serialize()
        },
        dataType: 'json',
        success: function(data) {
            if (data.valido == 'OK') {
                if (data.msg != '') {
                    exibe_modal('Login', data.msg, 'sm');
                    window.location.href = './areadoatleta';
                } else {
                    window.location.href = _URL_BASE_+'inscricao/'+$('#eventoURL').val()+'/inscricao';
                }
            } else {
                exibe_modal('Login', data.msg, 'sm');
            }
        }
    });
}

/**
 *
 */
function logout()
{
    $.ajax({
        type:       'POST',
        url:        _URL_BASE_+'login/logout',
        dataType:   'text',
        success:    function(data) {
            /*
            FB.getLoginStatus(function(response) {
                if (response.status === 'connected') {
                    FB.logout(function(response) {
                        window.location.href = './';
                    });
                } else if (response.status === 'not_authorized') {
                    FB.logout(function(response) {
                        window.location.href = './';
                    });
                }
            });
            */
            window.location.href = './';
        }
    });

}


/**
 *
 */
function login_esqueceu_senha_enviar()
{
    if ($('#usuario_equeceu_senha').val() == '') {
        exibe_modal('Login', 'Digite o email', 'sm');
        return;
    }
    exibe_modal('Login', 'Aguarde ...', 'sm');
    $.ajax({
        type:   'POST',
        url:    _URL_BASE_+'login/esqueceusenhaenviar',
        data:   {
            data: $('#formEsqueceuSenha').serialize()
        },
        dataType: 'json',
        success: function(data) {
            if (data.valido == 'OK') {
                exibe_modal('Login', 'Senha enviada. Verifique seu email.', 'sm');
            } else {
                exibe_modal('Login', data.msg, 'sm');
            }
        }
    });
}

/**
 *
 */
function login_facebook()
{
    var url = window.location.href;
    FB.login(function(response) {
        if (response.authResponse) {
            FB.api('/me', {fields: 'id, name, email'}, function(response) {
                console.log(response);
                $.ajax({
                    type:   'POST',
                    url:    _URL_BASE_+'login/facebook',
                    data:   {
                        id: response.id,
                        email: response.email
                    },
                    dataType: 'json',
                    success: function(data) {
                        if (data.valido == 'OK') {
                            if ($('#eventoURL').length > 0) {
                                window.location.href = url;
                            } else {
                                window.location.href = '/areadoatleta';
                            }
                        } else {
                            exibe_modal('Login', data.msg, 'sm', 'OK', 'window.location.href = \''+_URL_BASE_+'cadastro\'');
                        }
                    }
                });
            });
        } else {
            console.log('User cancelled login or did not fully authorize.');
        }
    }, {scope: 'public_profile, email'});
}

/**
 *
 */
function login_facebook_evento(url)
{
    FB.login(function(response) {
        if (response.authResponse) {
            FB.api('/me', function(response) {
                $.ajax({
                    type:   'POST',
                    url:    _URL_BASE_+'login/facebook',
                    data:   {
                        id: response.id
                    },
                    dataType: 'text',
                    success: function(data) {
                        if (data == 'OK')
                        {
                            window.location.href = 'inscricao/'+url+'/regulamento';
                        }
                        else
                        {
                            exibe_modal('Login', 'Usuário não cadastrado. Clique em OK e preencha o formulário de cadastro.', 'sm', 'OK', 'window.location.href = \''+_URL_BASE_+'cadastro\'');
                        }
                    }
                });
            });
        } else {
            console.log('User cancelled login or did not fully authorize.');
        }
    }, {scope: 'public_profile,email'});
}