<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="mdlflot.xsl"/>
  <xsl:import href="blank.xsl"/>
  <xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>


  <xsl:template match="oRun">
    <xsl:apply-templates select="MDaaS" mode="script" />
  </xsl:template>
  <xsl:template match="oRun" mode="table">
    <xsl:variable name="i" select="@i"/>
    <xsl:if test="count(//tr[td[2]=$i])=0">
      <tr id="run{@id}">
        <xsl:if test="/ShowPage/param=@i">
          <xsl:attribute name="style">background-color: #dddddd;</xsl:attribute>
        </xsl:if>
        <xsl:if test="/ShowPage/Edit[contains(@link,'edit')]">
          <td>
            <a href="?f={/ShowPage/@id}&amp;m=remove&amp;ex={@id}&amp;ex=noresp" target="noresp"
               onclick="remove_noresp('run{@id}')">del</a>
          </td>
        </xsl:if>
        <td>
          <xsl:if test="/ShowPage/param=@i">
            <a name="focus"/>
          </xsl:if>
          <xsl:variable name="href" select="//a/@href"/>
          <xsl:variable name="target" select="//a/@target"/>
          <xsl:choose>
            <xsl:when test="$href != ''">
              <a href="{MDaaS/@program}{/ShowPage/@id}&amp;param={$i}" target="{$target}">
                <xsl:value-of select="$i"/>
              </a>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="$i"/>
            </xsl:otherwise>
          </xsl:choose>
        </td>
        <td style="white-space: nowrap;">
          <!--
                    <a href="?f={/ShowPage/@id}&amp;param={$i}#focus" title="{//oRun[@i=$i]/MDaaS/step[last()]/@time}">View</a>
          <xsl:value-of select="@t"/>
                    -->
          <xsl:value-of select="MDaaS/step[last()]/@time"/>
        </td>
        <xsl:apply-templates select="//oRun[@i=$i]" mode="data"/>
      </tr>
      <xsl:if test="/ShowPage/param=@i">
        <xsl:apply-templates select="." mode="flot"/>
      </xsl:if>
    </xsl:if>
  </xsl:template>
  <xsl:template match="oRun" mode="flot">
    <tr>
      <td>
        <xsl:choose>
          <xsl:when test="/ShowPage/Edit[contains(@link,'edit')]">
            <xsl:attribute name="colspan">6</xsl:attribute>
          </xsl:when>
          <xsl:otherwise>
            <xsl:attribute name="colspan">4</xsl:attribute>
          </xsl:otherwise>
        </xsl:choose>
        <div id="outer" style="width: 678px; padding: 10px; border: 1px solid gray;">
          <div id="placeholder" style="width: 680px; height: 280px;"></div>
        </div>
      </td>
    </tr>
  </xsl:template>
  <xsl:template match="step" mode="note">
    <xsl:variable name="s">
      <xsl:call-template name="sum">
        <xsl:with-param name="i" select="2025" />
        <xsl:with-param name="t" select="output[@name='Fang']"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="$s = 26">3</xsl:when>
      <xsl:when test="$s > 13">2</xsl:when>
      <xsl:when test="$s > 3">1</xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="sum">
    <xsl:param name="i" />
    <xsl:param name="t" />
    <xsl:param name="s" select="0" />
    <xsl:choose>
      <xsl:when test="$i > 2050">
        <xsl:value-of select="$s"/>
      </xsl:when>
      <xsl:when test="number(substring-before(substring-after($t, concat($i,'.0, ')),'.')) > 14999.9">
        <xsl:call-template name="sum">
          <xsl:with-param name="i" select="$i + 1" />
          <xsl:with-param name="t" select="$t" />
          <xsl:with-param name="s" select="$s + 1" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="sum">
          <xsl:with-param name="i" select="$i + 1" />
          <xsl:with-param name="t" select="$t" />
          <xsl:with-param name="s" select="$s" />
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="oRun" mode="data">
    <td>
      <!-- Sonderbehandelung für Fischerei -->
      <xsl:choose>
        <xsl:when test="MDaaS/name='Fischerei.mdl'">
          <xsl:variable name="F" select="count(MDaaS/step[@id > 74 and output[@name='Fang']/@value > 14999])"/>
          <xsl:choose>
            <xsl:when test="$F > 12">
              <xsl:attribute name="style">
                background-color: #44ff44;
              </xsl:attribute>
            </xsl:when>
            <xsl:when test="$F > 9">
              <xsl:attribute name="style">
                background-color: #ffff44;
              </xsl:attribute>
            </xsl:when>
            <xsl:when test="$F > 5">
              <xsl:attribute name="style">
                background-color: #ffdd44;
              </xsl:attribute>
            </xsl:when>
            <xsl:otherwise>
              <xsl:attribute name="style">
                background-color: #ff4444;
              </xsl:attribute>              
            </xsl:otherwise>
          </xsl:choose>
          <xsl:value-of select="$F"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="MDaaS/parameter" mode="param"/>
          <xsl:apply-templates select="MDaaS/step[@id='100']" mode="note"/>
        </xsl:otherwise>
      </xsl:choose>

    </td>
    <td>
      <xsl:apply-templates select="MDaaS/step[last()]/output" mode="param"/>
    </td>
    <xsl:if test="/ShowPage/Edit[contains(@link,'edit')]">
      <td style="white-space: nowrap;">
        <xsl:choose>
          <xsl:when test="count(MDaaS/step[@user != ''])=0">
            <xsl:apply-templates select="MDaaS/@user" mode="polling"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates select="MDaaS/step[@user != '']" mode="user"/>
          </xsl:otherwise>
        </xsl:choose>
      </td>
    </xsl:if>
  </xsl:template>
  <xsl:template match="parameter|output" mode="param">

    <xsl:apply-templates select="." mode="title" />
    <xsl:text>=</xsl:text>
    <xsl:apply-templates select="." mode="value"/>
    <xsl:text> </xsl:text>

  </xsl:template>
  <xsl:template match="ShowPage" mode="content">
    <xsl:if test="/ShowPage/Agent[.='MSIE' and @version &lt; '9.0']">
      <script language="JavaScript" type="text/javascript" src="/flot/excanvas.js"></script>
    </xsl:if>
    <script language="javascript" type="text/javascript" src="/flot/jquery.js"></script>
    <script language="javascript" type="text/javascript" src="/flot/jquery.flot.js"></script>
    <script language="javascript" type="text/javascript" src="/flot/jquery.flot.symbol.js">   </script>
    <script language="javascript" type="text/javascript">
      function remove_noresp(id){
      var e = document.getElementById(id);
      e.style.display = 'none';
      }
    </script>
    <style type="text/css">
      td.legendLabel, td.legendColorbox {border: 0px;}
      .SimpleT td {font-size: 8pt;}
    </style>
    <xsl:variable name="p" select="/ShowPage/param"/>
    <xsl:apply-templates select="oRun[@i=$p]"/>
    <table cellpading="0" cellspacing="0">
      <tr>
        <td>
          <div id="maintext">
            <xsl:choose>
              <xsl:when test="mComment/MainText != ''">
                <xsl:for-each select="mComment[count(MainText)!=0]">
                  <xsl:if test="position()=last()">
                    <xsl:value-of select="MainText" disable-output-escaping="yes"/>
                  </xsl:if>
                </xsl:for-each>
              </xsl:when>
              <xsl:otherwise>
                <h3>
                  <xsl:value-of select="sTitle"/>
                </h3>
                <xsl:apply-templates select="sTitle" mode="Inhalt"/>
              </xsl:otherwise>
            </xsl:choose>

          </div>

        </td>
      </tr>

    </table>
    <div id="contentblock">
      <iframe name="noresp" id="noresp" style="display: none;"></iframe>
      <xsl:if test="count(ListOption/@nosubobj)=0">
        <br />
        <xsl:apply-templates select="." mode="Untergeordnet"/>
      </xsl:if>
      <div id="MainFooter">
      </div>
    </div>
  </xsl:template>
  <xsl:template match="step" mode="user">
    <xsl:variable name="u" select="@user"/>
    <xsl:choose>
      <xsl:when test="position()=1">
        <xsl:apply-templates select="@user" mode="polling"/>
      </xsl:when>
      <xsl:when test="count(preceding-sibling::step[@user = $u])=0">
        <xsl:text> </xsl:text>
        <xsl:apply-templates select="@user" mode="polling"/>
      </xsl:when>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="tbody">
    <xsl:choose>
      <xsl:when test="/ShowPage/Edit[contains(@link,'edit')]">
        <xsl:apply-templates />
        <xsl:apply-templates select="/ShowPage/oRun" mode="table">
          <xsl:sort select="substring(MDaaS/step[last()]/@time,7,4)" data-type="number" order="descending"/>
          <xsl:sort select="substring(MDaaS/step[last()]/@time,4,2)" data-type="number" order="descending"/>
          <xsl:sort select="substring(MDaaS/step[last()]/@time,1,2)" data-type="number" order="descending"/>
          <xsl:sort select="substring(MDaaS/step[last()]/@time,12,2)" data-type="number" order="descending"/>
          <xsl:sort select="substring(MDaaS/step[last()]/@time,15,2)" data-type="number" order="descending"/>
          <xsl:sort select="substring(MDaaS/step[last()]/@time,18,2)" data-type="number" order="descending"/>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="u" select="/ShowPage/LogonUser"/>
        <xsl:apply-templates select="/ShowPage/oRun[MDaaS[@user=$u or count(step[@user=$u])>0]]" mode="table"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="tr">
    <xsl:variable name="td" select="td[2]"/>
    <xsl:choose>
      <xsl:when test="$td > 0">
        <tr>
          <xsl:if test="/ShowPage/param=$td">
            <xsl:attribute name="style">background-color: #dddddd;</xsl:attribute>
          </xsl:if>
          <xsl:apply-templates select="td" mode="link"/>
          <td>
            <a href="?f={/ShowPage/@id}&amp;param={$td}">View</a>
          </td>
          <xsl:if test="count(//oRun[@i=$td])=0">
            <td colspan="3"/>
          </xsl:if>
          <xsl:apply-templates select="//oRun[@i=$td]" mode="data"/>
        </tr>
        <xsl:if test="/ShowPage/param=$td">
          <xsl:apply-templates select="//oRun[@i=$td]" mode="flot"/>
        </xsl:if>
      </xsl:when>
      <xsl:otherwise>
        <tr>
          <xsl:apply-templates />
        </tr>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="td" mode="link">
    <xsl:variable name="href" select="//a/@href"/>
    <xsl:variable name="target" select="//a/@target"/>
    <td>
      <xsl:choose>
        <xsl:when test="$href != ''">
          <a href="{$href}&amp;param={.}" target="{$target}">
            <xsl:value-of select="."/>
          </a>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="."/>
        </xsl:otherwise>
      </xsl:choose>
    </td>
  </xsl:template>
</xsl:stylesheet>
