<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0">
   <xsl:param name="data-type">ctry</xsl:param>

   <xsl:param name="data-level">1</xsl:param>

   <xsl:param name="data-id">1</xsl:param>

   <xsl:param name="shape_context">0</xsl:param>

   <xsl:param name="max-pts">3</xsl:param>

   <xsl:param name="ptsStr">0:1:3</xsl:param>

   <xsl:param name="context-scalar">50</xsl:param>

   <xsl:param name="data-shape">1</xsl:param>

   <xsl:param name="Last-N">4</xsl:param>

   <xsl:param name="game-before">110</xsl:param>

   <xsl:param name="data_view">2</xsl:param>
   <xsl:param name="LGE_lastN">6</xsl:param>
   <xsl:param name="LGE_ptsStr">0:1:3</xsl:param>
   <xsl:param name="LGE_max-pts">3</xsl:param>

   <xsl:template match="/">

      <xsl:choose>
         <xsl:when test="$data_view = 1">
      <xsl:variable name="nFocus" select="//*[local-name() = $data-type and @id=$data-id]" />
            <getthis id="data">
            <xsl:variable name="Teams" select="$nFocus//TM" />
           <LGES>
              <xsl:apply-templates select="$Teams" mode="table"/>           	           	
            </LGES>
             </getthis>

         </xsl:when>

         <xsl:when test="$data_view = 2">
       <xsl:variable name="nFocus" select="//*[local-name() = $data-type and @id=$data-id]" />
           <xsl:variable name="nScored-Teams">
               <xsl:apply-templates select="$nFocus//TM" />
            </xsl:variable>

            <xsl:variable name="Scored-Teams" select="msxsl:node-set($nScored-Teams)/TM" />

            <getthis id="data">
               <xsl:copy-of select="//data/FIXDATES" />

               <FIXLST>
                  <xsl:apply-templates select="$nFocus//FIXLST/R" mode="fill-fix">
                     <xsl:with-param name="Teams" select="$Scored-Teams" />
                  </xsl:apply-templates>
               </FIXLST>

               <TMS>
                  <xsl:copy-of select="$Scored-Teams" />
               </TMS>
            </getthis>
         </xsl:when>
         
         <xsl:when test="$data_view = 3">
		      <xsl:variable name="nFocus" select="//*[@lvl = $data-level and @id=$data-id]" />
		            <getthis id="data">
		            <STATS>
		<xsl:copy-of select="$nFocus" />
		            </STATS>
		 
		             </getthis>
         
         </xsl:when>
         
         

         <xsl:otherwise>
         <STATS/>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>
   
   

   
<!-- Table View ###################################################-->
   <xsl:template match="TM" mode="table">
      <TM>
      <xsl:copy-of select="@*" />

		<xsl:call-template name = "table" >
		   <xsl:with-param name="Results" select="R" />
		   <xsl:with-param name="name" select="'F'" />
		   <xsl:with-param name="LN" select="50" />
		</xsl:call-template>
		<xsl:call-template name = "table" >
		   <xsl:with-param name="Results" select="R[@v='H']" />
		   <xsl:with-param name="name" select="'H'" />
		   <xsl:with-param name="LN" select="50" />
		</xsl:call-template>
		<xsl:call-template name = "table" >
		   <xsl:with-param name="Results" select="R[@v='A']" />
		   <xsl:with-param name="name" select="'A'" />
		   <xsl:with-param name="LN" select="50" />
		</xsl:call-template>
		<xsl:call-template name = "table" >
		   <xsl:with-param name="Results" select="R[position() &lt;= $LGE_lastN]" />
		   <xsl:with-param name="name" select="'F'" />
		   <xsl:with-param name="LN" select="$LGE_lastN" />
		</xsl:call-template>
		
		<xsl:call-template name = "table" >
		   <xsl:with-param name="Results" select="R[@v='H'][position() &lt;= $LGE_lastN]" />
		   <xsl:with-param name="name" select="'H'" />
		   <xsl:with-param name="LN" select="$LGE_lastN" />
		</xsl:call-template>
		<xsl:call-template name = "table" >
		   <xsl:with-param name="Results" select="R[@v='A'][position() &lt;= $LGE_lastN]" />
		   <xsl:with-param name="name" select="'A'" />
		   <xsl:with-param name="LN" select="$LGE_lastN" />
		</xsl:call-template>
		
      </TM>

   </xsl:template>



   <xsl:template name="table">
      <xsl:param name="Results" />
      <xsl:param name="name" />
      <xsl:param name="LN" />
      <xsl:variable name="wpoints" select="translate('W','L:D:W',$LGE_ptsStr)" />
      <xsl:variable name="dpoints" select="translate('D','L:D:W',$LGE_ptsStr)" />
      <xsl:variable name="lpoints" select="translate('L','L:D:W',$LGE_ptsStr)" />

      <xsl:variable name="P" select="count($Results)" />

      <xsl:variable name="W" select="count($Results[@r='W'])" />

      <xsl:variable name="D" select="count($Results[@r='D'])" />

      <xsl:variable name="L" select="count($Results[@r='L'])" />

      <xsl:variable name="F" select="sum($Results/@f)" />

      <xsl:variable name="A" select="sum($Results/@g)" />

      <xsl:variable name="Pts" select="($L * $lpoints) + ($D * $dpoints) + ($W * $wpoints)" />

      <xsl:variable name="PC" select="format-number(100*$Pts div ($P * $LGE_max-pts),'0')" />
      
      <RC nm="{$name}" P="{$P}" W="{$W}" D="{$D}" L="{$L}" F="{$F}" A="{$A}" pts="{$Pts}" PC="{$PC}" ln="{$LN}"/>


   </xsl:template>











