var userLoggedIn = null;
var userStr = '';

var sURL = "https://family.go.com";
if( typeof( secureUrl ) != 'undefined')
  sURL = secureUrl;

function completeHeaderAndCallbacks(theUser) {
  var callbackIndex = 0;
  if (theUser != null && theUser.loggedIn) {
        userLoggedIn = true;
        userStr += '<li class="logInWelcome">Welcome, <span>'+theUser.userName+'</span></li>';
        userStr += '<li><a href="http://familyfun.go.com/favorites/" title="My Recipes & Crafts">My Recipes & Crafts</li>';
        
        userStr += '<li><a href="javascript:void(0)" ';
        userStr += 'onclick="return launch(\'http://family.go.com/profiles/'+theUser.userName+'/\', \'\', \'height=600,width=996,status=yes,toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes\');" ';
        userStr += 'title="Profile">Profile</a></li>';
        
        userStr += '<li><a href="javascript:void(0)" onclick="return launch(\'http://family.go.com/community/messages/\', \'\', \'height=600,width=996,status=yes,toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes\');" ';
        userStr += 'name="&lpos=TxtMessages&lid=Messages" title="Messages">Messages ('+theUser.messageCount+')</a></li>';
        
        userStr += '<li><a href="javascript:void(0)" onclick="return launch(\''+sURL+'/accounts/my-account/\',\'\', \'height=600,width=996,status=yes,toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes\');" title="Account">Account</a></li>';
        userStr += '<li class="last"><a href="javascript:void()" onclick="submitLogout(); return false;" title="Logout">Logout</a></li>';
        
        dojo.byId( "userInfo" ).innerHTML = userStr;
        if (typeof (organizerUserCallback) != 'undefined') {
            organizerUserCallback(theUser.userName, theUser.userId, theUser.loggedIn);
        } else if( typeof( userServiceCallbacks ) != 'undefined' ) {
        for(callbackIndex = 0; callbackIndex < userServiceCallbacks.length; callbackIndex = callbackIndex + 1) {
          userServiceCallbacks[callbackIndex](theUser.userName, theUser.userId, theUser.loggedIn);
        }
        }
  } else {
    userLoggedIn = false;
    
    // NOTE: variables registerLinkTitle and registerLinkLID are defined in the file that includes this external JS.  This is part of the Google Website Optimizer functionality
    userStr += '<li><a href="http://familyfun.go.com/favorites/" title="My Recipes & Crafts">My Recipes & Crafts</li><li><a href="javascript:void()" onclick="submitLogin(); return false;" name="&lpos=TxtLogin&lid=Login" title="Login">Login</a></li><li class="last"><a href="javascript:void()" onclick="submitRegister(); return false;" name="&lpos=TxtRegister&lid=' + registerLinkLID + '" title="Register">' + registerLinkTitle + '</a></li>';
    dojo.byId( "userInfo" ).innerHTML = userStr;
    if (typeof (organizerUserCallback) != 'undefined') {
            organizerUserCallback("", -1, false);
        } else if( typeof( userServiceCallbacks ) != 'undefined' ) {
      for(callbackIndex = 0; callbackIndex < userServiceCallbacks.length; callbackIndex = callbackIndex + 1) {
        userServiceCallbacks[callbackIndex]("", -1, false);
      }
    }     
  }
    
}

FFAddOnLoad( function() {
  var theUser = null;
    if(dojo.cookie.isSupported() && !!dojo.cookie("STNGS") && !!dojo.cookie("fam_net_u")){
      // don't call the service. Use the fam_net_u cookie
      try {
         var vars = dojo.cookie("fam_net_u").split(",");
         theUser = new Object();
         theUser.userName= vars[0];
         theUser.loggedIn = true;
         theUser.userId= vars[1];
         theUser.messageCount = vars[2];
         completeHeaderAndCallbacks(theUser);
      }
      catch(e){
        theUser = null;
      }
   }
   else{
      if(dojo.cookie.isSupported() && (!!!dojo.cookie("STNGS") && (!!dojo.cookie("loggedInForTwoWeeks") ||  !!dojo.cookie("GREEN") || !!dojo.cookie("BLUE")) || (dojo.cookie("STNGS") && !!!dojo.cookie("fam_net_u")))) {
         dojo.xhrGet( {
            url: "/svc_user_v1/index.json",                 // Get this service location from our global init file
            headers: { "Accept" : "application/*+json" },
            handleAs: "json",
            timeout: 10000,
            load: function( response, ioArgs ) {
               try {
                  theUser = response.user;
                  completeHeaderAndCallbacks(theUser);
               }
            
               catch (e) {
                  completeHeaderAndCallbacks(theUser);
                  return;
               }
            },
            error: function( response, ioArgs ) {
               completeHeaderAndCallbacks(null);
            }
         });
      }
      else {
         // no user is logged-in or cookies not supported.
         completeHeaderAndCallbacks(null);
      }
   }
}, true);