$(document).ready(function () {
    $.ajaxSetup({cache: true})
    $.getScript('https://connect.facebook.net/en_US/sdk.js', function () {
        //console.log($('#fbappid').val())
        try {
        FB.init({
            appId: $('#fbappid').val(),
            version: 'v7.0', // or v2.1, v2.2, v2.3, ...
        })
        //$('#loginbutton,#feedbutton').removeAttr('disabled');
        //FB.getLoginStatus(updateStatusCallback);
        } catch(e) {

        }
    })

    if ($('#fbreg').length) {
        $('#fbreg').click(function (e) {
            e.preventDefault()
            fblogin('registration')
        })
    }
    
    if ($('#fblogin').length) {
        $('#fblogin').click(function (e) {
            e.preventDefault()
            fblogin('login')
        })
    }
    
    if ($('#fbloginpopup').length) {
        $('#fbloginpopup').click(function (e) {
            e.preventDefault()
            fblogin('login')
        })
    }

    function fblogin(whattodo) {
        let path = window.location.pathname,
            origin = window.location.href,
            regpage = 0,
            loginlayer = 0;
            
        if (path.includes('/registration')){
            console.log('registration=TRUE');
            regpage = 1;
        }
        if ($('.submenu-registration').hasClass('sys')) {
            loginlayer = 1;
        }
        
        var accessToken = ''
        FB.login(function (response) {
            if (response.authResponse) {
                accessToken = response.authResponse.accessToken
                $.ajax({
                    type: 'POST',
                    url: '/ajax_fblogin',
                    data: {
                        accessToken: accessToken,
                        whattodo: whattodo,
                        regpage: regpage,
                        loginlayer:loginlayer,
                        origin: origin,
                    },
                    dataType: 'json',
                    success: function (resp) {
                        if (resp.message) {
                            createPopup(resp.messageheader, resp.message, resp.messagebutton);
                        } else {
                            if (resp.redir) {
                                $(location).attr('href', resp.redir)
                            } else {
                                if (regpage == 1) {
                                    bd.trigger('click');
                                    if (resp.name) {
                                        $('#registration-billingname').val(resp.name).parent().addClass('has-value');
                                    }
                                    if (resp.email) {
                                        $('#registration-email').val(resp.email).parent().addClass('has-value');
                                    }
                                }
                            }
                        }
                    },
                })
            }
        }, {scope: 'email', auth_type: 'rerequest', return_scopes: true})
    }
                
})
