var _FB = { UID: "", //user uid AUTH_TOKEN: "", SESSION_KEY: "", USER_FIRST_NAME: "", USER_LAST_NAME: "", erase: function() //_FB: Erasing all values { this.UID = ""; this.AUTH_TOKEN = ""; this.SESSION_KEY = ""; this.USER_FIRST_NAME = ""; this.USER_LAST_NAME = ""; } } var FBCookieWrapper = { set: function(){ var str = this.makeStr(); //console.log("FBCookieWrapper: set value into cookie"); CookieManager.setCookie("facebook", str, 365*24*60*60*1000); }, erase: function(){ //console.log("FBCookieWrapper: erase value from cookie"); CookieManager.setCookie("facebook", "", 365*24*60*60*1000); }, get: function(){ //console.log("FBCookieWrapper: retrieve value from cookie and store into _FB."); var tmpFbObj = null; var str = null; str = CookieManager.getRawCookie("facebook"); if(str!=null && str !=""){ eval("tmpFbObj = "+str+";"); //parse into an object } if(tmpFbObj != null) { //console.log("FBCookieWrapper: Setting Value from Cookie into _FB"); _FB.SESSION_KEY = tmpFbObj.SESSION_KEY; _FB.UID = tmpFbObj.UID; _FB.USER_LAST_NAME = tmpFbObj.USER_LAST_NAME; _FB.USER_FIRST_NAME = tmpFbObj.USER_FIRST_NAME; }else { //console.log("FBCookieWrapper: Cookie unavailable -- setting null to _FB"); _FB.SESSION_KEY = null; _FB.UID = null; _FB.USER_LAST_NAME = null; _FB.USER_FIRST_NAME = null; } }, makeStr: function(){ //convert _FB into flat string because cookiemanager only takes in flat string value var str = "{"+ "UID:\""+_FB.UID +"\","+ "SESSION_KEY:\""+_FB.SESSION_KEY +"\","+ "USER_FIRST_NAME:\""+_FB.USER_FIRST_NAME+"\","+ "USER_LAST_NAME:\""+_FB.USER_LAST_NAME +"\""+ "}"; //console.log("FBCookieWrapper: converting _FB into flat string."); return str; } } var FacebookManager = { GeneralUser: null, //GeneralUser is a reference to user.js when set. init: function(GeneralUser) { var dataSourceUrl = null; this.GeneralUser = GeneralUser; //console.log("FacebookManager: initializing"); FBCookieWrapper.get(); //Trying to load Data Into _FB if(_FB.UID != null && _FB.UID !="") {//cookie data was loaded //console.log("FacebookManager: Cookie Data Avilable"); this.processFacebookLoginSuccess(); return true; } else {//cookie data not present //console.log("FacebookManager: Cookie Data NOT Avilable"); if(_FB.AUTH_TOKEN != null && _FB.AUTH_TOKEN != "") //check for auth_token param -- spit out by facebook api { //console.log("FacebookManager: Got Auth Token"); dataSourceUrl = this.makeDataSourceUrl("facebook.auth.getSession"); this.getSessionKey(dataSourceUrl); } else { //console.log("FacebookManager: No Auth Token"); this.processFacebookLoginFailure("No Auth Token"); } } }, makeDataSourceUrl: function (method, format, callid, uids, fields, version) {//Make the Facebook API Request URL var procAjax = false; var date = null; var dataSourceUrl = null; if(version == null || version == "") { version = "1.0"; } if(callid == true); { date = new Date(); callid = date.getTime(); } if(format == null || format == "") { format = "XML"; } //console.log("FacebookManager.makeRequest: Values: method "+method+", format "+format+", callid "+callid+", uids "+uids+", fields "+fields+", version "+version); switch(method) //Construct the call { case "facebook.auth.getSession": ///GET SESSION //console.log("makeRequest: auth.getSession"); dataSourceUrl = ROOTDIR+'services/facebook/?auth_token='+_FB.AUTH_TOKEN+'&format='+format+'&method='+method+'&v='+version; break; case "facebook.users.getInfo": ///GET USER INFO //console.log("makeRequest: users.getInfo"); dataSourceUrl = ROOTDIR+'services/facebook/?session_key='+_FB.SESSION_KEY+'&call_id='+callid+'&format='+format+'&method='+method+'&fields='+fields+'&uids='+uids+'&v='+version; break; } return dataSourceUrl; }, getSessionKey: function(dataSourceUrl) { //console.log("FacebookManager: Getting Session Key"); var callback={ success:this.handleGetSessionKeySuccess, failure:this.handleGetSessionKeyFailure, scope: this }; //console.log("FacebookManager.getSessionKey: " + dataSourceUrl); try{ YAHOO.util.Connect.asyncRequest('POST', dataSourceUrl, callback, "confirm=1"); } catch(e) { this.processFacebookLoginFailure(e); } }, handleGetSessionKeyFailure: function(o) { this.processFacebookLoginFailure("Session Key Ajax Failure"); }, handleGetSessionKeySuccess: function(o) { var root = o.responseXML.documentElement; try { var oSessionKey = root.getElementsByTagName('session_key')[0].firstChild.nodeValue; var oUid = root.getElementsByTagName('uid')[0].firstChild.nodeValue; var oExpires = root.getElementsByTagName('expires')[0].firstChild.nodeValue; var dataSourceUrl =null; _FB.SESSION_KEY = oSessionKey; _FB.UID = oUid; //get user info right away dataSourceUrl = this.makeDataSourceUrl("facebook.users.getInfo", "XML", true, _FB.UID, "last_name,first_name"); this.getUsersInfo(dataSourceUrl); this.setUserInfoToCookie(); //put sessionID into cookie first for retrieving playlist usage. } catch(e) { this.processFacebookLoginFailure(e); } }, getUsersInfo: function(dataSourceUrl) { //console.log("FacebookManager: Getting Users Info"); var callback={ success:this.handleGetUsersInfoSuccess, failure:this.handleGetUsersInfoFailure, scope: this }; //console.log("FacebookManager.GetUsersInfo: " + dataSourceUrl); try{ YAHOO.util.Connect.asyncRequest('POST', dataSourceUrl, callback, "confirm=1"); } catch(e) { this.processFacebookLoginFailure(e); } }, handleGetUsersInfoSuccess: function(o) { var root = o.responseXML.documentElement; try { var oFirstName = root.getElementsByTagName('first_name')[0].firstChild.nodeValue; var oLastName = root.getElementsByTagName('last_name')[0].firstChild.nodeValue; _FB.USER_FIRST_NAME = oFirstName; _FB.USER_LAST_NAME = oLastName; FBCookieWrapper.set(); //store into cookie this.processFacebookLoginSuccess(); } catch(e) { this.processFacebookLoginFailure(e); } }, handleGetUsersInfoFailed: function(o) { this.processFacebookLoginFailure("Get Users Info Ajax Failure"); }, processFacebookLogout: function() { this.GeneralUser.showFacebookButton(); this.GeneralUser.hideFacebookLogout(); FBCookieWrapper.erase(); _FB.erase(); this.GeneralUser.processSignOutResetAll(); this.GeneralUser.loadAnonSession(); //calling user.js and load Anon Session }, processFacebookLoginSuccess: function(o) { //console.log("FacebookManager: processing Great Success"); this.GeneralUser.hideFacebookButton(); this.GeneralUser.showFacebookLogout(); this.setUserInfoToCookie(); this.GeneralUser.displayUsername(); }, processFacebookLoginFailure: function(e) { //console.log("FacebookManager: Processing General Failure. " + e); FBCookieWrapper.erase(); _FB.erase(); this.GeneralUser.showFacebookButton(); this.GeneralUser.hideFacebookLogout(); this.GeneralUser.loadAnonSession(); //calling user.js and load Anon Session }, setUserInfoToCookie: function() { //console.log("FacebookManager: Setting user info to Cookie"); this.GeneralUser.acctType = "Facebook"; //set User obj accttype this.GeneralUser.userObj.displayName = _FB.USER_FIRST_NAME + " "+ _FB.USER_LAST_NAME; this.GeneralUser.userObj.sessionid = _FB.SESSION_KEY; this.GeneralUser.userObj.id = _FB.UID; //console.log("FacebookManager: Setting user info to Cookie: "+this.GeneralUser.acctType); //console.log("FacebookManager: Setting user info to Cookie: "+this.GeneralUser.userObj.id); //console.log("FacebookManager: Setting user info to Cookie: "+this.GeneralUser.userObj.displayName); //console.log("FacebookManager: Setting user info to Cookie: "+this.GeneralUser.userObj.sessionid); this.GeneralUser.cookie.setCookie("ACCTTYPE", this.GeneralUser.acctType, this.GeneralUser.cookieLife); //set cookie this.GeneralUser.cookie.setCookie("USERID", this.GeneralUser.userObj.id, this.GeneralUser.cookieLife); //set cookie this.GeneralUser.cookie.setCookie("DISPLAYNAME", this.GeneralUser.userObj.displayName, this.GeneralUser.cookieLife); this.GeneralUser.cookie.setCookie("SESSID", this.GeneralUser.userObj.id, this.GeneralUser.cookieLife); this.GeneralUser.cookie.setCookie("ANON", "false", this.GeneralUser.cookieLife); } }