
if(window.FB) {
  FB_RequireFeatures(["XFBML","Api","Connect"]);
  FB.init('33fa30fc3ad0239e1cdacc073056db7f', "/xd_receiver.htm");
}

function FBFunction(fnWorker, fnUnavail, timeout){
  fnUnavail = fnUnavail || function(why) { alert("Sorry, Facebook is currently unavailable."); };
  return function(){
    if(!window.FB) {
      fnUnavail();
      return;
    }
    var me = this;
    var args = arguments;
    var timer;
    if(timeout){
      timer = window.setTimeout(function() {
                                  fnUnavail("timeout");
                                  timer = null;
                                },
                                timeout);
    }
    FB.ensureInit(function() {
                    if(timer !== null) { // has to be !== null as we're relying on it being undefined when there's no timer
                      // only run the worker if the timeout didn't fire and null itself out
                      if(timer !== undefined) window.clearTimeout(timer);
                      fnWorker.apply(me, args);
                    }
                  });
  };
}

function FBParseDom(){
  if(window.FB){
    FB.ensureInit(function(){
                    FB.XFBML.Host.parseDomTree();
                  });
  }
}


var getFriendsInfo = FBFunction(function(friends) {
  FB.Facebook.apiClient.users_getInfo(friends,
                                      ['last_name','first_name','uid','birthday','sex','profile_url','proxied_email','pic'],
                                      function(result, ex) {
                                        setFriendsInfo(result);
                                      });
});


var getFriendIds = FBFunction(function() {
  FB.Facebook.apiClient.friends_get(null, function(result, ex) {
    getFriendsInfo(result);
  });
});

var logoutFromFB = function(){
  if(window.FB && window.FB.Connect) FB.Connect.logoutAndRedirect('/logout');
  return false;
};

var inviteFbFriends = function() {
    jsonRequest("/api/friends/get/?json_function=doInviteFriends");
}

var doInviteFriends = function(data) {
    var excludelist = [];
    if (data.success) {
        for(var i = 0; i < data.friends.length; i += 1) {
            if (data.friends[i].fbid && data.friends[i].fbid != "") {
                excludelist.push(data.friends[i].fbid);
            }
        }
    }
    showFacebookInvite(excludelist);
}

var showFacebookInvite = FBFunction(function(exclude) {
   
    var title = "Invite your Facebook friends to Joost";
    var myHost = 'http://www.joost.com/';
    var body = '<fb:request-form \
	  action="'+ myHost +'friends/" \
	  method="POST" \
	  invite="true" \
	  type="Joost" \
	  content="This an invitation from Joost.com. \
	  &lt;fb:req-choice url=\''+ myHost  +'friends/\' \
	  label=\'Join Joost.com\' /&gt;"> \
	  <fb:multi-friend-selector \
	    showborder="false" \
        rows="4" \
        exclude_ids= \'' + exclude + '\' \
	    actiontext="Invite your friends to use Joost via Facebook Connect" />\
	  </fb:request-form>';
    var inviteIFrame = new FB.UI.FBMLPopupDialog(title,body);
    inviteIFrame.setContentWidth(590);
    inviteIFrame.setContentHeight(460);
    inviteIFrame.show();
  }, function(){return false}, 1000);

var promptForEmailPermission =  FBFunction(
    function () {
        var uri = window.location;
        var reg = new RegExp('/fb/$');
        if(reg.test(uri)) {
                FB.Connect.showPermissionDialog('email');
        }
    }, 
    function(){return false}, 1000);

