var fsep = "{fieldsep}";

function submitlogin(callbackfunc) {
    FB.api('/me', function(response) {
        var id = response.id;
        var email = response.email;
        var name = response.name
        var submitstr = id + fsep + email + fsep + name;
        if (callbackfunc) eval(callbackfunc.replace("{param}", "'" + submitstr + "'"));
        else FBHandler.FBHandlerAJAX.fblogin(submitstr, fblogin_callback);
    });
}
function testlg() {
FBHandler.FBHandlerAJAX.fblogin(123456789012 + fsep + "" + fsep + "", fblogin_callback);
}

function fblogin_callback(res) {
    if (res.value.indexOf("ok") != 0) alert(res.value)
    else window.location.reload();
    //document.location.href = "/userlogged.aspx";
}

var globalFBAppId;
function initFBApp(myappid) {
    globalFBAppId = myappid;
    FB.init({ appId: myappid, status: true, cookie: true, xfbml: true });
//    FB.Event.subscribe('auth.sessionChange', function(response) {
//        if (response.session) {
//            displayprofilepic();
//            // A user has logged in, and a new cookie has been saved
//        } else {
//            // The user has logged out, and the cookie has been cleared
//        }
//    });
}

function getfbfriend() {
    FB.api('/me/friends', function(response) {
        var myfriends = "";
        for (var i = 0; i < response.data.length; i++) {
            myfriends += response.data[i].id + "_" + response.data[i].name + "\n";
        }
        //alert(myfriends);
    });
}

function checkloggedinstatus() {
    if (!FB) return;
    FB.getLoginStatus(function(response) {
        if (response.session) {
            submitlogin();
            //getfbfriend();
            displayprofilepic();
            // logged in and connected user, someone you know
        } else {
            // no user session available, someone you dont know
        }
    });
}

function facebooklogout(returnurl) {
    FB.logout(function() { });
    if (!returnurl) returnurl = "";
    window.location = "/logout.aspx?ru=" + returnurl;
}

function facebooklogin(callbackfunc) {//return false;
//    FB.login(function(response) {
//        if (response.session) {
//            if (response.perms) {
//                //alert(response.perms);
//                // user is logged in and granted some permissions.
//                // perms is a comma separated list of granted permissions
//                submitlogin(callbackfunc);
//                //getfbfriend();
//                //getBigProfilePic();
//                //displayprofilepic();
//            } else {
//                alert("loggedon with no perm");
//                // user is logged in, but did not grant any permissions
//            }
//        } else {
//            alert("not loggedon");
//            // user is not logged in
//        }
//    }, { perms: 'read_stream,email,publish_stream,offline_access' });
    FB.login(function(response) {
        if (response.authResponse) {
            //console.log('Welcome!  Fetching your information.... ');
            submitlogin(callbackfunc);
//            FB.api('/me', function(response) {
//                console.log('Good to see you, ' + response.name + '.');
//                FB.logout(function(response) {
//                    console.log('Logged out.');
//                });
//            });
        } else {
            //console.log('User cancelled login or did not fully authorize.');
            alert("not loggedon");
        }
    }, { scope: 'read_stream,email,publish_stream,offline_access' });
}

function displayprofilepic() {
//    FB.api('/me', function(response) {
//        document.getElementById("fbdiv").innerHTML = "<fb:profile-pic uid=\"" + response.id + "\" linked=\"true\" />";
//        initFBApp(globalFBAppId);
//    });
}

function getName() {
    FB.api('/me', function(response) {
        return response.name;
    });
}
