jQuery(document).ready(function(){
    //Permet d'initialiser la session Facebook (à déclarer sur toutes les pages utilisant les fonctionnalités Facebook)
    try{
        FB.init("096ee2bf012f9acadda847e03ee867b5","http://www.itaste.com/fbconnect/xd_receiver.php");
    } catch(e){}
    jQuery('#formulaireInscription').hide();
});


//permet de tuer la session facebook en cours, et redirige vers l'index'
function deconnect_fb(){
    FB.Connect.logout(function(e){
        document.location.href = 'index.php';
    });
}


//action exécuté après la connexion
//si login OK redirection vers l'accueil, sinon affichage du formulaire d'inscription'
function update_bt_login(){
    jQuery.post("/fbconnect/ajax/login_fb.php", {}, function(data) {
        if(data == true){
             window.parent.location.href='/application/MyHome.php';
        }else if(data == false){
            /*jQuery.getJSON('/fbconnect/ajax/recup_infos.php',{},function(json){
                jQuery('#formulaireInscription').hide().fadeIn('slow');
                jQuery("#name").val(json['name']);
                jQuery("#surname").val(json['surname']);
                jQuery("#id_fb_user").val(json['fb_user']);
                jQuery("#session_fb_user ").val(json['session_fb_user ']);
            })*/
             window.parent.location.href='/application/22bRegister.php?ref=fbconnect';
        }else{
            alert("erreur interne");
        }
    });
}

function fb_login(){
    jQuery.post("/fbconnect/ajax/login_fb.php", {}, function(data) {});
}

function link_profile_fb(){
    jQuery.post("/fbconnect/ajax/linkProfile.php", {}, function(data) {
        if(data == true){
             autorisation_newsfeed();
             jQuery("#linkProfiles").hide();
             jQuery("#successMsgProfilesLinked").show();
        }
    });
}

function publication_newsfeed(){
    var template_bundle_id = 107382356494; //identifiant du template créé via l'outils Facebook http://developers.facebook.com/tools.php?feed
    var contenu = jQuery("#contenuNewsfeed").val();
    var template_data = {
        "contenu":contenu,
        "images":[{"src":"http://www.google.fr/intl/fr_fr/images/logo.gif", "href":"http://www.google.fr"}]
    };
    var body_general = '';
    //plus d'info sur cette fonction ici http://wiki.developers.facebook.com/index.php/Facebook.showFeedDialog
    FB.Connect.showFeedDialog(template_bundle_id,template_data,body_general);
}

function autorisation_newsfeed(){
    //la liste des autorisations est disponnible ici http://wiki.developers.facebook.com/index.php/Extended_permissions
    FB.Connect.showPermissionDialog('publish_stream, offline_access');
}

function callPublish(msg, attachment, action_links) {
    //FB.Connect.streamPublish('', attachment, action_link);
    FB.ui(
       {
         method: 'stream.publish',
         message: msg,
         attachment: attachment,
         action_links: action_links,
         user_message_prompt: 'Share your thoughts'
       },
       function(response) {
         /*if (response && response.post_id) {
           alert('Post was published.');
         } else {
           alert('Post was not published.');
         }*/
       }
     );
}
