

/* ##### facebookConnectLoginCheckV2() ##### */
function facebookInitAndLoginCheck( facebookAppID )
{
    window.fbAsyncInit = function() {
        FB.init({appId: facebookAppID, status: true, cookie: true, xfbml: true, oauth : true});
        facebookConnectLoginCheckV2();
    };
    (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
    }());
}


/* ##### facebookConnectLoginCheckV2() ##### */
function facebookConnectLoginCheckV2()
{
	try
	{
	    FB.getLoginStatus(function(response) 
	    {
	        //if (response.session) 
	    	if (response.status === 'connected')
	    	{
 			    // This function is defined in widget.js file.
		        determinedInitialFacebookConnectLoginStatus( response.authResponse.userID );
	        } 
	        else 
	        {
	    		determinedInitialFacebookConnectLoginStatus( "notLoggedIn" );
	        }
	    });
	}
	catch ( error1 )
	{
		determinedInitialFacebookConnectLoginStatus( "notLoggedIn" );
	}
}


/* ##### connectToFacebook() ##### */
function connectToFacebook()
{
	FB.login(function(response) 
    {
		//if (response.session) 
		if (response.authResponse)
        {
			// User logged in - this function defined in widget.js file.
			loginEnsured( response.authResponse.userID );
		} 
		else 
		{
		    // User canceled login - no need to do anything.
		}
	});
}


/* ##### connectToFacebook() ##### */
function connectToFacebookWithPermissions( permissions )
{
	// NOTE: Ignore the passed in parameters for now, since the "perms" string needs some work.
	FB.login(function(response) 
    {
		//if (response.session) 
		if (response.authResponse)
        {
			// User logged in - this function defined in widget.js file.
			loginEnsured( response.authResponse.userID );
		} 
		else 
		{
		    // User canceled login - no need to do anything.
		}
	}, { scope: 'publish_stream,email,user_birthday' } );
}


/* ##### logoutFromFacebook() ##### */
function logoutFromFacebook()
{
	FB.logout(function(response) 
    {
		// This function defined in widget.js file.
		userLoggedOut();
    });	
}


/* ##### postFeedOnFeedTypeBasis() ##### */
function postFeedOnFeedTypeBasis( gameName, appDeepLinkPath, gameUrl, gameDesc, origin, name, caption, 
		                          userPrompt, actionLinkName, appUrl, appFullName )
{
	 var gameLink =  appUrl + '/' + appDeepLinkPath + origin;
    
	 name = name.replace( 'GAME_NAME', gameName );
	 name = name.replace( 'APPLICATION_FULL_NAME', appFullName );
    
	 caption = caption.replace( 'GAME_NAME', gameName );
	 
	 // replace all instances (there are 2 of SITE_LOCATION)
	 caption = caption.replace( 'APPLICATION_FULL_NAME', appFullName );
	 
	 FB.ui( {
			   method: 'feed',
			   name: name,
			   link: gameLink,
			   picture: gameUrl,
			   caption: caption,
			   description: gameDesc,
			   message: '',
			   action_links: [ { text: actionLinkName, href: gameLink } ]
			},
			function(response) 
			{
			     if (response && response.post_id) 
			     {
			       // Post published, nothing for use to do ... for now.
			     }
			     else 
			     {
			       // Post not published, something went wrong.
			     }
			} );
}


/* ##### inviteFriends() ##### */
function inviteFriends( widgetSiteName, appFullName )
{
	var requestTitle = 'Invite friends to the ' + appFullName;
	if ( requestTitle.length > 50 )
	{
		requestTitle = requestTitle.substring( 0, 46 ) + '...';
	}
	
    FB.ui( {
		   method: 'apprequests',
		   title: requestTitle,
		   message: 'Join the ' + appFullName + ' and discovery great apps!',
		   data: appFullName + ' Invite Friends Request'
		}, function(response) {} );
}


/* ##### inviteFriends_customPartner() ##### */
function inviteFriends_customPartner( siteLink, partnerSiteName, encodedSiteLink, apiKey )
{
	var requestTitle = 'Invite friends to the ' + partnerSiteName;
	if ( requestTitle.length > 50 )
	{
		requestTitle = requestTitle.substring( 0, 46 ) + '...';
	}
	
    FB.ui( {
		   method: 'apprequests',
		   title: requestTitle,
		   message: 'Join the ' + partnerSiteName + ' and discovery great apps!',
		   data: partnerSiteName + ' Invite Friends Request'
		}, function(response) {} );
}


/* ##### inviteFriends_apptap() ##### */
function inviteFriends_apptap( widgetSiteName, siteLink )
{
	var requestTitle = 'Invite friends to the ' + widgetSiteName;
	if ( requestTitle.length > 50 )
	{
		requestTitle = requestTitle.substring( 0, 46 ) + '...';
	}
	
    FB.ui( {
		   method: 'apprequests',
		   title: requestTitle,
		   message: 'Join the ' + widgetSiteName + ' and discovery great apps!',
		   data: widgetSiteName + ' Invite Friends Request'
		}, function(response) {} );
}


/* ##### postShareCollection() ##### */
function postShareCollection( userID, origin, name, caption, userPrompt, actionLinkName, 
		                      appUrl, widgetSiteName, appFullName, logoUrl )
{
	var imageUrl = null;
	if ( logoUrl == null )
	{
	    imageUrl = appUrl + "/images/" + widgetSiteName + "/logo.png";
	}
	else
	{
	    imageUrl = logoUrl;
	}
	var userDeepLink =  appUrl + '/?userdetail=' + userID + '&context=collection&origin=' + origin;

	name = name.replace( 'APPLICATION_FULL_NAME', appFullName );
	caption = caption.replace( 'APPLICATION_FULL_NAME', appFullName );
	
	 FB.ui( {
		   method: 'feed',
		   name: name,
		   link: userDeepLink,
		   picture: imageUrl,
		   caption: caption,
		   description: '',
		   message: '',
		   action_links: [ { text: actionLinkName, href: userDeepLink } ]
		},
		function(response) 
		{
		     if (response && response.post_id) 
		     {
		       // Post published, nothing for use to do ... for now.
		     }
		     else 
		     {
		       // Post not published, something went wrong.
		     }
		} );
}

