{{ $s := partialCached "scratch.html" . }}

{{ $currentDir := "" }}
{{ with .File }}
  {{ $currentDir = .Dir }}
{{ end }}

{{ $currentFile := (printf "/%s" .File) }}
{{ $scratch := $s.Get $currentFile }}

{{ $is_in_ := newScratch }}
{{ with eq $scratch.depth "crust" }}
  {{ range $scratch.is_in }}
    {{ range $k,$v := $s.Get (printf "/%s" .File) }}
      {{ if eq $k "is_in" }}
        {{ range $v }}
          {{ $is_in_.Set (printf "/%s" .File) (printf "/%s" .File)  }}
        {{ end }}
      {{ end }}
    {{ end }}
  {{ end }}
{{ end }}

{{ $is_in_is_in := (slice) }}
{{ range $is_in_.Values }}
  {{ $is_in_is_in = $is_in_is_in | append ($.GetPage .) }}
{{ end }}

{{ if eq hugo.Environment "gitea" }}
  <div class="headiter" id="preview">
    <a class="edit-button" target="_blank" href="{{ $.Site.Params.sandpointsGiteaUrl }}/_edit/master/content/{{ .File }}">EDIT_THIS</a>
    {{ with $scratch.core }}<a class="edit-button" target="_blank" href="{{ $.Site.Params.sandpointsGiteaUrl }}/_new/master/content/{{ index . 0 }}/">ADD_{{ index . 0 | upper }}</a>{{ end }}
    {{ with $scratch.mantle }}<a class="edit-button" target="_blank" href="{{ $.Site.Params.sandpointsGiteaUrl }}/_new/master/content/{{ index . 0 }}/">ADD_{{ index . 0 | upper }}</a>{{ end }}
    {{ with $scratch.crust }}<a class="edit-button" target="_blank" href="{{ $.Site.Params.sandpointsGiteaUrl }}/_new/master/content/{{ index . 0 }}/">ADD_{{ index . 0 | upper }}</a>{{ end }}
    {{ if not $scratch.core }}<a class="edit-button" target="_blank" href="{{ $.Site.Params.sandpointsGiteaUrl }}/_new/master/content/{{ $currentDir }}">ADD_{{ substr $currentDir 0 -1 | upper }}</a>{{ end }}
    <a class="edit-button" target="_blank" href="{{ $.Site.Params.sandpointsGiteaUrl }}/_edit/master/PUBLISH.trigger.mkd">PUBLISH</a>
    <a class="edit-button" target="_blank" href="{{ $.Site.Params.sandpointsGiteaUrl }}"> ? </a>
  </div>
{{ else }}
  {{ $editPage := $.Site.GetPage "/edit"  }}
  <div class="headiter" id="preview">
    <a class="edit-button" href="{{ $editPage.RelPermalink }}index.html?edit={{ .File.UniqueID }}">EDIT_THIS</a>
    {{ with $scratch.core }}<a class="edit-button" href="{{ $editPage.RelPermalink }}index.html?new={{ index . 0 }}">ADD_{{ index . 0 | upper }}</a>{{ end }}
    {{ with $scratch.mantle }}<a class="edit-button" href="{{ $editPage.RelPermalink }}index.html?new={{ index . 0 }}">ADD_{{ index . 0 | upper }}</a>{{ end }}
    {{ with $scratch.crust }}<a class="edit-button" href="{{ $editPage.RelPermalink }}index.html?new={{ index . 0 }}">ADD_{{ index . 0 | upper }}</a>{{ end }}
    {{ if not $scratch.core }}<a class="edit-button" href="{{ $editPage.RelPermalink }}index.html?new={{ $currentDir }}">ADD_{{ substr $currentDir 0 -1 | upper }}</a>{{ end }}
  </div>
  </div>
{{ end }}

