$(document).ready(function() {

   // animate the flash message
   $('#flash_message').animate({ backgroundColor: "#ffffe0" }, 2000);
   
   // register external links
   $('a.external_link').attr('target', '_blank');
   
   // initialize facebook
   FB_RequireFeatures(["XFBML"], function() {
      
      FB.Facebook.init(facebookAPIKey, "/xd_receiver.html"); 
      
      // handle a variety of different tasks if the user is logged into facebook
      FB.Connect.ifUserConnected(function() {
         
         facebookReady();
      });
   });
});

function facebookReady() {
   // called when we know that the Facebook connection is ready
   
   // remove the Facebook connnect box(es) 
   $('.fb_connect').remove();
   
   // hook the sign out button
   $('a.sign_out').click(function() {
      
      FB.Connect.logoutAndRedirect('/futureme/sign-out/');
      
      // ignore the click
      return false;
   });
}

function verifyFacebookForAccount() {
   // makes an AJAX call to verify that the logged-in Facebook account
   // is consistent with the current FutureMeAccount
   $.ajax({
      url: '/futureme/fbconnect/ajax-verify/',
      dataType: 'text',
      success: function() {
         // all good
         facebookReady();
      },
      error: function(request, status, exception) {
         
         // logout of facebook
         FB.Connect.logoutAndRedirect('/futureme/sign-out/');
      }
   });
}

