<?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>
                    <a href="?f={/ShowPage/@id}&amp;param={$i}#focus" title="{//oRun[@i=$i]/MDaaS/step[last()]/@time}">View</a>
                </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="oRun" mode="data">
        <td>
            <xsl:apply-templates select="MDaaS/parameter" mode="param"/>
        </td>
        <td>
            <xsl:apply-templates select="MDaaS/step[last()]/output" mode="param"/>
        </td>
        <xsl:if test="/ShowPage/Edit[contains(@link,'edit')]">
            <td>
                <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: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>
