/* Facebook Resources:
	http://developers.facebook.com/docs/reference/javascript/fb.api/
	http://developers.facebook.com/docs/reference/api/user/
	http://developers.facebook.com/docs/authentication/permissions
*/

FB.init({
	appId  : '178717248833741',
	//appId  : '133988896664512',
	status : true, // check login status
	cookie : true, // enable cookies to allow the server to access the session
	xfbml  : true  // parse XFBML
});

var fb_logged_in=0;
/*////////// 
// Log in the user
//////////*/
function fb_login() {
	var cb = function(response) {
		if (response.session) {
			if (response.perms) {
				FB.api('/me', function(fb_data) {
					$.ajax({
						url: '/ajax/facebook.php',
						type: "GET",
						data: ({f:"login", facebook_uid:fb_data.id, username:fb_data.name, birthday:fb_data.birthday, email:fb_data.email, gender:fb_data.gender}),
						success: function(data) {
							//parse the result
							var obj = jQuery.parseJSON(data);
							if(obj.location=="reload") {
								window.location.reload();
							} else {
								window.location="/register.php";
							}
							
						}
					});
				});
			}
		}
	};
	//if(!fb_logged_in) {
		FB.login(cb, { perms: 'publish_stream,user_birthday,email' });
	//}
}


/*////////// 
// Log out the user
//////////*/
function fb_logout() {
	FB.logout(function(response) {
		// user is now logged out
		$.ajax({
			url: '/ajax/facebook.php',
			type: "GET",
			data: ({f:"logout"}),
			success: function(data) {
				fb_logged_in = 0;
				window.location="/";
			}
		});
	});
}


/*/////////
// Check if fb user is logged in
////////*/
FB.getLoginStatus(function(response) {
	if (response.session) {
		FB.api('/me', function(response) {
			fb_logged_in = response;
		});
	}
});