<div class="header">
  <a title="{{ $.Site.Home.Title }}'s Bibliotheke" href="/library/BROWSE_LIBRARY.html" class="bibliotheke" target="_blank">
    <img src="/images/bibliotheke.svg" />
  </a>
  <div class="breadcrumbs">
    {{ if and (not $scratch.depth) $.Site.Params.sandpointsHome }}
        <a href="{{ with $.GetPage $.Site.Params.sandpointsHome }}{{ .RelPermalink }}{{ end }}{{ if eq hugo.Environment "offline" }}index.html{{ end }}"><span class="supt">{{ with $.GetPage $.Site.Params.sandpointsHome }}{{ substr .Title 0 1  }}</span><i>{{ substr .Title 1 }}</i></a>{{ end }}
    {{ end }}
    {{ if eq $scratch.depth "mantle" }}
      {{ with gt (len $scratch.is_in) 1 }}
        <div class="ddmenu">
          <input id="toggly0" type="checkbox">
          <label for="toggly0" tabindex="1"><span class="sup">cf.&nbsp;</span><a href="{{ with (index $scratch.is_in 0) }}{{ .RelPermalink }}{{ end }}{{ if eq hugo.Environment "offline" }}index.html{{ end }}">{{ with index $scratch.is_in 0 }}<i>{{ .Title }}</i></a>&nbsp;{{ end }}</label>
          <div class="hiddendiv">
            <ul class="mt-4">
              {{ range after 1 $scratch.is_in }}
                <li><a href="{{ .RelPermalink }}{{ if eq hugo.Environment "offline" }}index.html{{ end }}"><i>{{ .Title }}</i></a></li>
              {{ end }}
            </ul>
          </div>
        </div>
      {{ else }}
        <a href="{{ with (index $scratch.is_in 0) }}{{ .RelPermalink }}{{ end }}{{ if eq hugo.Environment "offline" }}index.html{{ end }}"><span class="supt">{{ with (index $scratch.is_in 0) }}{{ substr .Title 0 1  }}</span><i>{{ substr .Title 1 }}</i></a>{{ end }}
      {{ end  }}
      <span class="mantlebar"><i> » <a href="{{ .RelPermalink }}{{ if eq hugo.Environment "offline" }}index.html{{ end }}">{{ .Title }}</a></i></span>
    {{ end }}

    {{ if eq $scratch.depth "crust" }}
      {{ with gt (len $is_in_is_in) 1 }}
        <div class="ddmenu">
          <input id="toggly1" type="checkbox">
          <label for="toggly1" tabindex="1"><span class="sup">cf.&nbsp;</span><a href="{{ with (index $is_in_is_in 0) }}{{ .RelPermalink }}{{ end }}{{ if eq hugo.Environment "offline" }}index.html{{ end }}">{{ with (index $is_in_is_in 0) }}<i>{{ .Title }}</i></a>&nbsp;{{ end }}</label>
          <div class="hiddendiv">
            <ul class="mt-4">
              {{ range after 1 $is_in_is_in }}
                <li><a href="{{ .RelPermalink }}{{ if eq hugo.Environment "offline" }}index.html{{ end }}"><i>{{ .Title }}</i></a></li>
              {{ end }}
            </ul>
          </div>
        </div>
      {{ else }}
        {{ $home := index $is_in_is_in 0 }}
        <a href="{{ $home.RelPermalink }}{{ if eq hugo.Environment "offline" }}index.html{{ end }}"><span class="supt">{{ substr $home.Title 0 1  }}</span><i>{{ substr $home.Title 1 }}</i></a>
      {{ end }}

      {{ with gt (len $scratch.is_in) 1 }}
        »&nbsp;
        <div class="ddmenu">
          <input id="toggly" type="checkbox">
          <label for="toggly" tabindex="1"><span class="sup">cf.&nbsp;</span><a href="{{ with (index $scratch.is_in 0) }}{{ .RelPermalink }}{{ end }}{{ if eq hugo.Environment "offline" }}index.html{{ end }}">{{ with (index $scratch.is_in 0) }}<i>{{ .Title }}</i></a>&nbsp;{{ end }}</label>
          <div class="hiddendiv">
            <ul class="mt-4">
              {{ range after 1 $scratch.is_in }}
                <li><a href="{{ .RelPermalink }}{{ if eq hugo.Environment "offline" }}index.html{{ end }}"><i>{{ .Title }}</i></a></li>
              {{ end }}
            </ul>
          </div>
        </div>
      {{ else }}»&nbsp;<a href="{{ with (index $scratch.is_in 0) }}{{ .RelPermalink }}{{ end }}{{ if eq hugo.Environment "offline" }}index.html{{ end }}">{{ with (index $scratch.is_in 0) }}<i>{{ .Title }}</i></a>{{ end }}
      {{ end }}
      <span class="mantlebar"><i> » <a href="{{ .RelPermalink }}{{ if eq hugo.Environment "offline" }}index.html{{ end }}">{{ .Title }}</a></i></span>
    {{ end  }}
  </div>
</div>
