         AppEvent.addListener(AppEvent.SETTINGS_SAVED, settingsChangeHandler);
     }
 
-    private function identityManager_signInHandler(event:IdentityManagerEvent):void
-    {
-        if (PortalModel.getInstance().hasSameOrigin(event.credential.server)
-            && !PortalModel.getInstance().portal.signedIn)
-        {
-            signIntoPortalInternally();
-        }
-    }
-
-    private function portalSignOutHandler(event:AppEvent):void
-    {
-        if (Log.isInfo())
-        {
-            LOG.info("Portal sign out requested");
-        }
-
-        IdentityManager.instance.removeEventListener(IdentityManagerEvent.SIGN_IN, identityManager_signInHandler);
-        portal.addEventListener(PortalEvent.LOAD, portal_loadHandler);
-        portal.addEventListener(FaultEvent.FAULT, portal_faultHandler);
-        portal.signOut();
-    }
-
-    private function portalSignInHandler(event:AppEvent):void
-    {
-        if (Log.isInfo())
-        {
-            LOG.info("Portal sign in requested");
-        }
-
-        signIntoPortalInternally();
-    }
-
-    private function signIntoPortalInternally():void
-    {
-        IdentityManager.instance.removeEventListener(IdentityManagerEvent.SIGN_IN, identityManager_signInHandler);
-        portal.addEventListener(PortalEvent.LOAD, portal_loadHandler);
-        portal.addEventListener(FaultEvent.FAULT, portal_faultHandler);
-        portal.signIn();
-    }
-
     private function settingsChangeHandler(event:AppEvent):void
     {
         //optimization: AGO uses OAuth, so we register the OAuth info upfront

        }
    }

    private function unloadPortal():void
    {
        if (Log.isDebug())
        {
            LOG.debug("Unloading Portal");
        }

        portal.url = null;
        portal.unload();
    }

    private function dispatchPortalStatusUpdate():void
    {
        updatePortalHandlersBasedOnStatus();
        AppEvent.dispatch(AppEvent.PORTAL_STATUS_UPDATED);
    }

    private function updatePortalHandlersBasedOnStatus():void
    {
        if (portal.signedIn)
        {
            AppEvent.addListener(AppEvent.PORTAL_SIGN_OUT, portalSignOutHandler);
            AppEvent.removeListener(AppEvent.PORTAL_SIGN_IN, portalSignInHandler);
            IdentityManager.instance.removeEventListener(IdentityManagerEvent.SIGN_IN, identityManager_signInHandler);
        }
        else
        {
            AppEvent.addListener(AppEvent.PORTAL_SIGN_IN, portalSignInHandler);
            IdentityManager.instance.addEventListener(IdentityManagerEvent.SIGN_IN, identityManager_signInHandler);
            AppEvent.removeListener(AppEvent.PORTAL_SIGN_OUT, portalSignOutHandler);
        }
    }

    private function portal_loadHandler(event:PortalEvent):void

            dispatchPortalStatusUpdate();
        }
    }

    private function unloadPortal():void
    {
        if (Log.isDebug())
        {
            LOG.debug("Unloading Portal");
        }

        portal.url = null;
        portal.unload();
    }

    private function dispatchPortalStatusUpdate():void
    {
        updatePortalHandlersBasedOnStatus();
        AppEvent.dispatch(AppEvent.PORTAL_STATUS_UPDATED);
    }

    private function updatePortalHandlersBasedOnStatus():void
    {
        if (portal.signedIn)
        {
            AppEvent.addListener(AppEvent.PORTAL_SIGN_OUT, portalSignOutHandler);
            AppEvent.removeListener(AppEvent.PORTAL_SIGN_IN, portalSignInHandler);
            IdentityManager.instance.removeEventListener(IdentityManagerEvent.SIGN_IN, identityManager_signInHandler);
        }
        else
        {
            AppEvent.addListener(AppEvent.PORTAL_SIGN_IN, portalSignInHandler);
            IdentityManager.instance.addEventListener(IdentityManagerEvent.SIGN_IN, identityManager_signInHandler);
            AppEvent.removeListener(AppEvent.PORTAL_SIGN_OUT, portalSignOutHandler);
        }
    }

    private function portalSignOutHandler(event:AppEvent):void
    {
        if (Log.isInfo())
        {
            LOG.info("Portal sign out requested");
        }

        IdentityManager.instance.removeEventListener(IdentityManagerEvent.SIGN_IN, identityManager_signInHandler);
        portal.addEventListener(PortalEvent.LOAD, portal_loadHandler);
        portal.addEventListener(FaultEvent.FAULT, portal_faultHandler);
        portal.signOut();
    }

    private function portalSignInHandler(event:AppEvent):void
    {
        if (Log.isInfo())
        {
            LOG.info("Portal sign in requested");
        }

        signIntoPortalInternally();
    }
}
}
