/**
 * Typeoneerror Facebook libraries and helper functions.
 *
 * @author     Benjamin Borowski <ben.borowski@typeoneerror.com>
 * @copyright  Copyright (c) Typeoneerror Studios http://typeoneerror.com
 * @version    $Id: facebook.js 262 2009-11-25 20:52:52Z ben $
 */
var Typeoneerror_Facebook = {

    /**
     * Name of swf id when Facebook is used in Flash
     *
     * @var string
     */
    swfID : "swf_id",

    /**
     * @see therunaround application
     *
     * The facebook_onload statement is printed out in the PHP. If the user's logged in
     * status has changed since the last page load, then refresh the page to pick up
     * the change.
     *
     * This helps enforce the concept of "single sign on", so that if a user is signed into
     * Facebook when they visit your site, they will be automatically logged in -
     * without any need to click the login button.
     *
     * @param alreadyLoggedIntoFacebook  Reports whether the server thinks the user
     *                                   is logged in, based on their cookies
     * @return void
     */
    doLoad : function(alreadyLoggedIntoFacebook)
    {
        // --user state is either: has a session, or does not.
        // -- if the state has changed, detect that and reload.
        FB.ensureInit(function()
        {
            FB.Facebook.get_sessionState().waitUntilReady(function(session)
            {
                var isNowLoggedIntoFacebook = session ? true : false;

                // -- if the new state is the same as the old (i.e., nothing changed)
                // -- then do nothing
                if (isNowLoggedIntoFacebook == alreadyLoggedIntoFacebook)
                {
                    return;
                }

                // -- otherwise, refresh to pick up the state change
                Typeoneerror_Facebook.doRefresh();
            });
        });
    },

    /**
     * Log user out of app and facebook
     *
     * @param string redirect  Location to head to after logout
     */
    doLogout : function(redirect)
    {
        if (!redirect || redirect == "")
        {
            redirect = "/logout";
        }

        // -- this doesn't work as of 6/18/9
        // FB.Connect.logout(function(){ window.location = "http://www.google.com"; });
        FB.Connect.logoutAndRedirect(redirect);
    },

    /**
     * Call flash loaded function
     */
    doFlashCallback : function(func)
    {
        // console.log(func, Typeoneerror_Facebook.swfID);
        // console.log(arguments);

        if (arguments.length > 1)
        {
            var args = Array.prototype.slice.call(arguments).slice(1);
            document[Typeoneerror_Facebook.swfID][func](args);
        }
        else
        {
            document[Typeoneerror_Facebook.swfID][func]();
        }
    },

    /**
     * Do login from flash
     */
    doFlashLogin : function()
    {
        FB.ensureInit(function()
        {
            // alert("attempt login");
            FB.Connect.requireSession(Typeoneerror_Facebook.doFlashLoginHandler);
        });
    },

    /**
     * Handles the login completetion
     */
    doFlashLoginHandler : function()
    {
        var s = FB.Facebook.apiClient.get_session();
        Typeoneerror_Facebook.doFlashCallback("onLogin", s.session_key, s.secret, s.uid);
    },

    /**
     * Reload the whole window
     */
    doRefresh : function()
    {
        window.location.reload();
    },

    /**
     * Set up facebook for flash
     */
    initForFlash : function(swfID)
    {
        Typeoneerror_Facebook.swfID = swfID;
    },

    /**
     * Re-parse the page (DOM)
     */
    parse : function()
    {
        FB.ensureInit(function()
        {
            FB.XFBML.Host.parseDomTree();
        });
    },

    /*
     * Prompts the user to grant a permission to the application.
     *
     * @param permission
     * @return void
     */
    promptPermission : function(permission, callback)
    {
        FB.ensureInit(function()
        {
            FB.Connect.showPermissionDialog(permission, callback);
        });
    },

    /**
     * Deprecated: Prompt the user with the chance to post a feed dialog
     *
     * @param int template                ID of template to publish
     * @param object data                 Json data to publish into template
     * @param string callback             Function to call when complete
     * @param string user_message_prompt  The label (which could be in the form of a question) that appears
     *                                    above the text box on the Feed form next to the Facebook-provided question, "What's on your mind?".
     * @param string user_message         Populate suggested text in the text box
     *
     * @return void
     *
     * @deprecated
     *
     * @see  http://wiki.developers.facebook.com/index.php/JS_API_M_FB.Connect.ShowFeedDialog
     * @see http://developers.facebook.com/docs/?u=facebook.jslib.FB.Connect.streamPublish
     */
    showFeedDialog : function(template, data, callback, user_message_prompt, user_message)
    {
        callback = callback || null;
        user_message_prompt = user_message_prompt || null;
        user_message = user_message || null;

        FB.ensureInit(function()
        {
            FB.Connect.showFeedDialog(template, data, null, null, null, FB.RequireConnect.doNotRequire, callback, user_message_prompt, user_message);
        });
    },

    /**
     * This method publishes a post into the stream on the Wall of a user or 
     * a Facebook Page, group, or event connected to the user. By default, this 
     * call publishes to the current session user's Wall, but if you specify a 
     * user ID, Facebook Page ID, group ID, or event ID as the target_id, then 
     * the post appears on the Wall of the target, and not the user posting the item. 
     *
     * @return void
     */
    streamPublish : function(user_message, attachment, action_links, target_id, user_message_prompt, callback, auto_publish, actor_id)
    {
        user_message = user_message || '';
        attachment = attachment || null;
        action_links = action_links || null;
        target_id = target_id || null;
        user_message_prompt = user_message_prompt || "What's on your mind?";
        target_id = target_id || null;
        auto_publish = auto_publish || false;
        actor_id = actor_id || null;

        FB.ensureInit(function()
        {
            FB.Connect.streamPublish(user_message, attachment, action_links, target_id, user_message_prompt, callback, auto_publish, actor_id);
        })
    }

}
