﻿function social_load(api_key) {
    twttr.anywhere(function (T) {
        if (T.isConnected())
            twttr.anywhere.signOut();

        document.getElementById("signin-btn").onclick = function () {
            T.signIn();
            return false;
        };

        T.bind("authComplete", function (e, user) {
            // triggered when auth completed successfully
            currentUser = user;
            screenName = currentUser.data('screen_name');
            // use screen_name to social login
            document.location.href = "/socialprocess/TwitterAuth/" + screenName;
            twttr.anywhere.signOut();
        });

        T.bind("signOut", function (e) {
            // triggered when user logs out
        });
    });

    //END TWITTER REGION
    //BEGIN FACEBOOK REGION
    var uid;
    FB.init({ apiKey: api_key });

    $('#SigOnWithFB').bind('click', function () {
        FB.login(handleSessionResponse, { perms: 'publish_stream,offline_access,email' });
    });

    // handle a session response from any of the auth related calls
    function handleSessionResponse(response) {

        // if we dont have a session, just hide the user info
        if (!response.session) {
            //clearDisplay();                
            return;
        }

        // if we have a session, query for the user's profile picture and name
        FB.api({ method: 'fql.query', query: 'SELECT id,name,pic FROM profile WHERE id=' + FB.getSession().uid }, function (response) {
            var user = response[0];
            uid = user.id;
            document.location.href = "/socialprocess/FacebookAuth/" + uid;
        });
    }

    FB.getLoginStatus(function (response) {
        if (response.session) {
            // logged in and connected user, someone you know
            FB.api({ method: 'fql.query', query: 'SELECT id,name,pic FROM profile WHERE id=' + FB.getSession().uid }, function (response) {
                var user = response[0];
                uid = user.id;
                $('#FBUid').val(user.id);
            });
        } else {
            // no user session available, someone you dont know
        }
    });

    //END FACEBOOK REGION
}
