POKE.facebook = function () {

    var init = function () {
        console.log('POKE.facebook.init: ' + POKE.settings.FACEBOOK_API_KEY + ' XD: ' + POKE.settings.FACEBOOK_XD_RECEIVER);
        FB.init(POKE.settings.FACEBOOK_API_KEY, POKE.settings.FACEBOOK_XD_RECEIVER, {"doNotUseCachedConnectState": true});
    };

    POKE.init.add_on_dom_ready(init);
    
    return {
        connect : function () {
            FB.Connect.requireSession(function(){
                console.log('connected!');
            }, false);
        },
        get_extras : function () {
            FB.Connect.showPermissionDialog('offline_access, publish_stream', function(rsp){
                console.log(rsp);
            });
        },
        share : function (published_song_pk) {
            $.getJSON('/api/get_facebook_embed_code_for_published_song/'+published_song_pk+'/', {}, function(data){
                FB.Connect.streamPublish(data.user_message, data.attachment, data.action_links, null, data.user_message_prompt, function(){
                    console.log('Share complete');
                });
            });
        },  
        invite_friends : function () {
             window.open ("http://"+POKE.settings.SITE_DOMAIN+'/social/fb/invite/open/',"mywindow","location=1,status=1,scrollbars=1,width=830,height=600");
        },

        _pop_invite_friends_dialog : function (action_url, currentFriendIds) {
            var fbml_content = "<fb:fbml><fb:request-form action=\"http://"+POKE.settings.SITE_DOMAIN+"/social/fb/invite/close/\"";
        	fbml_content += " method=\"POST\" invite=\"true\" type=\"Orange RockCorps\" content=\"";
        	fbml_content += "Create a unique Sound of Orange Rockcorps track with me. Go and record your sound now.";
            fbml_content += "<fb:req-choice url='http://"+POKE.settings.PARENT_DOMAIN+POKE.settings.PARENT_URL_PREFIX+"' label='Go make music' />\"";
        	fbml_content += "<fb:multi-friend-selector rows=\"2\" condensed=\"false\" bypass=\"cancel\"";
            if (currentFriendIds !== undefined && currentFriendIds !== null && currentFriendIds.length > 0) {
             var excludeIds = "";
             for (var i=0; i<currentFriendIds.length; i++) {
                 if (i > 0) {
                     excludeIds += ",";
                 }
                 excludeIds += currentFriendIds[i];
             }
             fbml_content += " exclude_ids=\"" + excludeIds + "\"";
            }
        	fbml_content += " showborder=\"false\" actiontext=\"Invite your friends to The Sound of Orange RockCorps\">";
        	fbml_content += "</fb:request-form></fb:fbml>";

        	var dialog = new FB.UI.FBMLPopupDialog('Invite Friends');
        	dialog.setFBMLContent(fbml_content);
        	dialog.setContentWidth(775);
        	dialog.setContentHeight(530);
        	//dialog.set_placement(FB.UI.PopupPlacement.topCenter);
        	dialog.show();
        }
        
    };
    
}();
