﻿
//  
//  Author: Kevin Burkitt
//  Date:   2010-01-26
//  
//  Facebook controller
//
cc.facebook = function() {

    //"private" variables:
    var CC_Username;

    // Init Facebook Features
    var init = function(callbacks) {

        FB_RequireFeatures(["XFBML"], function() {
            FB.Facebook.init(
				cc.FACEBOOK_API_KEY
				, "/xd_receiver.htm"
				, callbacks);
        });

    }

    function getInviteFBML() {
        var uid = FB.Facebook.apiClient.get_session().uid;
        var fbml = "";
        fbml =
		'<fb:fbml>\n' +
			'<fb:request-form\n' +
        //Redirect back to this page
								' action="' + document.location + '"\n' +
								' method="POST"\n' +
								' invite="false"\n' +
								' type="CandyCrib"\n' +
								' content="You should come and join me on CandyCrib.com' +
								' <fb:req-choice url=\'http://www.candycrib.com?utm_campaign=Facebook_Invite&utm_medium=Vistor_from_Invite_Link&utm_source=Facebook_Invite&utm_content=Invite_Owner_FB_UID_' + uid + '\' label=\'View CandyCrib\' />"\n' +
						  '>\n' +
						   ' <fb:multi-friend-selector\n' +
								' rows="2"\n' +
								' cols="4"\n' +
								' bypass="Cancel"\n' +
								' showborder="false"\n' +
								' actiontext="Invite your friends to connect with you on CandyCrib.com"/>\n' +
					' </fb:request-form>' +
			' </fb:fbml>';
        return fbml;
    }




    return {

        Init: function() {
            init();
        },


        DisplayInviteBox: function() {
            init({ "ifUserConnected": function() { }, "ifUserNotConnected": function() { } });

            FB.ensureInit(function() {

                FB.Connect.requireSession(function() {

                    FB.Facebook.get_sessionWaitable().waitUntilReady(function() {

                        var uid = FB.Facebook.apiClient.get_session().uid;

                        // Update server with connected account
                        // updateAccountFacebookUID();

                        var fbml = getInviteFBML();

                        var dialog = new FB.UI.FBMLPopupDialog("Invite Your Facebook Friends to CandyCrib.com", fbml);

                        dialog.setFBMLContent(fbml);
                        dialog.setContentWidth(650);
                        dialog.setContentHeight(450);
                        dialog.show();


                    });

                }, null, true);

            });
        }


        /*
        publishStreamMessage: function(username) {

            CC_Username = username;

            init();

            FB.ensureInit(function() {

                FB.Connect.requireSession(function() {

                    FB.Facebook.get_sessionWaitable().waitUntilReady(function() {

                        if (CC_Username == 'undefined') {
        alert('USERNAME UNDEFINED');
        }
                        
        FB.Connect.streamPublish(
        ''
        , { 'name': 'CandyCrib', 'href': 'http://www.candycrib.com/' + CC_Username, 'description': 'Go and Join CandyCrib' }
        , [{ "text": "My CandyCrib Profile", "href": 'http://www.candycrib.com/' + CC_Username}]
        , null
        , 'What do you think?'
        , function(post_id, exception) {
        // Insert Google tracking to show it was posted (or not, if post_id is null)
        }
        );

                    }, null, true);

                }, null, true);

            });


        }
        */

    };


} ();



/*******************************/
/** AUTOMATICALLY INITIALISE! **/
/*******************************/

cc.facebook.Init();

