// Title: TWOFace.js
//  Tiger Woods PGA TOUR Online / Facebook Integration API
//
//  Copyright:
//      (c) 2009-2010 Electronic Arts Tiburon. All Rights Reserved.
//
//  Original Author:
//      Michael Behan
//
//  Dependencies:
//      o jQuery
//      o <globals.js>
//      o <modal_queue.js>
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// Class: TWOFace
//  A singleton class that implements the API between
//  Tiger Woods PGA TOUR Online and Facebook.
var TWOFace = ( function () {
    // Constants
    var   DEBUG_MODE                  = 'on' // 'on' or 'off' for verbose or
                                             // no debug output
        , BRAND                       = 'Tiger Woods PGA TOUR&reg; Online'
        , CHANNEL_PATH                = '/xd_receiver.htm'
        , LOGOUT_URI                  = '/users/logout?surl=' + document.location
        , DEFAULT_FEATURES            = ['CanvasUtil', 'Api', 'Connect', 'XFBML']
        , PUBLISH_IMAGE               = 'http://' + location.host + '/img/facebook/publish2.gif'
        , PUBLISH_HREF                = 'http://' + location.host + '/?sourceid=Facebook'
        // DOM elements:
        , LOGOUT_BUTTON               = '#outlog'
        , LOGOUT_BUTTON2              = '#logout'
        , AVATAR_IMAGE                = '.profile_image img:first'
        , AVATAR_IMAGE_SIZE           = {width: 75, height: 70}
        , PERSONA_NAME                = '.my_persona'
        , PERSONA_BREADCRUMB          = '#labelPlayersPersonaName'
        , ACTION_CONTAINER            = '.fb_connect'
        , ACTION_BUTTON_CONNECTED     = '.fb_connect .invite'
        , ACTION_BUTTON_DISCONNECTED  = '.fb_connect .connect'

        // Regex tests:
        , NOT_VIEWING_OWN_PROFILE     = /profiles/.test(location.href)

        // Private variables:
        , api_key                     = ''
        , api                         = {}; // will be initialized to the Facebook api


    // Method: report
    //  Uses FireBug's console object to report information.
    //  This will only work if <TWOFace>.DEBUG_MODE is set to 'on'.
    //
    //  Parameters:
    //      charType - String; 'i' for info, 'w' for warning, 'e' for error.
    //      msg      - String; Console message to display.
    //
    // Scope: private
    var report = function (charType, msg) {
        // Fail gracefully under the following conditions:
        if ( (!msg)
             || (!window.console || !window.console.log)
             || (DEBUG_MODE !== 'on')
           ) { return; }

        var func = undefined;

        // Set appropriate output function depending on charType's value:
        switch (charType) {
           case 'i':
               func = console.info;
               break;
           case 'w':
               func = console.warn;
               break;
           case 'e':
               func = console.error;
               break;
           default:
               func = console.log;
        }

        func(msg);
    };

    // Logout of both Facebook and TWO.
    var logout = function (e) {
        if (e) { e.preventDefault(); }
        var url = location.protocol + '//' + location.host + (location.port.length > 0 ? (':' + location.port) : '') + LOGOUT_URI;

        FB.Connect.ifUserConnected(function () { FB.Connect.logoutAndRedirect(url); }, logoutOfSite );
    }

    // Method: parseElement
    //  Parses an (X)FBML element.
    //
    // Parameters:
    //  el - DOMElement
    var parseElement = function (el) {
        if (el !== null) {
            FB.XFBML.Host.parseDomElement(el);
        }
    };

    // Method: parseDom
    //  Parses (X)FBML tags in DOM tree.
    var parseDom = function () {
        FB.XFBML.Host.parseDomTree();
    }

    // Method: updateProfileActions
    //  Updates the profile action buttons section based on whether
    //  or not the user is connected to Facebook.
    var updateProfileActions = function () {

        if (NOT_VIEWING_OWN_PROFILE) {
            $(ACTION_CONTAINER).remove();
            return;
        }

        // explicityl start with disconnected state shown, since now the not connected state arg never seems to get called with this legacy api :-(
        $(ACTION_BUTTON_DISCONNECTED).show();
        $(ACTION_BUTTON_CONNECTED).hide();
        FB.Connect.ifUserConnected(
            // Connected:
            function ()  {
                $(ACTION_BUTTON_CONNECTED).show();
                $(ACTION_BUTTON_DISCONNECTED).hide();
            },

            // Not Connected:
            function () {
                $(ACTION_BUTTON_DISCONNECTED).show();
                $(ACTION_BUTTON_CONNECTED).hide();
            }
        );
    };

    // Method: useAvatar
    //  Updates avatar images with Facebook avatar
    var useAvatar = function () {
        var fbml = '<fb:profile-pic uid="loggedinuser" width="' + AVATAR_IMAGE_SIZE.width.toString() + '" height="' + AVATAR_IMAGE_SIZE.height.toString() + '"></fb:profile-pic>';
        $(AVATAR_IMAGE).replaceWith(fbml);
        parseDom();
    }

    // Method: usePersona
    //  Updates the persona name in the player card with the user's name
    //  is it appears in Facebook.
    var usePersona = function () {
        var fbml = '<fb:name uid="loggedinuser" useyou="false" linked="false"></fb:name>';
        var el1  = $(PERSONA_NAME);
        var el2  = $(PERSONA_BREADCRUMB);

        el1.html(fbml);

        // Update persona name in breadcrumb if it exists.
        if (el2.length > 0) {
            el2.html(fbml);
        }

        parseDom();
    }

    // Method: popupThankYou
    //  Callback for a successful connection to Facebook. Pops up a
    //  "Thank You" modal to the user. Use for the onlogin event of a
    //  fb:login-button element.
    //
    // Parameters:
    //  e - Event object
    //
    // See Also:
    //  o <modal_queue.js>
    var popupThankYou = function (e) {
        if (e) { e.preventDefault(); }
        show_modal('fb_thankyou'); // see modal_queue.js
        $('#fb_thankyou .left_column img').bind('mouseup', popupInviteFriends);
    };

    // Method: getInviteFriendsXFBML
    //  Returns XFBML used on the Invite Friends popup page.
    //
    // See Also:
    //  o <modal_queue.js>
    var popupInviteFriends = function ()  {
        close_modal(); // see modal_queue.js
        window.open('/facebook/popupinvitefriends', 'fb_InviteFriends', 'height=690,width=630,toolbar=no,location=no,scrollbars=no,menubar=no,status=no,resizable=no');
    };

    // Method: publish
    //  Publishes to a user's wall on behalf of the application.
    var publish = function (message) {
        //if (e) { e.preventDefault(); }

        if (typeof(message) === 'undefined') {
            message = '';
        }

        ModalQueue.showNextModal();

        FB.Connect.requireSession( function () {
            var uid  = FB.Connect.get_loggedInUser();
            var api  = FB.Facebook.apiClient;
            var name = null;

            api.fql_query('SELECT name FROM user WHERE uid=' + uid.toString(), function(rows) {
                name = rows[0].name;

                // Replace any occurrences of [#name#] in the message
                // with the Facebook username.
                message = message.replace(/\[#name#\]/g, name);
                  
                var attachment = {   'media': [{   'type':  'image'
                                                 , 'src':  PUBLISH_IMAGE
                                                 , 'href': PUBLISH_HREF
                                              }]
                                   , 'name':        BRAND
                                   , 'href':        PUBLISH_HREF
                                   //, 'description': 'Sign up for the beta now!'
                                   , 'caption': message
                                 };
                var actionLinks = [{'text': 'Play Now!', 'href': PUBLISH_HREF}];

                FB.Connect.streamPublish('', attachment, actionLinks);
            });
        });
    };

    // Method: init
    //  This initializes the Facebook API and performs setup of the TWOFace
    //  functionality.
    //
    // Parameters:
    //      api_key  - Your Facebook API key. Looks for global variable >facebookKey
    //                 by default.
    //      features - Array; array of Facebook features to include.
    //
    // Returns:
    //      boolean
    var init = function (api_key, features) {

        if (!api_key && !facebookKey) {
            report('e', 'Could not find an api key to connect with.')
            return false;
        }

        if (!api_key) {
            api_key = facebookKey;
        }

        FB_RequireFeatures((typeof(feaures) === 'object' ? features : DEFAULT_FEATURES),
            function () {
                FB.init(api_key, CHANNEL_PATH);

                updateProfileActions();

                $(LOGOUT_BUTTON).replaceWith('<a href="#" onclick="TWOFace.logout();" class="UserButton_text">Log Out</a>');
                $(LOGOUT_BUTTON2).replaceWith('<a href="#" onclick="TWOFace.logout();">Log Out</a>');
            }
        );

        return true;
    };

    // >>>
    // Expose public methods
    // >>>
    return {
          init: init
        , logout: logout
        , parseDom: parseDom
        , parseElement: parseElement
        , popupInviteFriends: popupInviteFriends
        , popupThankYou: popupThankYou
        , publish: publish
        , useAvatar: useAvatar
        , usePersona: usePersona
        , updateProfileActions: updateProfileActions
    };

}) ();

