function fbInitSdk(fbAppId) {
	window.fbAsyncInit = function() {
	FB.init({
	  appId  : fbAppId,
	  status : true, // check login status
	  cookie : true, // enable cookies to allow the server to access the session
	  xfbml  : true  // parse XFBML
	});

	FB.Event.subscribe('auth.login', function(response) {
	   login();
	});

	FB.Event.subscribe('auth.logout', function(response) {
	   logout();
	});

	FB.getLoginStatus(function(response) {
	if (response.session) {
		greet();
	   }
	});

	};

	(function() {
	var e = document.createElement('script');
	e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
	e.async = true;
	document.getElementById('fb-root').appendChild(e);
	}());
}

function login(){
   FB.api('/me', function(response) {
//	  alert('You have successfully logged in, '+response.name+"!");
   });
}

function logout(){
	alert('You have successfully logged out!');
}

function greet(){
   FB.api('/me', function(response) {
	  alert('Welcome, '+response.name+"!");
   });
}

function setStatus(){
	// check if user is logged in:
	FB.getLoginStatus(function(response) {
	  if (response.session) {
			new_status = document.getElementById('status').value;
			FB.api(
			  {
				method: 'status.set',
				status: new_status
			  },
			  function(response) {
				if (response == 0){
					alert('Your facebook status not updated. Give Status Update Permission.');
				}
				else{
					alert('Your facebook status updated');
				}
			  }
			);
	  } else {
			alert('please log in first :)');
	  }
	});
}

function fbGetMyInfo(element_id) {
	 FB.api('/me', function(response) {
		  var query = FB.Data.query('select uid, name, email, hometown_location, sex, pic_square from user where uid={0}', response.id);
		  query.wait(function(rows) {
			document.getElementById(element_id).innerHTML =
			  'Your name: ' + rows[0].name + "<br />" +
			  'Your email: ' + rows[0].email + "<br />" +
			  'Your hometown_location: ' + rows[0].hometown_location + "<br />" +
			  'Your sex: ' + rows[0].sex + "<br />" +
			  'Your uid: ' + rows[0].uid + "<br />" +
			  '<img src="' + rows[0].pic_square + '" alt="" />' + "<br />";
		  });
	 });
}

function fbGetFriendsInfo(element_id) {
	 FB.api('/me', function(response) {
		  var query = FB.Data.query('SELECT uid, name, email, hometown_location, sex, pic_square FROM user WHERE uid = me() OR uid IN (SELECT uid2 FROM friend WHERE uid1 = me())', response.id);
		  query.wait(function(rows) {
			document.getElementById(element_id).innerHTML = '';
			for(i=0;i<rows.length;i++) {
			document.getElementById(element_id).innerHTML +=
			  'Your name: ' + rows[i].name + "<br />" +
			  'Your email: ' + rows[i].email + "<br />" +
			  'Your hometown_location: ' + rows[i].hometown_location + "<br />" +
			  'Your sex: ' + rows[i].sex + "<br />" +
			  'Your uid: ' + rows[i].uid + "<br />" +
			  '<img src="' + rows[i].pic_square + '" alt="" />' + "<br />";
			}
		  });
	 });
}
