/*
 * Show the feed form. This would be typically called in response to the
 * onclick handler of a "Publish" button, or in the onload event after
 * the user submits a form with info that should be published. 
 *
 */
function facebook_publish_feed_story(form_bundle_id, template_data) {
  // Load the feed form
 FB.ensureInit(function() {
      FB.Connect.showFeedDialog(form_bundle_id, template_data);

      // hide the "Loading feed story ..." div
      ge('feed_loading').style.visibility = "hidden";
  });
}

function fb_send_story(comment_text) {
	if (!commenter_name) return submitComment();
	// send_story(post_url, post_title, story_template_id, comment_text, image_src, image_href);
        FB.Connect.get_status().waitUntilReady(function(status) {
          if (status == FB.ConnectState.connected) {
            send_story_new(post_url, post_title, comment_text, image_src, image_href);
          } else {
            submitComment();
          }
        });
}

function send_story_new(post_url,post_title,comment_text,image_src,image_href) {
  callPublish('posted a comment on "' + post_title + '"', {'name': post_title,'href': post_url, 'description': '"' + comment_text + '"', 'media':[{'type':'image','src': image_src,'href': image_href}]},[{'text':'Read More','href': post_url}]);
}

function callPublish(msg, attachment, action_link) {
  FB.ensureInit(function () {
    FB.Connect.streamPublish(msg, attachment, action_link, "", "", "submitComment");
  });
}

function send_story(post_url, post_title, story_template_id, comment_text, image_src, image_href) {
    FB_RequireFeatures(["Api", "Connect"], function()
    {
//        FB.Facebook.init(window.api_key, window.xd_receiver_url);
        FB.Connect.get_status().waitUntilReady(function(status) {
			var template_data = 
				{"images":[{"src": image_src, "href": image_href }],
				"post_title": post_title,
				"post_url": post_url,
				"comment_text": comment_text};
            FB.Connect.showFeedDialog(
                story_template_id,
                template_data,
				"",
				"",
				"",
				"",
				submitComment
            );
        });
    });
}

function submitComment() {
	document.comments_form.submit();
}

function facebook_logout() {
    FB_RequireFeatures(["Api", "Connect"], function() {
        FB.Facebook.init(window.api_key, window.xd_receiver_url);

        // Get the current logged in user
        var session = FB.Facebook.apiClient.get_session();
        if (!session) return;
        var viewerId = session.uid;
        if (!viewerId) return;

        FB.Connect.logout();
    });
}


/*
 * If a user is not connected, then the checkbox that says "Publish To Facebook"
 * is hidden in the "add run" form.
 * This function detects whether the user is logged into facebook but just
 * not connected, and shows the checkbox if that's true.
 *
 */
function facebook_show_feed_checkbox() {
  FB.ensureInit(function() {
      FB.Connect.get_status().waitUntilReady(function(status) {
          if (status != FB.UserLoginState.userNotLoggedIn) {
            // If the user is currently logged into Facebook, but has not
            // authorized the app, then go ahead and show them the beacon form + upsell
            checkbox = ge('publish_fb_checkbox');
            if (checkbox) {
              checkbox.style.visibility = "visible";
            }
          }
        });
    });
}

/*
 * Run 
 */
function signface_login () {
    var viewerId = FB.Connect.get_loggedInUser();
    document.getElementById('facebook-signin-id-input').setAttribute('value', viewerId);
    document.getElementById('facebook-signin-url-input').setAttribute('value', 'http://www.facebook.com/profile.php?id=' + viewerId);
    FB.Facebook.apiClient.users_getInfo(viewerId, ['first_name,pic_square'], function (x) {
	if (x[0] && x[0]['first_name']) {
	    var nickname = x[0]['first_name'];
	    document.getElementById('facebook-signin-nick-input').setAttribute('value', nickname);
	}
	document.getElementById('facebook-signin-form').submit();
    });
}
