[% 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">
    <form class="form-horizontal">
      <div class="modal-body">
        <div class="control-group">
          <label class="control-label">User name</label>
          <div class="controls">
            <input type="text" class="span3" name="username" value=""/>
          </div>
        </div>
        <div class="control-group">
          <label class="control-label">Password</label>
          <div class="controls">
            <input type="password" class="span3" name="password" value=""/>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button id="do-signin" class="btn btn-primary">Sign in</button>
        <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
      </div>
    </form>
  </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 %]
