'use strict';

function InviteCtrl(lib, session, $scope, $http, $routeParams, errors) {

    $scope.useExisting = false;
    $scope.createNew = false;

    var inviteId = $routeParams.inviteId;
    var invite = undefined;
    if (!inviteId) {
        return;
    }

    $http.get('/data/invite/' + inviteId)
    .success(function (data, status) {
        invite = data;
        $scope.invite = invite;
    })
    .error(function (data, status) {
        errors.log(data);
    });

    $scope.showUseExisting = function () {
        $scope.useExisting = true;
        $scope.createNew = false;
    };

    $scope.showCreateNew = function () {
        $scope.useExisting = false;
        $scope.createNew = true;
    };

    var signIn = function (email, password) {
        lib.signIn(email, password, function (err, user) {
            if (err) {
                errors.log(err);
                // TODO: Show error
                return;
            }
            lib.goHome(user, session, function (err) {
                if (err) {
                    errors.log(err);
                    // TODO: Show error
                }
            });
        });
    };

    $scope.createAccount = function (signup) {
        var data = {};
        data.account = signup;
        data.invite = invite;

        $http.post('/data/signup/invite', data)
        .success(function (data) {
            signIn(signup.email, signup.password);
        })
        .error(errors.handle);
    };

    $scope.useAccount = function (account) {
        var data = {};
        data.account = account;
        data.invite = invite;

        $http.post('/data/invite/accept', data)
        .success(function (data) {
            signIn(account.email, account.password);    
        })
        .error(errors.handle);
    };
}
InviteCtrl.$inject = ['lib', 'session', '$scope', '$http', '$routeParams', 'errors'];
@@ -58,16 +58,27 @@
 , $http,
+ $location,
  $routeP
@@ -95,16 +95,16 @@
 ors) %7B%0D%0A
-
 %0D%0A    $s
@@ -989,32 +989,34 @@
 %0A            %7D%0D%0A
+%0D%0A
             lib.
@@ -1015,191 +1015,144 @@
     
-lib.goHome(user, session, function (err) %7B%0D%0A                if (err) %7B%0D%0A                    errors.log(err);%0D%0A                    // TODO: Show error%0D%0A                %7D%0D%0A            %7D
+session.activeCircle = invite.circleId;%0D%0A            session.user = user;%0D%0A            session.save();%0D%0A%0D%0A            $location.path('/'
 );%0D%0A
@@ -1836,16 +1836,16 @@
  %7D;%0D%0A%7D%0D%0A
-
 InviteCt
@@ -1894,16 +1894,29 @@
 '$http',
+ '$location',
  '$route

Set active circle to circle joined