<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output version="1.0" encoding="UTF-8" method="xml" indent="no" omit-xml-declaration="yes"/>
  <xsl:strip-space elements="*"/>

  <!-- external parameters -->
  <!-- flag indicating whether or not to display branch images -->
  <xsl:param name="showimages"/>
  <!-- flag indicating whether to display hits in the toc -->
  <xsl:param name="showhitcount"/>
  <!-- path to images on the NXT server -->
  <xsl:param name="imagepath"/>
  <!-- name of browser -->
  <xsl:param name="browsername"/>
  <!-- version of browser -->
  <xsl:param name="browserversion"/>

  <xsl:variable name="command" select="/toc/command"/>
  <xsl:variable name="basepathidraw" select="/toc/basepathid"/>
  <xsl:variable name="depth" select="/toc/depth"/>
  <xsl:variable name="basepathtitle" select="/toc/basepathtitle"/>
  <xsl:variable name="syncpath" select="/toc/syncpath"/>
  <xsl:variable name="start" select="/toc/start"/>
  <xsl:variable name="direction" select="/toc/direction"/>

  <!-- NXT-931: in IE10 xslt processor may add additional line breaks,
  that may come here from /toc/basepathid into node id, so, we must cut them -->
    <xsl:variable name="basepathid">
        <xsl:choose>
            <xsl:when test="($browsername='MSIE')and(number($browserversion) &gt;= 10)">
                <xsl:call-template name="string-replace">
                    <xsl:with-param name="string" select="$basepathidraw"/>
                    <xsl:with-param name="find" select="'&#xA;'"/>
                    <xsl:with-param name="replace"></xsl:with-param>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$basepathidraw"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

  <xsl:template match="nodes">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="command|basepathid|basepathtitle|depth|syncpath|start|direction"/>

  <!-- process node element -->
  <xsl:template match="n">
    <li>
      <!-- select a class name for node -->
      <!-- 'ct' attribute means 'content-type' -->
      <xsl:variable name = "class">
        <xsl:choose>
          <xsl:when test="@ct='application/site'">
            <xsl:text>sitenode</xsl:text>
          </xsl:when>
          <xsl:when test="@ct='application/morenode'">
            <xsl:text>morenode</xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>treenode</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <!-- obtain an update direction for more node -->
      <!-- 'd' attribute means 'direction', possible values: 1, 2 -->
      <xsl:variable name = "direction">
        <xsl:if test="@ct='application/morenode'">
          <xsl:value-of select="@d"/>
        </xsl:if>
      </xsl:variable>

      <!-- specify a id attribute for node -->
      <!-- more node has id = parent_id + '_m' + direction -->
      <xsl:variable name = "id">
        <xsl:choose>
          <xsl:when test="@ct='application/morenode'">
            <xsl:choose>
              <xsl:when test="../@id">
                <xsl:value-of select="concat(../@id,'_m',$direction)"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="concat($basepathid,'_m',$direction)"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="@id"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <!-- select a hits count attibute -->
      <xsl:variable name = "hitcount">
        <xsl:choose>
          <xsl:when test="@h">
            <xsl:value-of select="@h"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="0"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <!-- select a plus-minus image -->
      <!-- 'hc' attribute means 'has-children' -->
      <xsl:variable name = "plusminus">
        <xsl:choose>
            <xsl:when test="@ct='application/site'">
                <xsl:value-of select="concat($imagepath,'toc-minus.png')"/>
            </xsl:when>
          <!-- if current node is folder with children, but current update does not contain childs -->
          <xsl:when test="@hc='y' and @ct='application/folder' and (not(./child::n) or not($depth))">
            <xsl:value-of select="concat($imagepath,'toc-plus.png')"/>
          </xsl:when>
          <!-- if current node is folder with children -->
          <xsl:when test="@hc='y' and @ct='application/folder'">
            <xsl:value-of select="concat($imagepath,'toc-minus.png')"/>
          </xsl:when>
          <!-- if current node is more node with children -->
          <xsl:when test="@hc='y' or @ct='application/morenode'">
            <xsl:value-of select="concat($imagepath,'toc-plus.png')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="concat($imagepath,'toc-nochildren.gif')"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <!-- fill node's attributes -->
      <!-- id attribute -->
      <xsl:attribute name="id">
        <xsl:value-of select="$id"/>
      </xsl:attribute>
      <!-- set classname -->
      <xsl:attribute name="class">
        <xsl:value-of select="$class"/>
      </xsl:attribute>
      <!-- content-type attribute -->
      <xsl:attribute name="ct">
        <xsl:value-of select="@ct"/>
      </xsl:attribute>
      <!-- has-children attribute -->
      <xsl:attribute name="hc">
        <xsl:value-of select="@hc"/>
      </xsl:attribute>
      <!-- name attribute -->
      <xsl:attribute name="n">
        <xsl:value-of select="@n"/>
      </xsl:attribute>
      <!--  offline attribute -->
      <xsl:if test="@o">
        <xsl:attribute name="o">
          <xsl:value-of select="@o"/>
        </xsl:attribute>
      </xsl:if>
      <!-- hitcount attribute -->
      <xsl:attribute name="h">
        <xsl:value-of select="$hitcount"/>
      </xsl:attribute>

      <!-- has-content attribute -->
      <xsl:attribute name="cn">
        <xsl:choose>
          <!-- if current node in the XML has nc attribute -->
          <!-- 'nc' in the XML means 'no-content' -->
          <xsl:when test="@nc">
            <xsl:choose>
              <!-- if no-content then set has-content to 'n' -->
              <xsl:when test="@nc='y'">
                <xsl:text>n</xsl:text>
              </xsl:when>
              <xsl:otherwise>
                <xsl:text>y</xsl:text>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:when>
          <!-- if no no-content attribute then set has-content to 'y' -->
          <xsl:otherwise>
            <xsl:text>y</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>

      <!-- need-children attribute -->
      <xsl:attribute name="nc">
        <xsl:choose>
          <!-- if update has child nodes for the current node, no children needed -->
          <xsl:when test="./child::n">
            <xsl:text>n</xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:choose>
              <!-- if current node has children, set need-children to 'y' -->
              <xsl:when test="@hc='y'">
                <xsl:text>y</xsl:text>
              </xsl:when>
              <xsl:otherwise>
                <xsl:text>n</xsl:text>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>

      <!-- checked attribute -->
      <xsl:if test="@ck">
        <xsl:attribute name="ck">
          <xsl:value-of select="@ck"/>
        </xsl:attribute>
      </xsl:if>

      <a tabindex="5">
        <xsl:attribute name="href">
          <xsl:text>#</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="class">
          <xsl:text>plusminus</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="onclick">
          <xsl:text>nxt.comp.toc.plusMinusClicked(event); return false;</xsl:text>
        </xsl:attribute>

        <!-- format plus-minus image -->
        <img>
          <xsl:attribute name="class">
            <xsl:text>plusminus</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="src">
            <xsl:value-of select="$plusminus"/>
          </xsl:attribute>
          <xsl:attribute name="width">
            <xsl:text>13</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="height">
            <xsl:text>13</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="onclick">
            <xsl:text>nxt.comp.toc.plusMinusClicked(event); return false;</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="alt">
            <xsl:choose>
              <xsl:when test="contains($plusminus, 'plus')">
                <xsl:text>Expand Node</xsl:text>
              </xsl:when>
              <xsl:when test="contains($plusminus, 'minus')">
                <xsl:text>Collapse Node</xsl:text>
              </xsl:when>
              <xsl:otherwise>
                <xsl:text>Node no children</xsl:text>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:attribute>
        </img>
      </a>

      <!-- if external variable showimages is set, then choose appropriate image by node's content-type -->
      <xsl:if test="$showimages='1'">
        <xsl:variable name = "nodeicon">
          <xsl:choose>
            <!-- set image for sub-document nodes -->
            <xsl:when test="@ct='application/subdocument'">
              <xsl:value-of select="concat($imagepath,'toc-leaf.png')"/>
            </xsl:when>
            <!-- set image for site nodes -->
            <xsl:when test="@ct='application/site'">
              <xsl:value-of select="concat($imagepath,'toc-site.png')"/>
            </xsl:when>
            <!-- set image for more nodes -->
            <xsl:when test="@ct='application/morenode'">
              <xsl:value-of select="concat($imagepath,'toc-more.png')"/>
            </xsl:when>
            <!-- set image for folder nodes -->
            <xsl:when test="@ct='application/folder'">
              <xsl:choose>
                <!-- offline folder -->
                <xsl:when test="@o='y'">
                  <xsl:value-of select="concat($imagepath,'toc-partialcollapsed.png')"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:choose>
                    <!-- online folder without content -->
                    <!-- this folder can be expanded, but can't be selected -->
                    <xsl:when test="@nc='y'">
                      <xsl:value-of select="concat($imagepath,'toc-collapsed.png')"/>
                    </xsl:when>
                    <xsl:otherwise>
                    <!-- online folder with content -->
                      <xsl:value-of select="concat($imagepath,'toc-collapsedhascontent.png')"/>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:when>
            <!-- set image for document node (default) -->
            <xsl:otherwise>
              <xsl:value-of select="concat($imagepath,'toc-leaf.png')"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>

        <xsl:variable name = "nodeiconalt">
          <xsl:choose>
            <xsl:when test="@ct='application/subdocument'">
              <xsl:text>Sub-document node</xsl:text>
            </xsl:when>
            <xsl:when test="@ct='application/morenode'">
              <xsl:text>More node</xsl:text>
            </xsl:when>
            <xsl:when test="@ct='application/site'">
              <xsl:text>Site node</xsl:text>
            </xsl:when>
            <xsl:when test="@ct='application/folder'">
                <xsl:choose>
                    <!-- offline folder -->
                    <xsl:when test="@o='y'">
                        <xsl:text>Closed folder node (offline)</xsl:text>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:choose>
                            <!-- online folder without content -->
                            <!-- this folder can be expanded, but can't be selected -->
                            <xsl:when test="@nc='y'">
                                <xsl:text>Closed folder node without content</xsl:text>
                            </xsl:when>
                            <xsl:otherwise>
                                <!-- online folder with content -->
                                <xsl:text>Closed folder node with content</xsl:text>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
              <xsl:text>Document node</xsl:text>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>

        <!-- format node icon image -->
        <img>
          <xsl:attribute name="class">
            <xsl:text>nodeicon</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="src">
            <xsl:value-of select="$nodeicon"/>
          </xsl:attribute>
          <xsl:attribute name="alt">
            <xsl:value-of select="$nodeiconalt"/>
          </xsl:attribute>
          <xsl:attribute name="width">
            <xsl:text>24</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="height">
            <xsl:text>18</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="onmouseover">
            <xsl:text>nxt.comp.toc.mouseOver(this)</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="onmouseout">
            <xsl:text>nxt.comp.toc.mouseOut(this)</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="onclick">
            <xsl:text>nxt.comp.toc.treeNodeClicked(event)</xsl:text>
          </xsl:attribute>
        </img>
      </xsl:if>

      <!-- format check-box image -->
      <a tabindex="5">
        <xsl:attribute name="href">
          <xsl:text>#</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="onclick">
          <xsl:text>nxt.comp.toc.checkBoxClicked(event); return false;</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="style">
          <xsl:text>display: none</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="class">
          <xsl:text>checkbox</xsl:text>
        </xsl:attribute>
        <img>
          <xsl:attribute name="style">
            <xsl:text>display: none</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="class">
            <xsl:text>checkbox</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="width">
            <xsl:text>14</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="height">
            <xsl:text>14</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="src">
            <xsl:value-of select="concat($imagepath,'toc-unchecked.png')"/>
          </xsl:attribute>
          <xsl:attribute name="alt">
            <xsl:text>Unchecked checkbox</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="onclick">
            <xsl:text>nxt.comp.toc.checkBoxClicked(event); return false;</xsl:text>
          </xsl:attribute>
        </img>
      </a>

      <!-- format hits count element -->
      <SPAN class="hitnode">
        <xsl:attribute name="onmouseover">
          <xsl:text>nxt.comp.toc.mouseOver(this)</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="onmouseout">
          <xsl:text>nxt.comp.toc.mouseOut(this)</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="onclick">
          <xsl:text>nxt.comp.toc.treeNodeClicked(event)</xsl:text>
        </xsl:attribute>
        <!-- if hits count should be hidden, set a proper style -->
        <xsl:if test="not($showhitcount='1')">
          <xsl:attribute name="style">
            <xsl:text>display: none;</xsl:text>
          </xsl:attribute>
        </xsl:if>
        <!-- set hits count -->
        <xsl:choose>
          <xsl:when test="$hitcount > 0">
            <SPAN class="hit-number">
              <xsl:value-of select="concat('(',$hitcount,')')"/>
            </SPAN>
          </xsl:when>
          <xsl:otherwise>
            <SPAN class="hitcount"></SPAN>
          </xsl:otherwise>
        </xsl:choose>
      </SPAN>

      <!-- format title element -->
      <!-- 't' attribute means 'title' -->
      <span>
        <xsl:attribute name="class">
          <xsl:text>nodetext</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="title">
          <xsl:value-of select="@t"/>
        </xsl:attribute>
        <xsl:attribute name="onmouseover">
          <xsl:text>nxt.comp.toc.mouseOver(this)</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="onmouseout">
          <xsl:text>nxt.comp.toc.mouseOut(this)</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="onclick">
          <xsl:text>nxt.comp.toc.treeNodeClicked(event); return false;</xsl:text>
        </xsl:attribute>
        <!-- keyboard support -->
        <a tabindex="5">
          <xsl:attribute name="href">
            <xsl:text></xsl:text>
          </xsl:attribute>
          <xsl:attribute name="class">
            <xsl:text>nodetext</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="onclick">
            <xsl:text>nxt.comp.toc.treeNodeClicked(event); return false;</xsl:text>
          </xsl:attribute>
          <xsl:value-of select="@t"/>
        </a>
      </span>

      <!-- format child-container if the current node has children or if it is more node -->
      <xsl:if test="@hc='y' or @ct='application/morenode'">
          <ul class="childrennode">
          <xsl:attribute name="id">
            <!-- set id for child-container -->
            <xsl:value-of select="concat($id,'_c')"/>
          </xsl:attribute>
          <xsl:choose>
            <!-- if no children then hide child-container -->
            <xsl:when test="not(./child::n) or not($depth)">
              <xsl:attribute name="style">
                <xsl:text>display: none;</xsl:text>
              </xsl:attribute>
            </xsl:when>
            <xsl:otherwise>
              <xsl:attribute name="style">
                <xsl:text>display: block</xsl:text>
              </xsl:attribute>
            </xsl:otherwise>
          </xsl:choose>
          <!-- apply stylesheet for nested nodes -->
          <xsl:apply-templates />
          </ul>
      </xsl:if>
    </li>
  </xsl:template>

    <xsl:template name="string-replace">
        <xsl:param name="string"/>
        <xsl:param name="find"/>
        <xsl:param name="replace"/>
        <xsl:choose>
            <xsl:when test="contains($string,$find)">
                <xsl:value-of select="substring-before($string,$find)" />
                <xsl:value-of select="$replace" />
                <xsl:call-template name="string-replace" >
                    <xsl:with-param name="string" select="substring-after($string,$find)" />
                    <xsl:with-param name="find" select="$find" />
                    <xsl:with-param name="replace" select="$replace" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$string" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>
