<template:meta default-text-matching="regex"/>
<template:switch-prioritized>
   <DIV t:condition="matches(@id, 'hitlist|speed_result_list_')">
    <DIV t:condition="tokenize(@class)=('box', 'trefferbox')">
      {nextPage := (), box-header := ()}
      <DIV class="box-header">
        {box-header := .,
         (.//*[starts-with(name(), "h")]/extract(., "[0-9]+")[.])[1] ! (search-result-count := .)
         }
      </DIV>?
      <h1 t:test="not(get('search-result-count', ()))" 
          t:condition="contains(., 'Treffer')">{search-result-count := extract(., "[0-9]+")}</h1>?
      <t:s>( 
         let $pagination := if ($box-header) then $box-header//div[tokenize(@class, ' ') = ('box-right','pagination')] 
                            else ()
         let $pagination := if ($pagination) then $pagination
                            else .//*[tokenize(@class, ' ') = ('box-right','pagination')] 
         return $pagination[1]
       ) ! (
       for $href in .//a/@href
       let $cp := extract($href,"(?:hitList.*curPos=|javascript.*move\( *[0-9]+ *, *)([0-9]+)", 1)
       where $cp and $cp > $curPos
       order by $cp ascending
       count $c
       where $c = 1
       return (
        $curPos := $cp,
        $nextPage := if (contains($href, "javascript")) then 
         let $args := extract($href, "\( *([0-9]+) *, *([0-9]+) *\)", (1,2) )
         return
           x"speedHitList.do?methodToCall=refineHitList&speedHitlistUrl=speedHitList.do&newMemorizeListName=&databaseId={$args[1]}&dbIdentifier={$args[1]}&pos={$args[2]}"
         else
          resolve-html($href))
       
      )</t:s>

      
      <TABLE class="data">
        <TR>
        <TD t:condition="@style or @class"> <!-- condition to skip image column -->
        {book := {"orderable": false()}, hasZ3988 := false()}
        <!--{book := {"orderable": .//a[contains(., 'vormerk') or contains(., 'bestell')]},          if ($book.orderable) then (book.order0 := $book.orderable/@href, book.orderTitle :=  $book.orderable) else (),          hasZ3988 := false()}-->
        <span class="Z3988">{hasZ3988 := true(),
        for $entry in tokenize(@title, "&amp") return 
           if (starts-with($entry, "rft.au=")) then book.author :=               uri-decode(substring-after($entry, "="))
           else if (starts-with($entry, "rft.isbn=")) then book.isbn :=          uri-decode(substring-after($entry, "="))
           else if (starts-with($entry, "rft.pub=")) then book.publisher :=      uri-decode(substring-after($entry, "="))
           else if (starts-with($entry, "rft.date=")) then book.year :=          uri-decode(substring-after($entry, "="))
           else if (starts-with($entry, "rft.atitle=") or starts-with($entry, "rft.btitle=") (:or starts-with($entry, "rft.tpages=") :)) then 
             book.title := concat($book.title, " ", uri-decode(substring-after($entry, "=")))
           else if (starts-with($entry, "rft_id=")) then book.home-url :=        uri-decode(substring-after($entry, "="))
           else ()      
        }</span>?
                    
        <A t:condition="contains(@href, 'singleHit')">{book._detail-url:=@href, if (not($hasZ3988) or $book.title = "") then book.title := (if (node-name(..) eq "div") then .. else .) else ()}</A>
        <t:if test="not($hasZ3988)">
          {if (exists($book.title!following-sibling::comment()[contains(., "Autor")])) then (
              $book.year := normalize-space(text()),  (: special output in winterthur :)
              $book.author := $book.title!following-sibling::br[2]/preceding-sibling::text()[1]  (: use preceding instead following to skip over comment :)
            ) else 
              book.author := normalize-space(text())
          }
          <strong class="c2">Band<t:s>
            temp := ./following-sibling::text()[1],
            book.subTitle := replace($temp, "\( *[0-9]+ *\)", ""), 
            book.year := extract($temp, "([0-9]{4})[. )]", 1)
          </t:s></strong>?
          <t:s>          
          typeswitch ($book.title) 
            case node() return
              if ($book.author = "" and not($book.subTitle) and exists($book.title/following-sibling::br[1])) then (
                brs := (for tumbling window $w in $book.title/following-sibling::node() start when true() end $e when $e/(self::br or (self::comment() and contains(., "br")) )
                       return normalize-space(join($w[not(self::div[contains(@id, "ncip")] or self::comment())]!inner-text())))[position() le 3], 
                if ($brs[1] = "") then $brs := tail($brs) else (),
                if (matches($brs[1], "[0-9]{2,4}")) then 
                  brs := (replace($brs[1], "([\s-]*)[0-9]{2,}", ""), extract($brs[1], "[0-9]+"), tail($brs))
                else (),
                if (normalize-space($brs[1])) then book.author :=  $brs[1] else (
                  let $split := extract(normalize-space($book.title/following-sibling::text()[1]), "(.*?)(\.[  \t\n\r]*)?-[  \t\n\r]*([0-9]+)",(1,3)) 
                  return if ($split[1]) then (
                    $book.author := $split[1],
                    $book.year := $split[2]
                  ) else ()),
                if (($book.author = "von" or $book.author = "") 
                     and (typeswitch ($brs[1]) case node() return $brs[1]/following-sibling::a and ($brs[1]/following-sibling::a)[1] &lt;&lt; $brs[2]  
                                              default return false()
                     )) then book.author := $brs[1]/following-sibling::a[1] 
                else if (matches($book.author, "^(ausleihbar|(entliehen|bestellbar).*)$")) then book.author := ""  (: pulheim :)
                else (),
                if (matches($brs[2], "[0-9]{2}")) then book.year := $brs[2] else ()
                (:,book.id := $brs[3] this can also be more title data (e.g. basel):)
              ) else () 
            default return ()
          </t:s>
        </t:if>
        
       <!-- <SPAN class="textgruen">{$book.availability}</SPAN>? who cares?-->
        </TD>
        </TR>*
      </TABLE>
    </DIV>    
  </DIV>

  <div id="main">
    {search-result-count:=1, singleBookResult:= base-uri() }
    <a>Titeldaten|mehr zum Titel<t:s>
      if (starts-with(@href, "#")) then () else singleBookResult := resolve-html() 
    </t:s></a>?
    <div t:condition="(@id,@class)=('tab-content', 'detailsTabs-titleinfo', 'box-container')">
      <template:switch>
        <table class="data"/>
        <table class="data2"/>
        <table t:condition="@class=('fulltitle','titleinfo','data','data2')"/>
      </template:switch>
    </div>
  </div>
  
  <main id="main"> <!-- bremen, chemnitz, etc. -->
   <div class="hitlist">
     <section class="box">
       <h1>{extract(., "[0-9]+")[.] ! (search-result-count := .)}</h1>
       <ul class="pagination">
         <a title="Nächste Seite">{$nextPage := resolve-html()}</a>?
       </ul>
     
       <div class="container-fluid">
         <div class="row">
           {book := {"orderable": false()}}
           <h2 class="recordtitle">
             {$book.title}
             <a>{$book._detail-url := resolve-html() }</a>
             {let $text := following-sibling::text()!normalize-space()[.]
              let $text := if ($text[1] => matches("[0-9]+")) then ("", $text) else $text
              return (
                $book.author := $text[1],
                $book.year := $text[2],
                $book.libraryLocation := $text[3]
              )
             }
           </h2>
           <div class="bibtip_isxn">{$book.isbn}</div>?	
         </div>+
       </div>
     </section>
   </div>
  </main>
  
  <div id="main"> <!-- bremen, chemnitz, etc. -->
    <ul class="singlehit-tabs">
      {search-result-count:=1, singleBookResult:= base-uri() }
      <a>Titeldaten|mehr zum Titel<t:s>
        if (starts-with(@href, "#")) then () else singleBookResult := resolve-html() 
      </t:s></a>?
    </ul>
  </div>
    
  <t:element t:condition="(self::h1 or self::h2) and matches(normalize-space(), 'Ihre Suche erzielte ( *leider *)?keine Treffer')">{search-result-count:=0}</t:element>
  
  <form>
    <input type="hidden" name="speedHitlistUrl" value="speedHitList.do" /> 
    <script>
    {$speedPage := resolve-uri(extract(., 'url: *"([^"]+)"', 1)) || "?databaseId=" || extract(., 'databaseId: "([0-9]+)"', 1)  }
    </script>
    
  </form>
  
  <div class="alert message-error">{vl:raise(.)}</div> <!-- for debugging -->
  <div class="error">{vl:raise(.)}</div>
  
  
  
</template:switch-prioritized>
