Dather.login_with_facebook_session = function(access_token){
  $('#facebook_connect_form #facebook_access_token').val(access_token);
  $('#facebook_connect_form').submit();
}

Dather.add_source_with_facebook_session = function(token){
  var source_identifier_tag = $("#source_identifier_facebook")
  source_identifier_tag.val(token)
  source_identifier_tag.parents("form:first").submit()
}

Dather.facebook_access_token_from_auth = function(){
  var perms = "user_about_me, read_stream, user_status, user_birthday, user_events, user_likes, user_photos, user_videos, email, publish_stream, offline_access"
  FB.login(function(response) {
    if (response.session) {
      // user successfully logged in
      return response.session.access_token
    } else {
      // user cancelled login, do nothing
    }
  }, {perms: perms});
}

Dather.facebook_login_handler = function(response){
  if(response.session){
    var token = response.session.access_token;
  }else{
    var token = Dather.facebook_access_token_from_auth();
  }
  Dather.login_with_facebook_session(token);
}

Dather.facebook_add_source_handler = function(response){
  if(response.session){
    var token = response.session.access_token;
  }else{
    var token = Dather.facebook_access_token_from_auth();
  }
  Dather.add_source_with_facebook_session(token);
}

//TODO: is the stuf below this used?

//function requestFacebookConnect(callback) {
//  Dather.facebookPermissionRequested = false
//
//  $.remoteDebug.start("Facebook Connect", function(debug) {
//    FB.Connect.requireSession(debug.callback("FB.Connect.requireSession", function(debug) {
//      if (!Dather.facebookPermissionRequested) {
//        FB.Connect.showPermissionDialog("offline_access,email", debug.callback("FB.Connect.showPermissionDialog", function(result, debug) {
//          debug.log("Permissions dialog displayed.")
//
//          if (result) {
//
//            debug.log("Permissions accepted.")
//
//            callback(result)
//
//            debug.success()
//          }
//          else {
//
//            debug.log("Permissions were denied.")
//
//            // Set facebook_permission_requested to false so in the next
//            // click the user will be prompted again for permissions.
//            Dather.facebookPermissionRequested = false
//          }
//        }))
//      }
//
//      Dather.facebookPermissionRequested = true
//
//      debug.success()
//    }))
//
//    debug.success()
//  })
//}
//
//function fb_connect_invite_friends(options) {
////  requestFacebookConnect(function(result){
//  fb_connect_invite_friends_page_setup(options);
////    $(self).close()
////  });
//}
//function fb_connect_invite_friends_page_setup(options){
//  if ( typeof options != 'object' ) { options = {} }
//  if ( options.title == undefined ) { options.title = "Invite your friends" }
//  if ( options.type == undefined ) { options.type = window.location.hostname }
//  if ( options.all_friends_invited == undefined ) { options.all_friends_invited = "<div style='padding: 10px; font-size: 1.2em;'>You've already invited all of your friends and they've accepted.</div>" }
//  if ( options.invitation_copy == undefined ) { options.invitation_copy = "" }
//  if ( options.invitation_choice_url == undefined ) { options.invitation_choice_url = window.location.protocol + '//' + window.location.host + '/' }
//  if ( options.invitation_choice_label == undefined ) { options.invitation_choice_label = 'Accept' }
//  if ( options.request_action_url == undefined ) { options.request_action_url = window.location.href }
//  if ( options.request_action_text == undefined ) { options.request_action_text = options.title }
//  if ( options.friend_selector_rows == undefined ) { options.friend_selector_rows = 3 }
//  if ( options.friend_selector_email_invite == undefined ) { options.friend_selector_email_invite = 'true' }
//  if ( options.friend_selector_bypass == undefined ) { options.friend_selector_bypass = 'cancel' }
//  if ( isNaN(options.width) ) { options.width = 600 }
//  if ( isNaN(options.height) ) { options.height = 510 }
//
//  var api = FB.Facebook.apiClient
//  var sequencer = new FB.BatchSequencer()
//  var friends = api.friends_get(null, sequencer)
//  var friends_app_users = api.friends_getAppUsers(sequencer)
//
//  sequencer.execute(function() {
//    var friend_ids = ''
//    try {
//      friend_ids = friends.result.sort().join(',')
//    }catch(e) {;}
//
//    var exclude_ids = ''
//
//    var dialog = new FB.UI.FBMLPopupDialog(options.title, '')
//
//    if ( friend_ids.length > 0 && exclude_ids.length > 0 && friend_ids == exclude_ids ) {
//      var fbml = ''
//      fbml += '<fb:fbml>'
//      fbml += options.all_friends_invited
//      fbml += '</fb:fbml>'
//
//      dialog.setFBMLContent(fbml)
//      dialog.setContentWidth(300)
//      dialog.setContentHeight(70)
//    }else {
//      var content = ''
//      content += options.invitation_copy
//      content += "<fb:req-choice url='" + options.invitation_choice_url + "' label='" + options.invitation_choice_label + "' />"
//
//      var fbml = ''
//      fbml += '<fb:fbml>'
//      fbml += '<fb:request-form type="' + options.type + '" content="' + content + '" invite="true" action="' + options.request_action_url + '" method="post">'
//      fbml += '<fb:multi-friend-selector'
//      fbml += ' actiontext="' + options.request_action_text + '" '
//      fbml += ' showborder="true" '
//      fbml += ' rows="' + options.friend_selector_rows + '" '
//      fbml += ' exclude_ids="' + exclude_ids + '" '
//      fbml += ' bypass="' + options.friend_selector_bypass + '" '
//      fbml += ' email_invite="' + options.friend_selector_email_invite + '" '
//      fbml += '/>'
//      fbml += '</fb:request-form>'
//      fbml += '</fb:fbml>'
//
//      dialog.setFBMLContent(fbml)
//      dialog.setContentWidth(options.width)
//      dialog.setContentHeight(options.height)
//    }
//    dialog.show()
//  })
//}

