<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- localized strings -->
<xsl:variable name='ColumnHeader_Date'>日付</xsl:variable>
<xsl:variable name='ColumnHeader_Time'>時刻</xsl:variable>
<xsl:variable name='ColumnHeader_From'>差出人</xsl:variable>
<!--xsl:variable name='ColumnHeader_To'>宛先</xsl:variable-->
<xsl:variable name='ColumnHeader_Message'>メッセージ</xsl:variable>

<!-- variables -->
<xsl:variable name='Debug'>0</xsl:variable>

<xsl:variable name='TableStyle'>font-family:MS UI Gothic; xfont-size:75%; text-align:left; vertical-align:top; table-layout:fixed;</xsl:variable>
<xsl:variable name='HeaderStyle'>border-bottom:1 solid black</xsl:variable>

<xsl:variable name='UseZebraStripe'>1</xsl:variable>
<xsl:variable name='ZebraStripeStyle'>background-color:#e0edff</xsl:variable>

<xsl:variable name='MostRecentSessionFirst'>0</xsl:variable>

<xsl:variable name='LogFirstSessionID' select="/Log/@FirstSessionID"/>
<xsl:variable name='LogLastSessionID' select="/Log/@LastSessionID"/>

<xsl:template match="Log">

    <html dir='ltr'>
    <head>
        <title>
            Message Log for <xsl:value-of select="@LogonName"/>
            <xsl:if test="$Debug = 1"> (Debug)</xsl:if>
        </title>

        <xsl:if test="$Debug = 1">
            <span style="font-family:trebuchet ms; font-size:120%">
                Debug Version
            </span>
            <hr/>
        </xsl:if>
		<style>
		    H1 { background-color:#EEEEEE;border-color:#AAAAAA;border-width:1px;border-style:solid;margin:0px;padding-left:4px; }
		</style>
    </head>

    <body style='margin:0;' onLoad="waitmsg.style.display='none',document.body.style.cursor='auto';">
		<DIV id="waitmsg" style="display:none;z-index:1;position:absolute;left:15%;right:15%;top:20%;background-color:#FFFFFF;border-color:#999999 #333333 #333333 #999999;border-width:2px;border-style:solid;margin:0px;padding:2px;filter:alpha(opacity=90,finishopacity=90,style=1,startx=0,starty=100,finishx=0,finishy=100)">
		<A name="rendering" />
		<H1>Rendering Now!</H1>
		<P style="margin:32px;">
			履歴をレンダリング中です。セッションリストのリンクをクリックするのは、しばらくお待ちください。
		</P>
		</DIV>
		<SCRIPT>
			<xsl:comment>
				document.body.style.cursor='wait';
				waitmsg.style.display='block';
			//</xsl:comment>
		</SCRIPT>
		<A name="session" />
		<H1>Session List</H1>
		<table style="{$TableStyle}" cellspacing='0' xwidth="100%">
            <col style="width:80px;"/>
            <col style="width:60px;"/>
            <col style="width:80px;"/>
            <col style="width:60px;"/>
            <col style="width:40px; text-align:right; padding-right:8px;"/>
            <col style="width:50px;"/>
            <col xwidth="*%"/>

            <thead>
	    	    <tr>
				    <th style="{$HeaderStyle}">開始日付</th>
				    <th style="{$HeaderStyle}">開始時刻</th>
				    <th style="{$HeaderStyle}">終了日付</th>
				    <th style="{$HeaderStyle}">終了時刻</th>
				    <th style="{$HeaderStyle}">計</th>
				    <th style="{$HeaderStyle}">差出人</th>
				    <th style="{$HeaderStyle}">開始メッセージ</th>
				</tr>
            </thead>
            <tbody style='vertical-align:top'>
				<xsl:call-template name="session_list"/>
            </tbody>
		</table>

		<A name="message" />
		<H1>Message List</H1>
		<table id='BodyTable' style="{$TableStyle}" cellspacing='0' xwidth="100%">

			<xsl:if test="$Debug = 1">
				<col style="vertical-align:top; width:1px;"/>
			</xsl:if>

			<col style="width:80px;"/>
			<col style="width:60px;"/>
			<col style="width:50px;"/>
			<!--col style="width:40px;"/-->
			<col xwidth="*%"/>

			<thead>
				<tr>
					<xsl:if test="$Debug = 1">
						<th style="{$HeaderStyle}">SID</th>
					</xsl:if>
					<th style="{$HeaderStyle}">
						<nobr><xsl:value-of select="$ColumnHeader_Date"/></nobr>
					</th>
					<th style="{$HeaderStyle}">
						<nobr><xsl:value-of select="$ColumnHeader_Time"/></nobr>
					</th>
					<th style="{$HeaderStyle}">
						<nobr><xsl:value-of select="$ColumnHeader_From"/></nobr>
					</th>
					<!--th style="{$HeaderStyle}">
						<nobr><xsl:value-of select="$ColumnHeader_To"/></nobr>
					</th-->
					<th style="{$HeaderStyle}">
						<nobr><xsl:value-of select="$ColumnHeader_Message"/></nobr>
					</th>
				</tr>
			</thead>
		</table>
		<xsl:call-template name="message_list"/>
    </body>
    </html>

</xsl:template>

<xsl:template name="session_list">
	<xsl:param name='CurrentSessionID' select="$LogLastSessionID"/>

	<tr>
		<xsl:if test="$UseZebraStripe = 1">
			<xsl:if test="($CurrentSessionID mod 2) = 1">
				<xsl:attribute name="style">
					<xsl:value-of select="$ZebraStripeStyle"/>
				</xsl:attribute>
			</xsl:if>
		</xsl:if>

		<xsl:for-each select="*[$CurrentSessionID=./@SessionID]">
			<xsl:sort select='@DateTime'  order='ascending'/>
			<xsl:if test="1=position()">
				<td><nobr><xsl:value-of select="@Date"/></nobr></td>
				<td><nobr><xsl:value-of select="@Time"/></nobr></td>
			</xsl:if>
			<xsl:if test="last()=position()">
				<td><nobr><xsl:value-of select="@Date"/></nobr></td>
				<td><nobr><xsl:value-of select="@Time"/></nobr></td>
			</xsl:if>
		</xsl:for-each>

		<td><nobr><xsl:value-of select="count(*[$CurrentSessionID=./@SessionID])"/></nobr></td>

		<xsl:for-each select="*[$CurrentSessionID=./@SessionID]">
			<xsl:sort select='@DateTime'  order='ascending'/>
			<xsl:if test="1=position()">
				<td>
					<A>
						<xsl:attribute name="href">#<xsl:value-of select="$CurrentSessionID"/></xsl:attribute>
						<nobr><xsl:apply-templates select="From/User"/></nobr>
					</A>
				</td>
				<td>
					<span>
						<xsl:attribute name="style">
							<xsl:value-of select="Text/@Style"/>
						</xsl:attribute>
						<xsl:value-of select="Text"/>
					</span>
				</td>
			</xsl:if>
		</xsl:for-each>
	</tr>

	<xsl:if test="$CurrentSessionID!=$LogFirstSessionID">
		<xsl:call-template name="session_list">
			<xsl:with-param name='CurrentSessionID' select="$CurrentSessionID -1"/>
		</xsl:call-template>
	</xsl:if>
</xsl:template>

<xsl:template name="message_list">
	<xsl:param name='CurrentSessionID' select="$LogFirstSessionID"/>

	<A name="{$CurrentSessionID}"/>

    <table id='BodyTable' style="{$TableStyle}" cellspacing='0' xwidth="100%">

		<xsl:if test="$Debug = 1">
			<col style="vertical-align:top; width:1px;"/>
		</xsl:if>

		<col style="width:80px;"/>
		<col style="width:60px;"/>
		<col style="width:50px;"/>
		<!--col style="width:40px;"/-->
		<col xwidth="*%"/>

        <tbody style='vertical-align:top'>
			<xsl:apply-templates select="*[$CurrentSessionID=./@SessionID]">
				<xsl:sort select='@SessionID' order='ascending' data-type='number'/>
				<xsl:sort select='@DateTime'  order='ascending'/>
			</xsl:apply-templates>

			<xsl:comment>
            <xsl:choose>

                <!-- newest session first -->
                <xsl:when test="$MostRecentSessionFirst = 1">
                    <xsl:apply-templates>
                        <xsl:sort select='@SessionID' order='descending' data-type='number'/>
                        <xsl:sort select='@DateTime'  order='ascending'/>
                    </xsl:apply-templates>
                </xsl:when>

                <!-- oldest session first -->
                <xsl:otherwise>
                    <xsl:apply-templates>
                        <xsl:sort select='@SessionID' order='ascending' data-type='number'/>
                        <xsl:sort select='@DateTime'  order='ascending'/>
                    </xsl:apply-templates>
                </xsl:otherwise>

            </xsl:choose>
			</xsl:comment>
        </tbody>
    </table>

	<xsl:if test="$CurrentSessionID!=$LogLastSessionID">
		<xsl:call-template name="message_list">
			<xsl:with-param name='CurrentSessionID' select="$CurrentSessionID +1"/>
		</xsl:call-template>
	</xsl:if>
</xsl:template>

<xsl:template match="Message">
    <tr>
        <xsl:call-template name="CommonMessageProcessing" />

        <td><nobr><xsl:apply-templates select="From/User"/></nobr></td>
        <!--td><nobr><xsl:apply-templates select="To/User"/></nobr></td-->
        <td>
            <span>
                <xsl:attribute name="style">
                    <xsl:value-of select="Text/@Style"/>
                </xsl:attribute>
                <XMP style="margin:0px;"><xsl:value-of select="Text"/></XMP>
            </span>
        </td>
    </tr>
</xsl:template>


<xsl:template match="Invitation|InvitationResponse|Join|Leave">
    <tr>
        <xsl:call-template name="CommonMessageProcessing" />

        <td/>  <!-- From -->
        <!--td/-->  <!-- To -->
        <td>
            <span>
                <xsl:attribute name="style">
                    <xsl:value-of select="Text/@Style"/>
                </xsl:attribute>
                <xsl:value-of select="Text"/>
            </span>
        </td>
    </tr>
</xsl:template>


<xsl:template match="User">
    <!-- add a comma before all but the first user -->
    <xsl:if test="position() != 1">, </xsl:if>

    <xsl:value-of select="@FriendlyName"/>
</xsl:template>


<xsl:template name="CommonMessageProcessing">
    <!-- zebra-stripe the sessions -->
    <xsl:if test="$UseZebraStripe = 1">
        <xsl:if test="(@SessionID mod 2) = 1">
            <xsl:attribute name="style">
                <xsl:value-of select="$ZebraStripeStyle"/>
            </xsl:attribute>
        </xsl:if>
    </xsl:if>

    <xsl:if test="$Debug = 1">
        <td> <xsl:value-of select="@SessionID"/> </td>
    </xsl:if>

    <td><nobr><xsl:value-of select="@Date"/></nobr></td>
    <td><nobr><xsl:value-of select="@Time"/></nobr></td>
</xsl:template>


</xsl:stylesheet>
