/*
 * FWB aka Friends with Benefits
 * http://developers.facebook.com/docs/reference/javascript/
 */

(function() {
    var e = document.createElement('script');
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    e.async = true;
    document.getElementById('fb-root').appendChild(e);
}());

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.init({
            appId   : this.api_key,
            status  : true, // check login status
            cookie  : true, // enable cookies to allow the server to access the session
            xfbml   : true,  // parse XFBML
            logging : this.debug_enabled
        });

        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.login(function(response) {
            if (response.session) {
                FWB.on_connected(response.session);
            } else {
                FWB.on_not_connected();
            }
        });
        //{perms:'publish_stream'});
        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://developers.facebook.com/docs/reference/javascript/FB.ui
     * 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.ui(
            {
                method: 'stream.publish',
                attachment: attachment,
                user_prompt_message: FWB.stream.user_message_prompt
            },
            FWB.on_share
        );
        return false;
    },

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

    /*
     * ref: http://developers.facebook.com/docs/reference/javascript/FB.ui
     * renders a facebook share page for the user (the simple popup one)
     *
     */
    share_simple: function(event) {
        var url = event.data.url || document.location;
        var share = {method: 'stream.share', u: url};
        FB.ui(share, FWB.on_share_simple);
        return false;
    },

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