
$(function() {
	var app_id = '184245778775374';
	var scopes = 'email, public_profile, user_friends';
	
	var btn_login = '<a href="#" id="login"><img src="../LOGO/face.png" width= "18%"></a>';
	var div_session = "<div id='facebook-session'>"+
					  "<a href='#' id='logout' class='btn btn-danger'>DESLOGAR</a>"+
					  "</div>";

	window.fbAsyncInit = function() {
	  	FB.init({
			appId      : app_id,
			status     : true,
			cookie     : true, 
			xfbml      : true, 
			version    : 'v2.1'
		});
		FB.getLoginStatus(function(response) {
	    		statusChangeCallback(response, function() {});
	  	});
  	};

  	var statusChangeCallback = function(response, callback){	
		if (response.status === 'connected') {
			getFacebookData();
		} else 

  	}

  	var checkLoginState = function(callback) {
		FB.getLoginStatus(function(response) {
			callback(response);
		});
  	}

  	var getFacebookData =  function() {
  		FB.api('/me', function(response) {
	  		$('#login').after(div_session);
	  		$('#login').remove();
	  	});
  	}

  	var facebookLogin = function() {
  		checkLoginState(function(data) {
  			if (data.status !== 'connected') {
  				FB.login(function(response) {
  					if (response.status === 'connected')
  						getFacebookData();
						window.location = "https://rodrigogmartins.github.io/mov/feed.html";
  				}, {scope: scopes});
  			}
  		})
  	}

  	var facebookLogout = function() {
  		checkLoginState(function(data) {
  			if (data.status === 'connected') {
				FB.logout(function(response) {
					$('#facebook-session').before(btn_login);
					$('#facebook-session').remove();
				})
			}
  		})

  	}

  	$(document).on('click', '#login', function(e) {
  		e.preventDefault();
  		facebookLogin();
  	})

  	$(document).on('click', '#logout', function(e) {
  		e.preventDefault();
  		return confirm("Quer mesmo deslogar?")?facebookLogout():false;
  	})
})

{
			callback(false);
		}