// Copyright (c) 2009 Tiny Pictures, Inc.

//== Functions ================================================================

/** Reveals Facebook Connect functionality when it's available. */
function fbc_onEnable() {
    FB.ensureInit(function() {
        //-- Remove all fbc alternative elements
        $j('.fbc_alternative').hide();

        //-- Reveal all fbc elements
        $j('.fbc_enabled').slideDownFade('slow');
    });
}

function fbc_onLogin(returnUrl) {
    if (returnUrl) {
        window.location = "/facebook/authorize/?returnUrl=" + returnUrl;
    }
    else {
        window.location = "/facebook/authorize/";
    }
}

/** Displays a feed dialog for a user to publish their comment to facebook. */
function fbc_showFeedDialogForComment(
        feedBundleId,
        postViewUrl, postTitle, postImgUrl,
        commentText,
        posterName, posterProfileUrl) {
    var userMessage = { value : '' };
    var templateData = {
        post_url        : postViewUrl,
        post_title      : postTitle,
        images:[
            {
                'src'   : postImgUrl,
                'href'  : postViewUrl
            }
            ],
        post_comment    : commentText,
        poster_name     : posterName,
        poster_profile_url : posterProfileUrl
    };

    // setTimeout (even with zero timeout) fixes showFeedDialog call from AJAX
    // in FF; because it prevents calls from "smashing into each other."
    // http://forum.developers.facebook.com/viewtopic.php?pid=155588
    setTimeout(
            function() {
                FB.ensureInit(function() {
                    FB.Connect.showFeedDialog(
                            feedBundleId,         // template_bundle_id
                            templateData,               // template_data
                            null,                       // target_id
                            null,                       // body_general
                            null,                       // deprecated story_size
                            FB.RequireConnect.promptConnect, // prompt if not connected
                            null,                       // callback
                            'Comment on your comment?', // user_message_prompt
                            userMessage);
                });
            }, 0);
} // end fbc_showFeedDialogForComment()