﻿var Facebook = {
  LoginStatus: {
    LoggedIn: false,
    PublishEnabled: false,
    SetLoginStatus: function () {
      FB.getLoginStatus(function (response) {
        Facebook.LoginStatus.LoggedIn = response.session != null;
        Facebook.LoginStatus.PublishEnabled = response.perms.indexOf("publish_stream") > -1;
      }, { perms: 'publish_stream' });
    }
  },
  login: function () {
    FB.login(
		  function (response) {
		    if (response.session && response.perms) {
		      Facebook.LoginStatus.SetLoginStatus();
		    }
		  },
		  { perms: 'publish_stream' });
  },
  publish: function (name, link) {
    Facebook.LoginStatus.SetLoginStatus();
    if (!Facebook.LoginStatus.PublishEnabled) Facebook.LoginStatus.SetLoginStatus();

    if (!Facebook.LoginStatus.PublishEnabled) {
      Facebook.login();
      $("#fbShareStatus").html("Your journal entry was not published. Please try again.");
      $("#fbShareStatus").removeClass();
      $("#fbShareStatus").addClass("legal error");
    } else {
      FB.api('/me/feed', 'post',
				  {
				    message: name,
				    link: link,
				    caption: "Check out this Journal Page on Smart Fish Map!",
				    picture: "http://www.smartfishmap.com/images/SmartFishMapLogo72.png"
				  });
      $("#fbShareStatus").html("Your journal entry was published on your wall.");
      $("#fbShareStatus").removeClass();
      $("#fbShareStatus").addClass("legal");
    }
  }
};

