ErrorManager = {
    NONE : 0,
    DOES_NOT_EXIST : 101
}

window.fbAsyncInit = function() {
      FB.init({
        appId: '166211956726115',
        status     : true, 
        cookie     : true,
        xfbml      : true,
        oauth      : true
      });
   fbApiInit = true;
    };
    (function(d){
       var js, id = 'facebook-jssdk';if (d.getElementById(id)) {return;}
       js = d.createElement('script');js.id = id;js.async = true;
       js.src = "//connect.facebook.net/en_US/all.js";
       d.getElementsByTagName('head')[0].appendChild(js);
     }(document));


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

function countCommonFriendsFb(fuid){
    FB.api('/me/mutualfriends/'+fuid, function(response) {
        if (response && !response.error) {
            $('td.commonFriends').html(response.data.length);
            if(response.data.length)
                $('td.commonFriends').parent('tr').fadeIn();
        }
    });
}

function commonFriendsFb(fuid){
    
    FB.api('/me/mutualfriends/'+fuid, function(response) {
        if (response && !response.error) {
            var friendsfuid = new Array();
            for(var i in response.data){
                friendsfuid.push(response.data[i].id);
            }
            if(friendsfuid.length){
                $('#contentMenuProfile').load('/user/facebookCommonFriends', {friendsfuid: friendsfuid});
            }
        }
    });
}

function connectFb(option){
     FB.api('/me', function(response) {
         $.post('/login/loginFb', {fuid: response.id}, function(data){
             if(data.status == ErrorManager.NONE){
                window.location = option.redirect || "/en/profile";
             }
             else if(data.status == ErrorManager.DOES_NOT_EXIST){
               $('#errorPanel').html(option.errorDoesNotExistMessage || "Your Facebook is not linked with a JustBip account");
               $('#errorPanel').fadeIn().delay('5000').fadeOut();
             }   
         },"json");
    });
}

function registerFb(option){
    FB.getLoginStatus(function(response) {
        if (!response.session) {
            FB.login(function(response){
                FB.api('/me', function(response) {
                     $('#user_lastname').val(response.last_name);
                     $('#user_firstname').val(response.first_name);
                     $('#user_email').val(response.email);
                     response.gender == 'male' ? $('#user_gender_1').attr('checked', true) : $('#user_gender_1').attr('checked', true);
                    var bday = response.birthday.split('/')
                    $('#user_dateofbirth_month').val(parseInt(bday[0]));
                    $('#user_dateofbirth_day').val(parseInt(bday[1]));
                    $('#user_dateofbirth_year').val(parseInt(bday[2]));
                    $('#fuid').val(response.id);
                    //$('#user_pseudo').val(response.name);
                });
            }, {scope:'email,user_birthday,publish_stream'});
        }
    });
}


function loginFb(){
    FB.getLoginStatus(function(response) {
        if (!response.session) {
            FB.login(function(response){
                FB.api('/me', function(response) {
                    var fuid = response.id;
                    $.post('/user/addFuid', {fuid:fuid});
                    window.location.reload();
                });
            }/*, {scope:'email,user_birthday,publish_stream'}*/);
        }

    });
}
function logoutFb(){
    $.post('/user/delFuid');
    FB.logout(function(response) {
        $.post('/user/delFuid');
        window.location.reload();
    });
    

}
