/*
 * FWB aka Friends with Benefits 
 */

var FWB = {
    api_key : "",
    xd_receiver : "",
    debug_enabled : false,
    stream : {
        default_attachment : {
            "name" : document.title,
            "href" : document.location,
            "comments_xid" : encodeURIComponent(document.location),
        },
        user_message_prompt : "What do you think?",
        auto_publish : false
    },

    init: function(api_key, xd_receiver, debug_enabled) {
        this.api_key = api_key;
        this.xd_receiver = xd_receiver;
        this.debug_enabled = debug_enabled || false;

        // add the checkbox plugin hooks to jquery.
        jQuery.fn.check = function(mode) {
                // if mode is undefined, use 'on' as default
                var mode = mode || "on";
                return this.each(function() {
                switch(mode) {
                    case "on":
                        this.checked = true;
                        break;
                    case "off":
                        this.checked = false;
                        break;
                    case "toggle":
                        this.checked = !this.checked;
                        break;
                }
            });
        };

        FB.Bootstrap.requireFeatures(["Connect", "Api", "Comments", "XFBML"], function(){
            FB.Facebook.init(
                    FWB.api_key,
                    FWB.xd_receiver,
                    {
                        "ifUserConnected" : FWB.on_connected,
                        "ifUserNotConnected" : FWB.on_not_connected
                    }
                );
            if (FWB.debug_enabled) {
                FB.FBDebug.logLevel = 6;
                FB.FBDebug.isEnabled = true;
            }
        });
        
        this.on_ready();
    },

    /*
     * simple little debugger simply alerts
     * the message text right now.
     *
     */
    debug: function(msg) {
        if (!FWB.debug_enabled) return;
        alert(msg);
    },

    /*
     * announced when the FWB object is ready.
     *
     */
    on_ready: function() {
        FWB.debug("FWB_READY");
        jQuery(document).trigger("FWB_READY");
    },

    /*
     * forces a user to login and connect
     * to our facebook app
     *
     */
    connect: function() {
        FB.Connect.requireSession();
        return false;
    },

    /*
     * gets called when the user successfully logs
     * into/connects on facebook from a login box
     *
     */
    on_connected: function(user_id) {
        FWB.debug("FWB_CONNECTED :: user_id :: " + user_id);
        jQuery(document).trigger("FWB_CONNECTED");
    },

    /*
     * gets called when the user's connect status
     * changes to logged out or they disconnect
     * from the app.
     *
     */
    on_not_connected: function() {
        FWB.debug("FWB_NOT_CONNECTED");
        jQuery(document).trigger("FWB_NOT_CONNECTED");
    },

    /*
     * ref: http://wiki.developers.facebook.com/index.php/FB.Connect.streamPublish
     * this function magically makes sure the user is logged in and according
     * to facebook doesn't even require the user to connect to our app.  simple.
     *
     */
    share: function(event) {
        var attachment = event.data.attachment || FWB.stream.default_attachment;
        FB.ensureInit(function(){
            FB.Connect.streamPublish('', attachment, null, null, FWB.stream.user_message_prompt, FWB.on_share, FWB.stream.auto_publish);
        });
        return false;
    },

    /*
     * gets called after the user shares something on facebook
     *
     */
    on_share: function(post_id, exception) {
        if (post_id) {
            FWB.debug("FWB_SHARED :: post_id :: " + post_id);
            jQuery(document).trigger("FWB_SHARED");
            return;
        }
        FWB.debug("FWB_SHARE_FAILED :: exception :: " + exception);
    },

    /*
     * ref: http://wiki.developers.facebook.com/index.php/JS_API_M_FB.Connect.ShowShareDialog
     * renders a facebook share page for the user (the simple popup one)
     *
     */
    share_simple: function(event) {
        var url = event.data.url || document.location;
        FB.ensureInit(function(){
            FB.Connect.showShareDialog(url);
        });
        return false;
    },

    /*
     * gets called after the user shares something on facebook
     * simple version
     *
     */
    on_share_simple: function() {
        FWB.debug("FWB_SHARED_SIMPLE");
        jQuery(document).trigger("FWB_SHARED_SIMPLE");
    }
};