// ---------------------------------------------------------------------
// Used on the login page
// ---------------------------------------------------------------------
function connect_user(url) {
  $('#facebook_button').html('<div class="activity_indicator"><img src="/css_images/ajax_loader.gif"/> Contacting Facebook ...</div>');
  var api = FB.Facebook.apiClient; 
  api.users_getInfo([api.get_session().uid], ['first_name', 'last_name', 'pic_small', 'pic_big', 'about_me', 'sex', 'current_location'], function(result, ex) {
    $('#facebook_photo_element .facebook_image').html('<img src="'+result[0].pic_small+'" />');
    $('#facebook_photo_element').show();
    $('#user_first_name').val(result[0].first_name);
    $('#fb_first_name').html(result[0].first_name);
    $('#user_last_name').val(result[0].last_name);
    if(result[0].current_location != null) {
      $('#user_zip').val(result[0].current_location.zip);
    }
    $('#user_gender').val(result[0].sex);
    $('#user_bio').val(result[0].about_me);
    $('#facebook_photo').val(result[0].pic_big);
    $('#facebook_photo_thumb').val(result[0].pic_small);
    $('#facebook_button').hide();
    $('.login_box.registration').addClass('facebook');
    $('#user_email').trigger('focus');
    get_friends(url, result[0].first_name);
  });
}

function get_friends(url, first_name) {
  var api = FB.Facebook.apiClient; 
  var friends = new Array();
  api.friends_get(null, function(result){ 
    api.users_getInfo(result, ['first_name', 'last_name'], function(friends_result, ex) {
      for (var i = 0; i < friends_result.length; i++){
        friends[friends.length] = friends_result[i].first_name+' '+friends_result[i].last_name;
      };
      $.post(url, {'users[]':friends, 'authenticity_token':fb_auth_token}, function(data){
        if (data == '') {
          $('#facebook_photo_element h2.your_connections').html('Be the first of your Facebook friends to join ActofGood.org! ');
          $('#facebook_photo_element .welcome_text').html('<p>Hi '+first_name+'!</p><p>Complete the Sign Up process to invite your Facebook friends to connect with you and support their favorite charitable organizations on ActofGood.org.</p>');
        } else {
          $('#facebook_photo_element h2.your_connections').html('Your Facebook connections!')
          $('#facebook_photo_element .welcome_text').html('<p>Hi '+first_name+'!</p><p>These Facebook friends are already on ActofGood.org.</p><p>Select the boxes to the right of the names to invite these connections to your ActofGood network.</p>');
          $('.login_box.right_column').addClass('facebook');
          $('.right_column .content').html(data);
          $('.right_column .footer_text').html('&nbsp;');
          // Check pre-selected friends
          $.each($('#user_people_you_may_know').val().split(','), function(){
            $('#connect_user-'+this).attr('checked', true);
          });
        }
      });
    });
  });
}

function update_people_you_may_know(checkbox) {
  var people_you_may_know = new Array();
  $('input:checkbox:checked.person_you_may_know').each(function(){
    people_you_may_know.push(this.value);
  });
  $('#user_people_you_may_know').val(people_you_may_know.join(','));
}




// ---------------------------------------------------------------------
// Used on the invite friends page
// ---------------------------------------------------------------------
function get_facebook_friends(url) {
  $('#facebook_button').html('<div class="activity_indicator"><img src="/css_images/ajax_loader.gif"/> Contacting Facebook ...</div>');
  var api = FB.Facebook.apiClient; 
  var friends = new Array();
  api.friends_get(null, function(result){ 
    api.users_getInfo(result, ['first_name', 'last_name'], function(friends_result, ex) {
      for (var i = 0; i < friends_result.length; i++){
        friends[friends.length] = friends_result[i].first_name+' '+friends_result[i].last_name;
      };
      $.post(url, {'users[]':friends, 'authenticity_token':fb_auth_token}, function(data){
        if (data == '') {
          $('#facebook_button').html("We couldn't find any Facebook friends on ActofGood.org!");
        } else {
          $('.invitation_box .title').html('Find your Facebook friends on ActofGood');
          $('.invitation_box .content').html(data);
        }
      });
    });
  });
}
