﻿function initializeFacebookLibrary() {

    FB.init({
        appId: facebookClientID,
        status: true,
        cookie: true,
        xfbml: true
    });
}

function FacebookButton_onLogin() {
    FB.getLoginStatus(function(response) {
        ProcessFacebookLogin(response, false);
    });
}

function OpenFacebookLogin(event, populateLinkedFields) {
    if (event != null) {
        event.preventDefault();
    }
    FB.login(function(response) {
        ProcessFacebookLogin(response, populateLinkedFields)
    }, { perms: 'publish_stream,email' });
}

function ProcessFacebookLogin(response, populateLinkedFields) {
    if (response.session) {
        var url = window.location.href;

        url = AddQueryStringItem(url, 'fblogin', 'true');

        if (populateLinkedFields) {
            url = AddQueryStringItem(url, 'fbpopulate', 'true');
        }

        window.location.href = url;
    }
}

function FacebookLogout() {
    // N.B. Not using FB.logout as this logs the user out of Facebook.com as well as the current site,
    // which is no good if the user had selected "Keep me logged in" when signing in to Facebook.com
    // Haven't found a way of just logging the user out of the current site.
    //FB.logout(function(response) {
    //});
}

function AddQueryStringItem(url, key, value) {

    if (url.indexOf(key + '=') == -1) {
        if (url.indexOf('?') == -1) {
            url += '?';
        }
        else {
            url += '&';
        }
        url += key + '=' + value;
    }

    return url;
}

//// **************************************************************************************************************
//// The following functions have been removed and mainly replaced with server side parsing of the Facebook cookie
//// **************************************************************************************************************

//function subscribeToLoginEvent() {
//    FB.Event.subscribe('auth.login', function(response) {
//        processLogin(response, false, true);
//    });
//}

//function attemptAutoLogin() {
//    FB.getLoginStatus(function(response) {
//        processLogin(response, false, true);
//    });
//}

//function openFacebookLogin(event, populateLinkedFields) {
//    if (event != null) {
//        event.preventDefault();
//    }
//    FB.login(function(response) {
//        processLogin(response, populateLinkedFields, populateLinkedFields);
//    }, { perms: 'publish_stream,email' });
//    //window.open('http://graph.facebook.com/oauth/authorize?client_id=' + facebookAppID + '&redirect_uri=http://' + currentURL + '/OneStopCMS/Core/facebookaccountlink.aspx&type=user_agent&display=popup&scope=publish_stream,create_event,user_photos,offline_access,email', '', 'status=1,width=500, height=500');
//}

//function processLogin(response, populateLinkedFields, reloadPage) {
//    if (response.session) {
//        cookiePresent = false;
//        if (document.cookie.length > 0) {
//            var parts = [];
//            parts = document.cookie.split('; ');
//            for (i = 0; i < parts.length; i++) {
//                if (parts[i] == 'fb_' + facebookClientID + '_userID=' + response.session.uid) {
//                    cookiePresent = true;
//                    break;
//                }
//            }
//        }
//        if (!cookiePresent) {
//            document.cookie = 'fb_' + facebookClientID + '_userID=' + response.session.uid;
//            document.cookie = 'fb_' + facebookClientID + '_accessToken=' + response.session.access_token;
//        }
//        document.cookie = 'fb_' + facebookClientID + '_populateLinkedFields=' + populateLinkedFields;
//        if (reloadPage) {
//            window.location.reload();
//        }
//    }
//    else {
//        // The user has logged out, and the cookie has been cleared
//        document.cookie = 'fb_' + facebookClientID + '_userID=;';
//        document.cookie = 'fb_' + facebookClientID + '_accessToken=;'
//    }
//}
//// **************************************************************************************************************
