/*
 * Movember Facebook Connect Implementation
 *
 * - Attempt to make a connection to Facebook via JS
 * - Detect a change in session status, if changes, present FB Dialog offering to login...
 *
 */


/**
 * Given a heap of params, attempt to post to the user's facebook stream
 *
 * name:        Will be displayed just before the preset message
 * nameLink:    If a URL, will turn 'name' into a clickable link to that URL
 * message:     Optional text to default the user message to (ie. the main content entry box)
 * description: Preset message that will be shown under the custom user-generated message
 * actionTitle: Will be shown after the Comment and Like links under the preset message
 * actionLink:  If a URL, will turn the actionTitle into a clickable link tot hat URL
 * userPrompt:  When the user needs to FB login, this will be shown in the prompt
 * caption:     Optional extra caption that appears between 'name' and 'description'
 */
function facebook_streamPublish(name, message, description, nameLink, actionTitle, actionLink, userPrompt, caption, picture){
    if (facebookShare)
    {
        FB.ui(
        {
            method: 'stream.publish',
            message: message,
            name: name,
            caption: caption,
            description: (description),
            link: nameLink,
            picture: picture,
            action_links: [
                { text: actionTitle, href: actionLink }
            ],
            user_prompt_message: userPrompt
        },
        function(response) {

        });
    }
    else
    {
        $('#facebook-share-lightbox-content').html(description);
        $.fancybox($('#facebook-share-lightbox').html(),fancyboxSettings);
    }
}

/***
 * This function is attached to FB.Event.Subscribe(auth.login) event
 **/
function fb_login(){
    // when user logs in from fb login button check for whether member is connected to our application or not
    // if user is connected direct user to auth/connect to log him/her in
    // or user is trying to register via facebook

   FB.getLoginStatus(function (response)
   {
        if ( 'connected' == response.status )
            window.location.href = sBaseUrl + 'auth/connect'
   });

}

/***
 * This function is attached to FB.Event.Subscribe(auth.logout) event
 **/
function fb_logout(){

}


function fb_greet(){
   FB.api('/me', function(response) {
      alert('Welcome, '+response.name+"!");
   });
}


/**
 * Prompt the user to share their Donate URL and optional message onto their
 * facebook stream
 *
 * @param data  Array of options to pass through to Facebook Share prompt:
 */
function facebook_requestFunds( data )
{
    facebook_streamPublish( data.name, data.message, data.description, data.nameLink, data.actionTitle, data.actionLink, data.prompt, '');

    return;
}

/**
 * Prompt the user to share their Team details asking friends to join their team
 *
 * @param data  Array of options to pass through to Facebook Share prompt:
 */
function facebook_teamInvite( data )
{
    facebook_streamPublish( data.name, data.message, data.description, data.nameLink, data.actionTitle, data.actionLink, data.prompt, '');

    return;
}

/**
 * Prompt the user to share the fact that they have just made a donation
 *
 * @param data  Array of options to pass through to Facebook Share prompt:
 */
function facebook_justDonated( data )
{
    facebook_streamPublish( data.name, data.message, data.description, data.nameLink, data.actionTitle, data.actionLink, data.prompt, '');

    return;
}

/**
 * Prompt the user to share the fact that they have just bought a ticket to a Gala Parte
 *
 * @param data  Array of options to pass through to Facebook Share prompt:
 */
function facebook_attendGalaParte( data )
{
    facebook_streamPublish( data.name, data.message, data.description, data.nameLink, data.actionTitle, data.actionLink, data.prompt, '');

    return;
}

/**
 * Prompt the user to share the fact that they have just created a custom poster
 *
 * @param data  Array of options to pass through to Facebook Share prompt:
 */
function facebook_sharePosterData( data )
{
	facebook_streamPublish( data.name, data.message, data.description, data.nameLink, data.actionTitle, data.actionLink, data.prompt, '', data.picture);
	
	return;
}

/**
 * Prompt the user to share a Movember Fact
 *
 * @param data  Array of options to pass through to Facebook Share prompt:
 */
function facebook_shareFact( data )
{
    facebook_streamPublish( data.name, data.message, data.description, data.nameLink, data.actionTitle, data.actionLink, data.prompt, '');

    return;
}


function facebook_revokeAuthorization()
{
    FB.api({ method: 'Auth.revokeAuthorization' }, function(response) {
        window.location = sBaseUrl + 'auth/revoke-facebook-authorisation';
    });
}



function facebook_connected( fbUser )
{
    fbConnected = true;

// These header items no longer required
//
//    if( $.cookie( ckName+'_id' ) && $.cookie( ckName ) )
//    {
//        $( "#auth_table-fb-who" ).show();
//        $( "#auth_table-fb-icon" ).show();
//        $( "#auth_table-fb-spacer" ).show();
//    } else {
//        $( "#auth_table-fb-who" ).show();
//        $( "#auth_table-fb-icon" ).show();
//        $( "#auth_table-fb-spacer" ).show();
//        $( "#auth_table-fb-disconnect" ).show();
//        $( "#auth_table-fb-disconnect-spacer" ).show();
//    }
}

/**
 * If facebook is connected, show the conencted box
 */
//function facebook_updateHeader()
//{
//    fbConnected = true;
//
//    $( "#auth_table-fb-who" ).show();
//    $( "#auth_table-fb-icon" ).show();
//    $( "#auth_table-fb-spacer" ).show();
//
//    // If not logged in, let the user disconnect from facebook if they already
//    // happen to be connected
//    if( $.cookie( ckName+'_id' ) && $.cookie( ckName ) )
//        ;
//    else
//    {
//        $( "#auth_table-fb-disconnect" ).show();
//        $( "#auth_table-fb-disconnect-spacer" ).show();
//    }
//}

function facebook_login()
{
    facebook_isLoggedIn();
    window.location.reload();
}

function facebook_isLoggedIn()
{
//    facebook_updateHeader();
}

function facebook_logout()
{
    window.location.reload();
}

function facebook_toggleDisplayAllConnectedFriends()
{
    $(".hiddenFBFriends").toggle();
    $("#facebook_showAllLink").toggle();
}



