/* ################################################################# */
/*                                                                   */
/*  TITLE:        NBC_USER.JS                                        */
/*  VERSION:      0.02                                               */
/*  LAST UPDATED: 12/01/2009                                         */
/*  UPDATED BY:   Brian Maniere                                      */
/*                                                                   */
/*  NAMESPACE: user                                                  */
/*                                                                   */
/*  PUBLIC FUNCTIONS:                                                */
/*    init()                                                         */
/*                                                                   */
/*  @release traffic brian                                           */
/*                                                                   */
/* ################################################################# */
var NBC_User = function() {
};

NBC_User.prototype.userId; 
NBC_User.prototype.userName;
NBC_User.prototype.email;
NBC_User.prototype.zipCode;
NBC_User.prototype.userAge;
NBC_User.prototype.userGender;
NBC_User.prototype.token;
NBC_User.prototype.facebookId;
NBC_User.prototype.role;
NBC_User.prototype.picUrl;
NBC_User.prototype.loggedIn;

NBC_User.prototype.init = function() {
  var userData = this.readUserData();
  if( typeof userData != 'undefined' && userData.userId ) {
    U.log("Parsing user data...");
    this.userId = userData.userId;
    U.log("userId has value [" + this.userId + "]");
    
    if (userData.userName) {
      this.userName = userData.userName;
      U.log("userName has value [" + this.userName + "]");
    }
    if (userData.email) {
      this.email = userData.email;
      U.log("email has value [" + this.email + "]");
    }
    if (userData.zipCode) {
      this.zipCode = userData.zipCode;
    }
    if (userData.userAge) {
      this.userAge = userData.userAge;
    }
    if (userData.userGender) {
      this.userGender = userData.userGender;
    }
    if (userData.token) {
      this.token = userData.token; 
      U.log("token has value [" + this.token + "]");
    }
    if (userData.fbId && userData.fbId != 'undefined') {
      this.facebookId = userData.fbId;
      U.log("facebookId has value [" + this.facebookId + "]");
    }
    if (userData.role) {
      this.role = userData.role;
      U.log("role has value [" + this.role + "]");
    }
    if (userData.picUrl && userData.picUrl != "undefined") {
      this.picUrl = userData.picUrl;
      U.log("picUrl has value [" + this.picUrl + "]");
    }
    if (this.isValid()) {
      this.loggedIn = true;
    }
  }
  else {
    U.log("User not logged in: No cookie present.");
  }
  
    // wait till all components are loaded, and then fire user.login
  var self = this;
  $(document).ready(function() {
    if (self.isLoggedIn()) {
      U.log("user: firing user.login event");
      NBC_EventManager.fire("user.login");
    }
  });
};

NBC_User.prototype.isValid = function() {
  return typeof this.userName != "undefined";
};

NBC_User.prototype.isEditor = function() {
  return typeof this.role != "undefined" && this.role === "EDITOR";
};

NBC_User.prototype.setValues = function(args) {
  for( property in args ) {
    U.log("Setting user property [" + property + "] with value [" + args[property] + "]");
    this[property] = args[property];
      // login when userName is specified
    if( property === "userName" && args[property] != "undefined" ) {
      this.loggedIn = true;
    }
  }
  if (this.loggedIn) {
    NBC_EventManager.fire("user.login");      
  }
};

NBC_User.prototype.readUserData = function() {
  if( U.readCookie("u_ka") ) { // logged in
    var cookieValue = unescape(U.readCookie("u_ka"));
    U.log("user: User cokie value [" + cookieValue + "]");
    var userCookie = eval("(" + cookieValue + ")");
    var userData = userCookie.user;
    return userData;
  }
};

NBC_User.prototype.persist = function(persistCookie) {
  if (!this.isValid()) {
    throw new Error("Invalid user state.");
  }
    // given a response in this JSON format
    // {"ka":{"TOKEN":"jYuha1rthMNYOYEJcscCHW2p6/UiHS5t","METHOD":"POST","PRIVILEGES":"RW","payload_type":"json","userId":4852011,"email":"stanley.kubasek@nbcuni.com"}} 
    // store it as {"user":{"userId":"...,"userName","...","email":"..."}}
  var cookieVal = '{"user":{"token":"' + this.token + '","userId":"' + this.userId + '","userName":"' + this.userName + '","email":"' + this.email + '","role":"' + this.role + '","picUrl":"'+ this.picUrl + '","zipCode":"'+ this.zipCode + '","userAge":"'+ this.userAge + '","userGender":"'+ this.userGender + '","fbId":"' + this.facebookId + '"}}';  

  U.log("user: Writing cookies value [" + cookieVal + "]");
  if (persistCookie) {
    U.createCookie("u_ka", cookieVal, 365); // expire after 1 year
  }
  else {
    U.log("Cookie saved as a session!");
    U.createCookie("u_ka", cookieVal); // session cookie
  }
};

NBC_User.prototype.isLoggedIn = function() {
  return this.loggedIn;
};

NBC_User.prototype.getUserName = function() {
  if (typeof this.userName != "undefined") {
    return this.userName;
  }
  else if (this.isFacebookUser()) {
    return this.facebookId;
  }
  else {
    U.log("Warning: user object has userName empty!");
    return this.userName;
  }
};

NBC_User.prototype.isFacebookUser = function() {
  return this.facebookId && this.facebookId != 'undefined';
};

NBC_User.prototype.logout = function() {
  U.eraseCookie("u_ka");
  this.userId = this.userName = this.email = this.zipCode = this.userAge = this.userGender = this.token = this.facebookId = this.role = this.picUrl = null;
  this.loggedIn = false;
  NBC_EventManager.fire("user.logout");
};
