[% IF c.user_exists  %]

  [% IF c.user.type == 'google' %]
    <script src="https://apis.google.com/js/platform.js" async="1" defer="1"></script>
  [% END %]

  <script>
    function finishSignOut() {
      $.post("[% c.uri_for('/logout') %]")
        .done(function(data) {
          window.location.reload();
        })
        .fail(function() { bootbox.alert("Server request failed!"); });
    }

    function signOut() {
      [% IF c.user.type == 'google' %]
        gapi.load('auth2', function() {
          gapi.auth2.init();
          var auth2 = gapi.auth2.getAuthInstance();
          auth2.then(function () {
            auth2.signOut().then(finishSignOut, finishSignOut);
          });
        });
      [% ELSE %]
        finishSignOut();
      [% END %]
    }
  </script>

[% ELSE %]

  <div id="hydra-signin" class="modal hide fade" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <form>
          <div class="modal-body">
            <div class="form-group">
              <label for="username" class="col-form-label">User name</label>
              <input type="text" class="form-control" name="username"/>
            </div>
            <div class="form-group">
              <label for="password" class="col-form-label">Password</label>
              <input type="password" class="form-control" name="password"/>
            </div>
          </div>
          <div class="modal-footer">
            <button id="do-signin" type="button" class="btn btn-primary">Sign in</button>
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
          </div>
        </form>
      </div>
    </div>
  </div>

  <script>

    function finishSignOut() { }

    $("#do-signin").click(function() {
      requestJSON({
        url: "[% c.uri_for('/login') %]",
        data: $(this).parents("form").serialize(),
        type: 'POST',
        success: function(data) {
          window.location.reload();
        }
      });
      return false;
    });

    $("#sign-in-menu").on("click", function() {
      [% IF c.config.enable_google_login %]
        $.getScript("https://apis.google.com/js/platform.js");
      [% END %]
    });

  </script>

  [% IF c.config.enable_google_login %]
    <script>
      function onGoogleSignIn(googleUser) {
        requestJSON({
          url: "[% c.uri_for('/google-login') %]",
          data: "id_token=" + googleUser.getAuthResponse().id_token,
          type: 'POST',
          success: function(data) {
            window.location.reload();
          }
        });
        return false;
        };

      $("#google-signin").click(function() {
        $(".g-signin2:first-child > div").click();
      });
    </script>
  [% END %]

[% END %]