<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal">

  <xsl:template match="FieldRef[@ID='5a14d1ab-1513-48c7-97b3-657a5ba6c742']" mode="Number_body">
    <xsl:param name="thisNode" select="."/>
    <xsl:variable name="url">
      <xsl:choose>
        <xsl:when test="$thisNode/@FileLeafRef.Suffix='000' or not($thisNode/@FileRef)"><xsl:value-of select="$FORM_DISPLAY_HTMLURLATTRIBUTEENCODED"/>&amp;amp;ID=<xsl:value-of select="$thisNode/@ID"/></xsl:when>
        <xsl:otherwise><xsl:value-of select="concat($HttpHost,$thisNode/@FileRef)"/></xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="title">
      <xsl:choose>
        <xsl:when test="string($thisNode/@Title)!=''">
          <xsl:value-of select="$thisNode/@Title"/>
        </xsl:when>
        <xsl:when test="string($thisNode/@DiscussionTitleLookup)!=''">
          <xsl:value-of select="$thisNode/@DiscussionTitleLookup"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$thisNode/@FileLeafRef.Name"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="ratingValueLocalized">
      <xsl:choose>
        <xsl:when test="$thisNode/@*[name()=current()/@Name]=''">0</xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$thisNode/@*[name()=current()/@Name]"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="ratingsData" select="ddwrt:GetRatingsData(string($thisNode/@ID))"/>
    <xsl:variable name="controlId" select="$thisNode/@ID"/>
    <xsl:variable name="ratingValue">
      <xsl:value-of select="translate($ratingValueLocalized, ',','.')"/>
    </xsl:variable>

    <xsl:if test="$ratingsData/FeatureActivated/node()='true' and count($thisNode/preceding-sibling::Row)=0 and (./ancestor::View/ViewStyle/@ID='14' or ./ancestor::View/@BaseViewID='40')">
      <xsl:call-template name="emit_RatingsInitialization"/>
    </xsl:if>

    <xsl:if test="not($ratingsData/ItemContentTypeContainsField) or $ratingsData/ItemContentTypeContainsField/node()='true'">
      <xsl:variable name="ratingClass">
        <xsl:choose>
          <xsl:when test="number($ratingValue) &lt;=  .25">ms-rating_0</xsl:when>
          <xsl:when test="number($ratingValue) &lt;=  .75">ms-rating_0_5</xsl:when>
          <xsl:when test="number($ratingValue) &lt;= 1.25">ms-rating_1</xsl:when>
          <xsl:when test="number($ratingValue) &lt;= 1.75">ms-rating_1_5</xsl:when>
          <xsl:when test="number($ratingValue) &lt;= 2.25">ms-rating_2</xsl:when>
          <xsl:when test="number($ratingValue) &lt;= 2.75">ms-rating_2_5</xsl:when>
          <xsl:when test="number($ratingValue) &lt;= 3.25">ms-rating_3</xsl:when>
          <xsl:when test="number($ratingValue) &lt;= 3.75">ms-rating_3_5</xsl:when>
          <xsl:when test="number($ratingValue) &lt;= 4.25">ms-rating_4</xsl:when>
          <xsl:when test="number($ratingValue) &lt;= 4.75">ms-rating_4_5</xsl:when>
          <xsl:otherwise>ms-rating_5</xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:variable name="altMessage">
        <xsl:choose>
          <xsl:when test="$ratingClass='ms-rating_0'"><xsl:value-of select="string($Rows/@resource.sps.Ratings_CurrentAverageUnrated)"/></xsl:when>
          <xsl:when test="$ratingClass='ms-rating_0_5'"><xsl:value-of select="string($Rows/@resource.sps.Ratings_CurrentAverageRating0_5Stars)"/></xsl:when>
          <xsl:when test="$ratingClass='ms-rating_1'"><xsl:value-of select="string($Rows/@resource.sps.Ratings_CurrentAverageRating1_0Stars)"/></xsl:when>
          <xsl:when test="$ratingClass='ms-rating_1_5'"><xsl:value-of select="string($Rows/@resource.sps.Ratings_CurrentAverageRating1_5Stars)"/></xsl:when>
          <xsl:when test="$ratingClass='ms-rating_2'"><xsl:value-of select="string($Rows/@resource.sps.Ratings_CurrentAverageRating2_0Stars)"/></xsl:when>
          <xsl:when test="$ratingClass='ms-rating_2_5'"><xsl:value-of select="string($Rows/@resource.sps.Ratings_CurrentAverageRating2_5Stars)"/></xsl:when>
          <xsl:when test="$ratingClass='ms-rating_3'"><xsl:value-of select="string($Rows/@resource.sps.Ratings_CurrentAverageRating3_0Stars)"/></xsl:when>
          <xsl:when test="$ratingClass='ms-rating_3_5'"><xsl:value-of select="string($Rows/@resource.sps.Ratings_CurrentAverageRating3_5Stars)"/></xsl:when>
          <xsl:when test="$ratingClass='ms-rating_4'"><xsl:value-of select="string($Rows/@resource.sps.Ratings_CurrentAverageRating4_0Stars)"/></xsl:when>
          <xsl:when test="$ratingClass='ms-rating_4_5'"><xsl:value-of select="string($Rows/@resource.sps.Ratings_CurrentAverageRating4_5Stars)"/></xsl:when>
          <xsl:when test="$ratingClass='ms-rating_5'"><xsl:value-of select="string($Rows/@resource.sps.Ratings_CurrentAverageRating5_0Stars)"/></xsl:when>
        </xsl:choose>
        <xsl:if test="$ratingsData/FeatureActivated/node()='true' and $ratingsData/IsUserAnonymous/node()='false'">
          <xsl:text> </xsl:text>
          <xsl:value-of select="string($Rows/@resource.sps.Ratings_SubmitNewRatingOpenerMessage)"/>
        </xsl:if>
      </xsl:variable>
      <span id="RatingsCtrl_{$controlId}">
        <xsl:if test="$GroupingRender">
          <xsl:attribute name="url"><xsl:value-of select="$url"/></xsl:attribute>
          <xsl:attribute name="title"><xsl:value-of select="$title"/></xsl:attribute>
          <xsl:attribute name="onmouseover">ratingsManager.CreateControlForGrpBy(new Sys.UI.DomEvent(event));</xsl:attribute>
          <xsl:attribute name="onfocusin">ratingsManager.CreateControlForGrpBy(new Sys.UI.DomEvent(event));</xsl:attribute>
        </xsl:if>
        <a class="ms-currentRating" href="javascript:;">
          <img class="{$ratingClass}" src="{$ratingsData/RatingImageStripUrl/node()}" alt="{$altMessage}"/>
        </a>
        <xsl:if test="$ratingsData/FeatureActivated/node()='true' and $ratingsData/IsUserAnonymous/node()='false'">
          <span class="ms-submitRating">
            <img tabindex='0' alt='{string($Rows/@resource.sps.Ratings_Rate1StarMessage)} {string($Rows/@resource.sps.Ratings_SubmitInstruction)} {string($Rows/@resource.sps.Ratings_IncreaseRatingInstruction)} {string($Rows/@resource.sps.Ratings_EscapeInstruction)}' class='ms-rate1star'/>
            <img tabindex='0' alt='{string($Rows/@resource.sps.Ratings_Rate2StarMessage)} {string($Rows/@resource.sps.Ratings_SubmitInstruction)} {string($Rows/@resource.sps.Ratings_IncreaseRatingInstruction)} {string($Rows/@resource.sps.Ratings_DecreaseRatingInstruction)} {string($Rows/@resource.sps.Ratings_EscapeInstruction)}' class='ms-rate2stars'/>
            <img tabindex='0' alt='{string($Rows/@resource.sps.Ratings_Rate3StarMessage)} {string($Rows/@resource.sps.Ratings_SubmitInstruction)} {string($Rows/@resource.sps.Ratings_IncreaseRatingInstruction)} {string($Rows/@resource.sps.Ratings_DecreaseRatingInstruction)} {string($Rows/@resource.sps.Ratings_EscapeInstruction)}' class='ms-rate3stars'/>
            <img tabindex='0' alt='{string($Rows/@resource.sps.Ratings_Rate4StarMessage)} {string($Rows/@resource.sps.Ratings_SubmitInstruction)} {string($Rows/@resource.sps.Ratings_IncreaseRatingInstruction)} {string($Rows/@resource.sps.Ratings_DecreaseRatingInstruction)} {string($Rows/@resource.sps.Ratings_EscapeInstruction)}' class='ms-rate4stars'/>
            <img tabindex='0' alt='{string($Rows/@resource.sps.Ratings_Rate5StarMessage)} {string($Rows/@resource.sps.Ratings_SubmitInstruction)} {string($Rows/@resource.sps.Ratings_DecreaseRatingInstruction)} {string($Rows/@resource.sps.Ratings_EscapeInstruction)}' class='ms-rate5stars'/>
          </span>
        </xsl:if>
      </span>
      <xsl:if test="$ratingsData/FeatureActivated/node()='true' and $ratingsData/IsUserAnonymous/node()='false' and not($GroupingRender)">
        <script type="text/javascript">
        function RatingsControlLoader_<xsl:value-of select="$controlId"/>()
        {
          ratingsManager.CreateControl('<xsl:value-of select="ddwrt:EcmaScriptEncode($url)"/>', '<xsl:value-of select="ddwrt:EcmaScriptEncode($title)"/>', 'RatingsCtrl_<xsl:value-of select="$controlId"/>');
        }

        ExecuteOrDelayUntilEventNotified(RatingsControlLoader_<xsl:value-of select="$controlId"/>, 'RatingsManagerLoaded');
        </script>
      </xsl:if>
    </xsl:if>
  </xsl:template>

  <xsl:template name="emit_RatingsInitialization">
    <xsl:variable name="ratingsData" select="ddwrt:GetRatingsData(string(-1))"/>
    <xsl:if test="$ratingsData/FeatureActivated/node()='true' and $ratingsData/IsUserAnonymous/node()='false'">
      <script src="/_layouts/Ratings.js?rev={$ratingsData/CurrentBuildVersion/node()}" type="text/javascript" defer="defer"></script>
      <script type="text/javascript">
        var ratingsManager = null;
        function RatingsManagerLoader()
        {
          var ratingsData = new RatingsCommonData(
            '<![CDATA[<img src="/_layouts/images/loading16.gif"/> ]]><xsl:value-of select="string($Rows/@resource.sps.Ratings_PopupWaitMessage)"/>',
            '<xsl:value-of select="string($Rows/@resource.sps.Ratings_PopupAverageRatingLabel)"/>',
            '<xsl:value-of select="string($Rows/@resource.sps.Ratings_PopupRatingCountLabel)"/>',
            '<xsl:value-of select="string($Rows/@resource.sps.Ratings_PopupUserRatingLabel)"/>',
            '<xsl:value-of select="string($Rows/@resource.sps.Ratings_PopupSubmitMessage)"/>',
            '<xsl:value-of select="string($Rows/@resource.sps.Ratings_PopupDataNotAvailableMessage)"/>',
            '<xsl:value-of select="string($Rows/@resource.sps.Ratings_PopupDataNotRatedMessage)"/>',
            '<xsl:value-of select="string($Rows/@resource.sps.Ratings_ClickToRateMessage)"/>',
            '<xsl:value-of select="$ratingsData/SessionID/node()"/>',
            '<xsl:value-of select="$ratingsData/WebID/node()"/>',
            '<xsl:value-of select="ddwrt:EcmaScriptEncode($HttpVDirUniEncoded)"/>',
            '<xsl:value-of select="$ratingsData/SiteID/node()"/>',
            '<xsl:value-of select="$ratingsData/NewRatingIconUrl_EcmaScriptEncoded/node()"/>',
            '<xsl:value-of select="$ratingsData/EmptyRatingIconUrl_EcmaScriptEncoded/node()"/>'
        );

          ratingsManager = new RatingsManager(ratingsData);

          NotifyEventAndExecuteWaitingJobs('RatingsManagerLoaded');
        }

        ExecuteOrDelayUntilScriptLoaded(RatingsManagerLoader, 'ratings.js');
      </script>
    </xsl:if>
  </xsl:template>

  <xsl:template ddwrt:dvt_mode="header" match="FieldRef[@ID='5a14d1ab-1513-48c7-97b3-657a5ba6c742']" mode="header">
    <th class="ms-vh2" nowrap="nowrap" scope="col" onmouseover="OnChildColumn(this)">
      <xsl:call-template name="dvt_headerfield">
        <xsl:with-param name="fieldname">
          <xsl:value-of select="@Name"/>
        </xsl:with-param>
        <xsl:with-param name="fieldtitle">
          <xsl:value-of select="@DisplayName"/>
        </xsl:with-param>
        <xsl:with-param name="displayname">
          <xsl:value-of select="@DisplayName"/>
        </xsl:with-param>
        <xsl:with-param name="fieldtype">x:string</xsl:with-param>
      </xsl:call-template>
      <xsl:call-template name="emit_RatingsInitialization"/>
    </th>
  </xsl:template>

</xsl:stylesheet>

