FB.init({
appId : '129286603791590',
session : null, // don't refetch the session when PHP already has it
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// whenever the user logs in, we refresh the page
FB.Event.subscribe('auth.login', function() {
// Create the cookie to show the share this page on the next load
// This cookie will be deleted after the dialog is shown
// Run the session saver to save / update the users information in the database
saveFacebookData();
// set the expiration date
var expDate=new Date();
expDate.setDate(expDate.getDate());
// Create cookie
document.cookie = 'spa_suggest_friends = true';
});
// Catch when the user logs out
FB.Event.subscribe('auth.logout', function() {
window.location.reload();
});
// This dialog contains the facebook connect object and users image / name
jQuery('#facebook_container').ready(function(){
FB.getLoginStatus(function(response)
{
if(response.session)
{
FB.api('/me', function(response)
{
jQuery("#facebook_welcome_message").html("Welcome Back: " + response.name);
});
jQuery("#facebook_user_picture").attr("src", "http://graph.facebook.com/" + response.session.uid + "/picture");
jQuery("#facebook_login_button").html("Facebook Logout");
FB.XFBML.parse(document.getElementById('facebook_login_button'));
}
});
// Parse the hidden facebook Friend Suggest dialog
FB.XFBML.parse($("#share_with_friends_dialog")[0]);
// Create the previous voter message dialog object
jQuery("#share_with_friends_dialog").dialog({
modal: true,
width: 670,
height: 700,
zIndex: 7000,
closeonescape: true,
autoOpen: false,
draggable: false,
resizable: false,
open: function(event, ui) {
jQuery(".ui-dialog-titlebar-close").hide();
jQuery('#share_with_friends_dialog').parent().css('overflow', 'visible');
jQuery('#share_with_friends_dialog').prev().html('Share With Your Friends!
');
},
buttons: {
'Close': function() {
jQuery( this ).dialog( "close" );
}
}
});
//Check if the dialog has been shown before
if(!getCookie('spa_share-dialog-shown'))
{
// Check if the cookie exists to show the "Share this" dialog
if(getCookie('spa_suggest_friends'))
{
// Give the facebook stuff time to load
setTimeout('showShareDialog()', 1500);
}
}
});
// This is fired if the user has a cookie which is created right after they login.
function showShareDialog()
{
// Open the dialog
jQuery("#share_with_friends_dialog").dialog('open');
// Delete the cookie since the message has been shown
document.cookie = 'spa_suggest_friends=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
// Get get the date for next year
var now = new Date();
var expireDate = new Date(now.getFullYear() +1, now.getMonth(), now.getDate());
// Create a new cookie so the dialog wont be shown any more
document.cookie = 'spa_share-dialog-shown=true;expires='+ expireDate +';path=/';
}
// Function will retreive the passed cookies name
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1)
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}
function saveFacebookData()
{
// Pass the information off to the ajax handler
jQuery.ajax({
type: 'POST',
dataType: 'html',
url: '/ajax/save-facebook-data',
data: {userHash: userHash},
success: function(){
window.location.reload();
}
});
}