$(document).ready(function(){
    FB.init({ apiKey: 'b62e8b2ffad7293b9b653d2d35262d93', status: true, cookie: true, xfbml: true });
    FB.getLoginStatus(handleSessionResponse);
    $('#login').bind('click', function() {
	FB.login(handleSessionResponse);
    });
    $('#logout').bind('click', function() {
	FB.logout(handleSessionResponse);
    });
    $('#disconnect').bind('click', function() {
	FB.api({ method: 'Auth.revokeAuthorization' }, function(response) {
	    clearDisplay();
	});
    });
    function clearDisplay() {
	//$('#login').hide('fast');
    }
    function handleSessionResponse(response) {
	if (!response.session) {
	    clearDisplay();
	    return;
	}
	FB.api({
	    method: 'fql.query',
	    query: 'SELECT name, pic FROM profile WHERE id=' + FB.getSession().uid
	},function(response) {
	    var user = response[0];
	    if (user) {
		$('#login').html(user.name).show('fast');
	    }
	});
    }
});

