function fbAsyncInit ( ) { 
	FB.init( { 
		appId:   FACEBOOK_APP_ID,
		status:  true,
		cookie:  true,
		oauth:   true,
		xfbml:   true
	} );

	FB.getLoginStatus( function ( response ) {

		try
		{
			if (response.status === 'connected') {
				// logged into fb and has authorized app
				is_logged_into_facebook = true;
				// var uid = response.authResponse.userID;
				// var accessToken = response.authResponse.accessToken;
			} else if (response.status === 'not_authorized') {
				// logged into fb but hasn't authorized app
				is_logged_into_facebook = false;
			} else {
				is_logged_into_facebook = false;
			}
		}
		catch ( e )
		{
			is_logged_into_facebook = false;
		}
	} );
}

$( function ( ) {
	$( 'a.fb_button' ).live( 'click', function ( ) { 
		FB.Event.subscribe( 'auth.authResponseChange', function ( response ) { 

			try
			{
				if ( response.authResponse ) {
					window.location.href = '/oauth/fb/connected';
				}
			}
			catch ( e )
			{
				// We don't want to keep redirecting to the oauth connection
				// page if there's a problem with this call.  Catch this so
				// as not to continue with the automatic redirects.  So yes,
				// this actually a case where I don't want to do anything with
				// the caught error! :)
			}
		} );

		if ( is_logged_into_facebook ) {
			window.location.href = '/oauth/fb/connected';
		}
	} );
} );

