var BFG=BFG||{};BFG.Facebook=(function(){var G=false,I=[],A={};var F={api_key:"",debug_level:0,doNotUseCachedConnectState:false,xd_receiver_path:"/facebook/xd_receiver.html",hurl:"",susi_fblogin_url:"",susi_fblink_url:"",susi_fbunlink_url:"",is_loggedin_bfg:false,logged_in_via_fbconnect:false,csrf_token_name:"",csrf_token_value:"",cookie_domain:window.location.host};function D(){var J=window.console&&window.console.log||function(){};D=J;return J.apply(null,arguments);}function B(J){J=J||document.body;$(J).click(function(O){var L=$(O.target);while(L.length>0&&!L.hasClass("fb-control")){L=L.parent();}if(L.length===0||!L.hasClass("fb-control")){return ;}var K=(L.get(0).className.match(/fb-control-(\w+)/)||["",""])[1],N=BFG.Facebook.CONTROLS[K.toUpperCase()],M=K&&("handle"+K.charAt(0).toUpperCase()+K.substring(1))||null;if(M&&BFG.Facebook.hasOwnProperty(M)&&typeof BFG.Facebook[M]==="function"){O.preventDefault();if(!BFG.Facebook.isControlDisabled(N)){BFG.Facebook.disableControl(N);BFG.Facebook[M](L);}}});}function E(J){J=J||document.body;$(J).click(function(L){var K=$(L.target);while(K.length>0&&!K.is("a")){K=K.parent();}if(K.length===0||!K.is("a")||!K.attr("href").match(/logout.(php|html?)/)){return ;}L.preventDefault();BFG.Facebook.handleLogout(K.attr("href"));});}function H(K){var M=K+"=",J=document.cookie.split(";"),O,L,N;for(L=0,N=J.length;L<N;L++){O=J[L];while(O.charAt(0)===" "){O=O.substring(1,O.length);}if(O.indexOf(M)===0){return O.substring(M.length,O.length);}}return null;}function C(M,J){var L=new Date(),K=L.toGMTString();L.setTime(L.getTime()+(-1*24*60*60*1000));document.cookie=M+"=; expires="+K+"; path=/; domain="+J;}return{config:F,init:function(J){this.config=$.extend(F,J||{});var K=this;FB.init(this.config.api_key,this.config.xd_receiver_path,{doNotUseCachedConnectState:this.config.doNotUseCachedConnectState,debugLogLevel:this.config.debug_level,ifUserConnected:function(L){G=true;},ifUserNotConnected:function(){G=false;BFG.Facebook.destroyFacebookSession();}});this.attachClickHandlers(document.body);this.init=function(){};},destroyFacebookSession:function(){var J=this;if(this.config.logged_in_via_fbconnect&&this.fookieExists()){this.fireEvent(this.EVENTS.BEFORE_FOOKIE_DELETE,null,function(){J.config.logged_in_via_fbconnect=false;J.deleteFookie();J.fireEvent(J.EVENTS.FOOKIE_DELETE);});}},fookieExists:function(){return H("fookie")!==null;},deleteFookie:function(){C("fookie",this.config.cookie_domain);},attachClickHandlers:function(J){B(J);E(J);},attachClickHandlersToFrame:function(J){var L=this,K=document.getElementById(J),N;if(!K){return ;}try{N=K.contentDocument||K.contentWindow&&K.contentWindow.document;if(N){if(N.body){this.attachClickHandlers(N.body);}}}catch(M){$(K).load(function(){K=document.getElementById(J);try{N=K.contentDocument||K.contentWindow&&K.contentWindow.document;L.attachClickHandlers(N.body);}catch(O){}});}},getLoggedInFBUID:function(){return G&&FB.Connect.get_loggedInUser()||null;},isLoggedInBFG:function(){return this.config.is_loggedin_bfg;},EVENTS:{BEFORE_UNLINK:"beforeRevokeAuthorization",UNLINK:"revokeAuthorzation",UNLINK_ERROR:"revokeAuthorzationError",LINK:"link",LINK_ERROR:"linkError",BEFORE_LOGIN:"beforeLogin",LOGIN_FB_CANCEL:"cancelLoginFB",LOGIN_FB:"loginFB",LOGIN_BFG:"loginBFG",LOGIN_BFG_REDIRECT:"loginBFGRedirect",LOGIN_BFG_ERROR:"loginBFGError",BEFORE_LOGOUT:"beforeLogout",BEFORE_FOOKIE_DELETE:"beforeFookieDelete",FOOKIE_DELETE:"fookieDelete"},addEvent:function(J,K){J=J.replace(/^on/,"").toLowerCase();if(I[J]===undefined){I[J]=[];}I[J].push(K);return this;},fireEvent:function(M,K,J){M=M.replace(/^on/,"").toLowerCase();var N=new BFG.FacebookEvent(M,K);if(I[M]!==undefined){for(var L=0,O=I[M].length;L<O;L++){I[M][L].call(null,N);}}if(!N.defaultPrevented&&J&&typeof J==="function"){J.call(null,N);}return N;},CONTROLS:{LOGIN:"fb-control-login",DISCONNECT:"fb-control-disconnect"},disableControl:function(J){A[J]=true;},enableControl:function(J){A[J]=false;},isControlDisabled:function(J){return A.hasOwnProperty(J)&&A[J];},sendJSONPRequest:function(N,J,P){var M=this,L="_callback"+(""+Math.random()).replace(/[^\d]/,""),O;N=N+(N.match(/\?/)?"&":"?")+"callback=BFG.Facebook."+L;N=N+"&"+encodeURIComponent(this.config.csrf_token_name)+"="+encodeURIComponent(this.config.csrf_token_value);this[L]=function(R){if(R.errorcode||R.errormsg){if(P&&typeof P==="function"){P(R,R);}}else{if(J&&typeof J==="function"){J(R);}}if(O.parentNode){O.parentNode.removeChild(O);}M[L]=null;try{delete M[L];}catch(Q){}};O=document.createElement("script");O.type="text/javascript";O.src=N;var K=document.getElementsByTagName("head")[0]||document.documentElement;K.insertBefore(O,K.firstChild);},handleLogin:function(K){var L=this;var N=false,M=function(){if(N){return function(){};}N=true;return function(){N=false;G=true;L.fireEvent(L.EVENTS.LOGIN_FB,null,function(){if(L.isLoggedInBFG()){L.createLink();}else{L.loginBFG();}});};};var J=function(){var O=this;this.fireEvent(this.EVENTS.BEFORE_LOGIN,null,function(){FB.ensureInit(function(){FB.Connect.requireSession(M(),function(){O.fireEvent(O.EVENTS.LOGIN_FB_CANCEL);O.enableControl(O.CONTROLS.LOGIN);},true);});});};this.handleLogin=J;J.apply(this,arguments);},loginBFG:function(){var J=this;this.sendJSONPRequest(this.config.susi_fblogin_url,function(K){if(K.loggedIn){J.config.is_loggedin_bfg=true;J.fireEvent(J.EVENTS.LOGIN_BFG,[K],function(){window.location.reload();});}else{if(K.redirect){J.fireEvent(J.EVENTS.LOGIN_BFG_REDIRECT,[K],function(){window.location=K.redirect;});}}J.enableControl(J.CONTROLS.LOGIN);},function(K){J.fireEvent(J.EVENTS.LOGIN_BFG_ERROR,[K.errormsg,K.errorcode,K.errorkey,K]);J.enableControl(J.CONTROLS.LOGIN);});},createLink:function(){var J=this;this.sendJSONPRequest(this.config.susi_fblink_url,function(K){J.fireEvent(J.EVENTS.LINK,[K]);J.enableControl(J.CONTROLS.LOGIN);},function(K){J.fireEvent(J.EVENTS.LINK_ERROR,[K.errormsg,K.errorcode,K.errorkey,K]);J.enableControl(J.CONTROLS.LOGIN);});},handleDisconnect:function(L){var N=this,J=this.config.susi_fbunlink_url,M=new RegExp(this.CONTROLS.DISCONNECT+"-([0-9]+)"),K=(($(L).attr("id")||"").match(M)||["",""])[1];if(K){J=J+(J.match(/\?/)?"&":"?")+"uid="+K;}this.fireEvent(N.EVENTS.BEFORE_UNLINK,null,function(){N.sendJSONPRequest(J,function(O){N.fireEvent(N.EVENTS.UNLINK,[O],function(){if(N.config.logged_in_via_fbconnect){N.handleLogout(N.config.hurl);}else{window.location.reload();}});N.enableControl(N.CONTROLS.DISCONNECT);},function(O){N.fireEvent(N.EVENTS.UNLINK_ERROR,[O.errormsg,O.errorcode,O.errorkey,O]);N.enableControl(N.CONTROLS.DISCONNECT);});});},handleLogout:function(L){var K=this;var J=this.fireEvent(this.EVENTS.BEFORE_LOGOUT,null,function(){K.logout(L);});},logout:function(J){if(this.config.logged_in_via_fbconnect){FB.ensureInit(function(){FB.Connect.logoutAndRedirect(J);});}else{window.location=J;}}};})();BFG.FacebookEvent=function(B,A){this.type=B;this.defaultPrevented=false;this.args=A;};BFG.FacebookEvent.prototype.preventDefault=function(){this.defaultPrevented=true;};