define(["Ti/_/Evented","Ti/_/lang"],function(e,o){function n(){FB.init({appId:a,status:!1,cookie:!0,oauth:!0,xfbml:!0}),FB.getLoginStatus(function(e){s=!0,"connected"==e.status&&t(e)||u&&c()},!0)}function t(e){var o=e.authResponse;return o?(r.loggedIn=!0,r.uid=o.userID,r.expirationDate=new Date(Date.now()+1e3*o.expiresIn),r.accessToken=o.accessToken,o.expiresIn&&setTimeout(function(){r.logout()},1e3*o.expiresIn),r.fireEvent("login",{cancelled:!1,data:e,success:!0,uid:r.uid}),!0):void 0}function i(e,o,n,t){result={source:r,success:!1},result[o]=n,!e||e.error?e&&(result.error=e.error):(result.success=!0,result.result=JSON.stringify(e)),t(result)}function c(){FB.login(function(e){t(e)||r.fireEvent("login",{cancelled:!0,data:e,error:"user cancelled or an internal error occured.",success:!1,uid:e.id})},{scope:r.permissions.join()})}var r,s=!1,u=!1,a=null,d="not logged in",l=document.createElement("div"),f="facebook-jssdk",g=!1;if(r=o.setObject("Ti.Facebook",e,{authorize:function(){if(!a)throw new Error("App ID not set. Facebook authorization cancelled.");s?c():u=!0},createLoginButton:function(e){return new(require("Ti/Facebook/LoginButton"))(e)},dialog:function(e,o,n){r.loggedIn?(o.method=e,FB.ui(o,function(o){i(o,"action",e,n)})):n({success:!1,error:d,action:e,source:r})},logout:function(){r.loggedIn&&FB.logout(function(){r.loggedIn=!1,r.fireEvent("logout",{success:!0})})},request:function(e,o,n){r.loggedIn?(o.method=e,o.urls="facebook.com,developers.facebook.com",FB.api(o,function(o){i(o,"method",e,n)})):n({success:!1,error:d,method:e,source:r})},requestWithGraphPath:function(e,o,n,t){r.loggedIn?FB.api(e,n,o,function(o){i(o,"path",e,t)}):t({success:!1,error:d,path:e,source:r})},constants:{BUTTON_STYLE_NORMAL:1,BUTTON_STYLE_WIDE:2},properties:{accessToken:void 0,appid:{set:function(e){return a=e,g&&n(),e}},expirationDate:void 0,forceDialogAuth:!0,loggedIn:!1,permissions:void 0,uid:void 0}}),l.id="fb-root",document.body.appendChild(l),!document.getElementById(f)){var p=document.createElement("script"),m=document.getElementsByTagName("head")[0];p.id=f,p.async=!0,p.src="//connect.facebook.net/en_US/all.js",m.insertBefore(p,m.firstChild)}return window.fbAsyncInit=function(){g=!0,a&&n()},r});