{% load url from future %}
<div id="fb-root"></div>
<script>
  window.FB_login = function() {};
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '{{facebook_app.key}}',
      channelUrl : '{{facebook_channel_url}}',
      status     : true,
      cookie     : true,
      oauth      : true,
      xfbml      : true
    });
    window.FB_login = function(nextUrl) {
      FB.login(function(response) {
        if (response.authResponse) {
          document.getElementById("_fb_access_token").value = response.authResponse.accessToken;
          document.getElementById("_fb_expires_in").value = response.authResponse.expiresIn;
          document.getElementById("_fb_next_url").value = nextUrl || '';
          document.getElementById("_fb_login").submit();
        } else {
          var next;
          if (response && response.status && response.status == "notConnected") {
            next = '{% url 'socialaccount_login_cancelled' %}';
          } else {
            next = '{% url 'socialaccount_login_error' %}';
          }
          window.location.href = next;
        }
      }, {scope: '{{facebook_perms}}' });
    }
  };
  (function(d){
     var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/{{facebook_jssdk_locale}}/all.js";
     d.getElementsByTagName('head')[0].appendChild(js);
   }(document));
</script>

<form id="_fb_login" method="post" action="{% url 'facebook_login_by_token' %}">
{% csrf_token %}
<input type="hidden" name="next" value="" id="_fb_next_url"/>
<input type="hidden" name="access_token" id="_fb_access_token"/>
<input type="hidden" name="expires_in" id="_fb_expires_in"/>
</form>
