function fbLoginStatusChanged(response) {
    $.ajax({
        url: "ajax",
        type: "POST",
        data: {
            fbLoginStatusChanged: JSON.stringify(response)
        },
        dataType: "json",
        async: true,
        success: function (response) {
	    var fbadmin=false;
	    if(response){
		fbadmin= response.fbadmin ? true : false;
	    }
            if (response) {
                $("#centercontainer").data("fbadmin", fbadmin);
            }
            else {
                $("#centercontainer").data("fbadmin", false);
            }
            if (response && response.accesstoken) {
                var logins = response.numlogins;
                FB.api('/me?fields=first_name,picture', function (response) {
                    $("#fbadminimg").css("width", "2em").attr("src", response.picture);
                    $("#fbconnecttext").empty();
                    if (response.numlogins == 1) {
                        alert(window.firstlogintext.replace("%s", response.first_name));
                    }
                    enableEditing(fbadmin);
                });
            }
            else {
                $("#fbadminimg").attr("src", "common-images/fb-logged-in");
                enableEditing(false);
                $('#fb-like').fadeOut(3000, function () {
                    $(this).empty().append(($(window.pagelike)));
                    FB.XFBML.parse($(this).is());
                    $(this).fadeIn(3000, function () {});
                });
                $("#fbconnecttext").html("Goodbye...").show().fadeOut(1000, function () {
                    $(this).hide().html(window.fbconnecttext).fadeIn(2000);
                });
            }

        }

    });

}

function fbCreateAction(action,object,url,starttime,durationmins,place){

    log("creating new FB action (action,object,url,starttime,duration)",action,object,url,starttime,durationmins);
    var d = new Date(starttime);

    var curtime= new Date();
    var expires= Math.round(starttime!==null?(d-curtime)/1000+(durationmins*60):null);
    log("duration:"+durationmins+",startdate:"+d+",curdate:"+curtime+",expires:"+expires);
    var commandurl='/me/'+og_namespace+':'+action+'?start_time='+ISODateString(curtime)+'&place='+place+'&expires_in='+expires+'&'+object+'='+url;
    log(commandurl);
    FB.api(commandurl,'post',
           function(response) {
	       log("error response from fbCreateAction",response);
               if (!response || response.error) {
                   log('Error occured');
               } else {
                   log('Post was successful! Action ID: ' + response.id);
               }
           });
}

function fbClientConnect() {

    window.fbAsyncInit = function () {

        FB.init({
            appId: window.appid,
            status: true,
            cookie: true,
            xfbml: true,
            oauth: true
        });

        FB.Event.subscribe('auth.authResponseChange', function (response) {
            if (response.authResponse) {
                // fbLoggedIn(response);
            }
            else {
                // fbLoggedOut(response);
            }
            fbLoginStatusChanged(response);
        });

        FB.getLoginStatus(function (response) {
            if (response.authResponse) {
		$("#fblogin").hide();
                fbLoginStatusChanged(response);
            }
            else {
                // no user session available, someone you dont know
            }
        });
    };

    (function () {
        var e = document.createElement('script');
        e.async = true;
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        document.getElementsByTagName('head')[0].appendChild(e);
    }());
}

