<!--                                                                  -->
<!--  Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved     -->
<!--  $Id$  -->
<!--                                                                  -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:variable name="regionCount" select="count(/Root/Region)" />
    <xsl:variable name="digitalProduct" select="/Root/Params/digital" />

    <xsl:template name="regionSearchLayout">
        <xsl:call-template name="searchFormInline" />
    </xsl:template>

    <xsl:template name="searchResults">
        <div class="RegionSearch InlineSearch">
            <xsl:choose>
                <xsl:when test="not(Params/Query) and $regionCount = 0">
                    <div class="Table-empty-results">
                        <p>No regions have been created.</p>
                    </div>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:call-template name="resultsList" />
                </xsl:otherwise>
            </xsl:choose>
        </div>
        <xsl:if test="$regionCount &gt; 0 and Params/Query">
            <xsl:call-template name="buttons">
                <xsl:with-param name="type" select="'stick'" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

    <xsl:template name="resultsList">
        <xsl:if test="Params/Query">
            <h4>Search Results</h4>
        </xsl:if>
        <xsl:call-template name="searchNoResult" />
        <xsl:call-template name="searchNav">
            <xsl:with-param name="form">regionSearch</xsl:with-param>
            <xsl:with-param name="area">region</xsl:with-param>
            <xsl:with-param name="withText">true</xsl:with-param>
        </xsl:call-template>

        <xsl:if test="$regionCount != 0">
            <xsl:call-template name="resultsHeader" />
            <div class="Table-wrapper Table-wrapper-srolltop">
                <xsl:call-template name="regionSearchTable" />
            </div>
        </xsl:if>
        <xsl:if test="not(Params/_inline)">
            <xsl:call-template name="searchNav" />
        </xsl:if>
    </xsl:template>

    <xsl:template name="resultsHeader">
        <table class="Table" id="resultsHeader">
            <thead>
                <tr class="Table-row Table-row-header">
                    <th class="Table-col Table-col-select Table-col-select-wide">Select</th>
                    <th class="Table-col InlineSearch-region-col">Name</th>
                    <th class="Table-col InlineSearch-country-col">Countries</th>
                </tr>
            </thead>
        </table>
    </xsl:template>

    <xsl:template name="regionSearchTable">
        <table class="Table">
            <tbody>
                <xsl:for-each select="Region">
                    <xsl:call-template name="regionSearchTableRow" />
                </xsl:for-each>
            </tbody>
        </table>
        <script type="text/javascript">window.matchCollection = matchCollection;</script>
    </xsl:template>

    <xsl:template name="regionSearchTableRow">
        <xsl:variable name="notUSorCA">
            <xsl:for-each select="CountryList/Country">
                <xsl:if test="CountryCode != 'US' and CountryCode != 'CA'">1</xsl:if>
            </xsl:for-each>
        </xsl:variable>
        <xsl:variable name="disableRegion">
            <xsl:if test="$digitalProduct = 1 and $notUSorCA != ''">1</xsl:if>
        </xsl:variable>
        <tr class="Table-row Table-row-data">
            <xsl:attribute name="class">
                <xsl:text>Table-row Table-row-data</xsl:text>
                <xsl:if test="$disableRegion = 1">
                    <xsl:text> Table-row-disabled</xsl:text>
                </xsl:if>
            </xsl:attribute>
            <td class="Table-col Table-col-select Table-col-select-wide">

                <xsl:call-template name="uiRadio">
                    <xsl:with-param name="disabled">
                        <xsl:if test="$disableRegion = 1">
                            <xsl:text>true</xsl:text>
                        </xsl:if>
                    </xsl:with-param>
                    <xsl:with-param name="name" select="'regionMatch'"/>
                    <xsl:with-param name="onClick">
                        modifyField('selectMatch','enable');
                    </xsl:with-param>
                    <xsl:with-param name="value" select="position()"/>
                </xsl:call-template>

            </td>
            <script type="text/javascript">
                match<xsl:value-of select="position()" /> = new Object();
                match<xsl:value-of select="position()" />['RegionID'] = '<xsl:value-of select="RegionID" />';
                match<xsl:value-of select="position()" />['Name'] = '<xsl:call-template name="escapeJavascript"><xsl:with-param name="string" select="Name" /></xsl:call-template>';
                matchCollection['position<xsl:value-of select="position()" />'] = match<xsl:value-of select="position()" />;
            </script>
            <td class="Table-col InlineSearch-region-col">
                <xsl:value-of select="Name" />
            </td>
            <td class="Table-col InlineSearch-country-col">
                <xsl:variable name="countryCount" select="count(CountryList/Country)" />
                <xsl:choose>
                    <xsl:when test="$countryCount &gt; 8">
                        <abbr class="js-tooltip" id="region{RegionID}">
                            <xsl:attribute name="title">
                                <xsl:for-each select="CountryList/Country">
                                    <xsl:value-of select="CountryCode" />
                                    <xsl:if test="position() != last()">, </xsl:if>
                                    <xsl:if test="(position() mod 8) = 0">&lt;br&gt;</xsl:if>
                                </xsl:for-each>
                            </xsl:attribute>
                            <xsl:for-each select="CountryList/Country[position() &lt; 9]">
                                <xsl:value-of select="CountryCode" />
                                <xsl:if test="position() != last()">, </xsl:if>
                            </xsl:for-each>
                            ...
                        </abbr>
                        <script type="text/javascript">
                            addSweetTitle('region<xsl:value-of select="RegionID" />');
                        </script>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:for-each select="CountryList/Country">
                            <xsl:value-of select="CountryCode" />
                            <xsl:if test="position() != last()">, </xsl:if>
                        </xsl:for-each>
                    </xsl:otherwise>
                </xsl:choose>
            </td>
        </tr>
    </xsl:template>

</xsl:stylesheet>
