//Facebook Connect
function auth_using_fb() {
  //get the users data from FB
  var viewer  = FB.Facebook.apiClient.fql_query(
  
      'SELECT name, pic_square_with_logo,profile_url FROM user WHERE uid='+FB.Facebook.apiClient.get_session().uid,
      
      function(results) {
        update_userbox( results[0].name,
                        results[0].pic_square_with_logo,
                        results[0].profile_url,
                        'FB.Connect.logoutAndRedirect("./index.php");return false;')
      }
  );
}


//Google Friend Connect
function auth_using_gfc() {
  //Request GFC to send extra profile data
  var params = {};
      params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] =
      [opensocial.Person.Field.URLS];
      
  
  // Create a request to grab the current viewer.
  var req = opensocial.newDataRequest();
  req.add(req.newFetchPersonRequest('VIEWER', params), 'viewer_data');
  // Sent the request
  req.send(function(data) {
  
    // If the view_data had an error, then user is not signed in
    if (!data.get('viewer_data').hadError()) {
      //get the users data from GFC
      var viewer = data.get('viewer_data').getData();
      
      update_userbox( viewer.getDisplayName(),
                      viewer.getField(opensocial.Person.Field.THUMBNAIL_URL),
                      viewer.getField(opensocial.Person.Field.URLS)[0].getField('address'),
                      'google.friendconnect.requestSignOut()' );
    }
  
  });
}


//Generic updates #userbox with info retrieved
//from services
function update_userbox(name, image, url, logout) {

  //populate the data in #userbox and show it
  $('#userbox').html( "<a href='"+url+"'>"
                    + "<img alt='"+name+"' src='"+image+"' />"
                    + "Logged in as " + name + "</a> "
                    + "(<a href='./index.php' onclick='" + logout + "'>logout</a>)" ).show();
  
  //hide name input and service
  //login buttons
  $('#userinfo').hide();
  
  
  //populate the values of the inputs
  //using data from serivce
  $('#name').val(name);
  $('#url').val(url);
  $('#image').val(image);

}