function logout(url) {
    if (FBU_id > 0) {
        //logout of fb before redirect to logout page.
        //FB.ensureInit(function() { FB.Connect.logoutAndRedirect(BDP.go('logout', null, true)); });
        fbEnsureInit(function() {
            //FB.Connect.Logout().waitUntilReady(function( status ) {BDP.go('logout');}); });//BDP.go('logout')); });
            FB.logout(function() {location.href=url});
        });
    } else {
        location.href=url;
    }
}

function fbEnsureInit(callback) {
    if (!window.fbApiInitialized) {
        setTimeout(function() {fbEnsureInit(callback);}, 50);
    } else {
        if (callback) {callback();}
    }
}

function fbLogin(callback, fscope) {
    console.log(fscope)
    var lscope=fscope;
    var dolog = function(callback, lscope) {
        console.log(lscope)
        FB.login(function(response) {
            if (response.authResponse) {
                //console.log('Welcome!  Fetching your information.... ');
                FB.api('/me', function(response) {
                    callback();
                    //console.log('Good to see you, ' + response.name + '.');
                });
            } else {
                //console.log('User cancelled login or did not fully authorize.');
                msg.showNotice('You have canceled the connecting.');
                //top.location.href='/';
            }
        }, {scope: lscope});
    };
    fbEnsureInit(dolog(callback, fscope))
}
