vanilla.facebook = {

    api_key : null,
    template_id : null,
    is_initialized : false,
    body_general : '',
    feed_template_data : {},
    story_url : '',
    
    ensure_init : function(callback) {
        if(!vanilla.facebook.api_key) {
            alert("api_key is not set");
        }

        if(vanilla.facebook.is_initialized) {
            callback();
        } else {
            FB_RequireFeatures(["XFBML", "CanvasUtil"], function() {
                FB.FBDebug.LogLevel = 4;
                FB.Facebook.init(vanilla.facebook.api_key, "/assets/facebook.html");
                vanilla.facebook.is_initialized = true;
                callback();
            });
        }
    },
    
    show_feed_checkbox : function() {
        vanilla.facebook.ensure_init(function() {
            FB.Connect.get_status().waitUntilReady(function(status) {
                if (status != FB.ConnectState.userNotLoggedIn) {
                    jQuery("#publish_fb_checkbox").show();
                    jQuery("#id_publish_to_facebook").attr("checked", "checked");
                    jQuery("#fb_login_button").hide();
                }
            });
        });
    },
    
    ask : function() {
        vanilla.facebook.ensure_init(function() {
            FB.Connect.showFeedDialog(
                vanilla.facebook.template_id,
                vanilla.facebook.feed_template_data,
                null /* target_id */,
                vanilla.facebook.body_general /* body general text */,
                null /* story_size - deprecated */,
                FB.RequireConnect.promptConnect
            );
        });
    }
};