<!-- Scored Fixture View ###################################################-->

   <xsl:template match="R" mode="fill-fix">
      <xsl:param name="Teams" />

      <xsl:variable name="hid" select="@h" />

      <xsl:variable name="HT" select="$Teams[@id = $hid]" />

      <xsl:variable name="aid" select="@a" />

      <xsl:variable name="AT" select="$Teams[@id = $aid]" />

      <xsl:variable name="ht-X" select="$HT/@maxscore" />

      <xsl:variable name="ht-N" select="format-number($HT/@nscore div $ht-X,'0.00')" />

      <xsl:variable name="ht-V" select="format-number($HT/@hscored div $ht-X,'0.00')" />

      <xsl:variable name="at-X" select="$AT/@maxscore" />

      <xsl:variable name="at-N" select="format-number($AT/@nscore div $at-X,'0.00')" />

      <xsl:variable name="at-V" select="format-number($AT/@ascored div $at-X,'0.00')" />

      <xsl:variable name="tot-N" select="format-number(100*($ht-N - $at-N),'0')" />

      <xsl:variable name="tot-V" select="format-number(100*($ht-V - $at-V),'0')" />

      <FIX htx="{$ht-X}" htn="{$ht-N}" htv="{$ht-V}" atx="{$at-X}" atn="{$at-N}" atv="{$at-V}" totn="{$tot-N}" totv="{$tot-V}">
         <xsl:copy-of select="@*" />

<!--xsl:copy-of select="$HT" />
         <xsl:copy-of select="$AT" /-->
      </FIX>
   </xsl:template>

   <xsl:template match="R">
      <xsl:copy-of select="." />
   </xsl:template>

   <xsl:template match="TM">
      <xsl:variable name="Results" select="R[position() &lt;= $Last-N]" />

      <xsl:variable name="nscored-result">
         <xsl:call-template name="process-result">
            <xsl:with-param name="Results" select="$Results" />

            <xsl:with-param name="Count" select="count($Results)" />

            <xsl:with-param name="base" select="100" />

            <xsl:with-param name="factor" select="$game-before" />
         </xsl:call-template>
      </xsl:variable>

      <xsl:variable name="scored-result" select="msxsl:node-set($nscored-result)/R" />

      <xsl:variable name="goals-for" select="sum($scored-result/@f)" />

      <xsl:variable name="goals-aga" select="sum($scored-result/@g)" />

      <xsl:variable name="avg-goal-diff" select="format-number(($goals-for - $goals-aga) div count($scored-result),'0.00')" />

      <xsl:variable name="max-score" select="format-number(sum($scored-result/@max),'0.00')" />

      <xsl:variable name="nscored" select="format-number(sum($scored-result/@fval),'0.00')" />

      <xsl:variable name="hscored" select="format-number(sum($scored-result[@v='H']/@fval) + sum($scored-result[@v='A']/@vval),'0.00')" />

      <xsl:variable name="ascored" select="format-number(sum($scored-result[@v='A']/@fval) + sum($scored-result[@v='H']/@vval),'0.00')" />

      <TM maxscore="{$max-score}" nscore="{$nscored}" hscored="{$hscored}" ascored="{$ascored}">
         <xsl:copy-of select="@*" />

         <xsl:copy-of select="$scored-result" />
      </TM>
   </xsl:template>

   <xsl:template name="process-result">
      <xsl:param name="Results" />

      <xsl:param name="Count" />

      <xsl:param name="base" />

      <xsl:param name="factor" />

      <xsl:variable name="this-result" select="$Results[$Count]" />

      <xsl:if test="$this-result">
         <xsl:call-template name="process-result">
            <xsl:with-param name="Results" select="$Results" />

            <xsl:with-param name="Count" select="$Count - 1" />

            <xsl:with-param name="base" select="format-number($base * $factor div 100,'0.00')" />

            <xsl:with-param name="factor" select="$factor" />
         </xsl:call-template>

         <xsl:variable name="points" select="translate($this-result/@r,'L:D:W',$ptsStr)" />

         <xsl:variable name="fval" select="format-number($base * $points,'0.00')" />

         <xsl:variable name="vval" select="format-number($fval * $context-scalar div 100,'0.00')" />

         <xsl:variable name="max" select="format-number($base * $max-pts,'0.00')" />

         <R p1="{$points}" fval="{$fval}" vval="{$vval}" max="{$max}">
            <xsl:copy-of select="$this-result/@*" />
         </R>
      </xsl:if>
   </xsl:template>
</xsl:stylesheet>


