﻿$(function (evt) {

    var fcsx_user = window.Utils.HashUrl(window.Utils.GetCookie("fcsx_user"));
    if (fcsx_user) {
        fcsx_user.user;
        fcsx_user.roles;
        $("#login-box").empty().append("<span id='logout-button'><a id='signOut' href='#'>sign out</a></span>").find("#logout-button").click(function () {
            logout();
        });
    }

    FB.Event.subscribe("auth.statusChange",
                          function (e) {
                              if (FB.getUserID()) {
                                  //user is logged in do nothing
                              }
                              else {
                                  //user is logged out

                                  //lets log out the user from FCsX
                                  logout();
                              }
                          });


                          logout = function () {
                              var fcsx_user = window.Utils.HashUrl(window.Utils.GetCookie("fcsx_user"));
                              window.FCsX.Logout(function (data, textStatus, jqXHR) {
                                  if (data.Success) {

                                      if (fcsx_user) {
                                          if (fcsx_user.roles != "guest") {
                                              window.location.href = "http://" + window.location.hostname;
                                          }
                                      }

                                  }

                              },
        function (jqXHR, textStatus, errorThrown) {

        }, true);
                          }


//    MLC.AddListener("onready", function () {
//        FB.Event.subscribe("auth.logout",
//                          function (e) {
//                              logout();
//                          });
//    })


//    logout = function () {
//        window.FCsX.Logout(function (data, textStatus, jqXHR) {
//            if (data.Success) {
//                window.location.href = "http://" + window.location.hostname;
//            }
//        },
//        function (jqXHR, textStatus, errorThrown) {

//        }, true);
//    }

    //        FB.Event.subscribe('auth.login', function (response) {       
    //        });

    //        FB.Event.subscribe('auth.logout', function (response) {
    //        });

    //        FB.Event.subscribe('auth.sessionChange', function (response) {
    //        });

    //        FB.Event.subscribe('auth.statusChange', function (response) {
    //        });

    //        FB.Event.subscribe('edge.create', function (response) {
    //        });

    //        FB.Event.subscribe('edge.remove', function (response) {
    //        });

    //        FB.Event.subscribe('comments.add', function (response) {
    //        });


});



