﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
	<xsl:include href="../../../Version_2_0/PopUpHeader.xslt"/>
	<xsl:output method="xml" indent="yes"/>

	<xsl:template match="/">
		<xsl:variable name="TournamentItemID" select="AjaxRequest/Params/ItemID"></xsl:variable>
		<xsl:variable name="Origin" select="AjaxRequest/Params/Origin"></xsl:variable>
		<xsl:variable name="TemplateURL" select="normalize-space(AjaxRequest/TemplateURL)"></xsl:variable>
		<xsl:variable name="HostName" select="normalize-space(AjaxRequest/HostName)"></xsl:variable>
		<xsl:variable name="WinnerTeam" select="AjaxRequest/Tournament/Winner/WinnerTeam"></xsl:variable>
		<xsl:variable name="PageMode" select="AjaxRequest/Params/PageParams/PageMode"></xsl:variable>
		<xsl:variable name="TournamentName" select="normalize-space(AjaxRequest/TournamentDetail/Tournament/Name)"></xsl:variable>
		<xsl:variable name="BrandName" select="normalize-space(AjaxRequest/TournamentDetail/Tournament/BrandName)"></xsl:variable>

		<xsl:if test="$Origin = 'membertournaments'">
			<xsl:call-template name="PopUpHeader">
				<xsl:with-param name="ItemID" select="$TournamentItemID"></xsl:with-param>
				<xsl:with-param name="TemplateURL" select="$TemplateURL"></xsl:with-param>
				<xsl:with-param name="HeaderText">
					<xsl:text>Tournament Brackets</xsl:text>
				</xsl:with-param>
			</xsl:call-template>
		</xsl:if>

		<xsl:variable name="tableStyle">
			<xsl:choose>
				<xsl:when test="$Origin = 'membertournaments'">
					<xsl:text></xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text>height:670px;</xsl:text>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<xsl:if test="(not($Origin) or $Origin != 'membertournaments')">
			<div class="padding5 labelGoalTopicsText">
				<div style="float:left;">
					<xsl:value-of select="$TournamentName"/>
				</div>
				<div style="float:right;">
					<xsl:value-of select="$BrandName"/>
				</div>
			</div>
		</xsl:if>
		<div id="divTournamentBracketsContainer" class="marginTop25">
			<table align="center" style="{$tableStyle}" id="tabTournamentBrackets">
				<tr>
					<xsl:for-each select="AjaxRequest/Tournament/Round">
						<xsl:variable name="Value" select="Value"></xsl:variable>

						<td class="verticallyMiddle textCenter">
							<xsl:for-each select="Match [Round = $Value and position() &lt;= ceiling(count(../../Round[Value = $Value]/Match) div 2)]">
								<xsl:if test="count(../../Round[Value = $Value]/Match) = 1">

									<xsl:choose>
										<xsl:when test="$WinnerTeam">
											<div style="margin-top:-100px;position:absolute;width:100px;">
												<div style="line-height:40px;" class="communityTopHeaderTitle">
													<span>Winner</span>
												</div>
											</div>
											<div style="margin-top:-50px;position:absolute;">
												<div class="tournamentBracketBox">
													<xsl:value-of select="$WinnerTeam"/>
												</div>
											</div>
										</xsl:when>
										<xsl:otherwise>
											<xsl:if test="(not($PageMode) or $PageMode != 'View') and (not($Origin) or $Origin != 'membertournaments')">
												<div style="margin-top:-100px;margin-left:-40px;position:absolute;width:180px;">
													<div style="line-height:40px;" class="TournamentButtonGray onMouseOver"
                                                         onclick="startRound('{$TournamentItemID}', 'divTournamentBracketsContainer');">
														<span>Start Round</span>
													</div>
												</div>
											</xsl:if>
										</xsl:otherwise>
									</xsl:choose>
								</xsl:if>
								<xsl:call-template name="TeamBox">
									<xsl:with-param name="TeamName1" select="TeamName1"></xsl:with-param>
									<xsl:with-param name="TeamName2" select="TeamName2"></xsl:with-param>
								</xsl:call-template>

								<xsl:if test="(not($PageMode) or $PageMode != 'View') and (not($Origin) or $Origin != 'membertournaments')">
									<xsl:if test="count(../../Round[Value = $Value]/Match) = 1 and $WinnerTeam">
										<div style="margin-left:-80px;position:absolute;width:260px;">
											<div style="line-height:40px;" class="TournamentButtonGray onMouseOver" onclick="Tournament_Complete('{$TournamentItemID}');">
												<span>Publish to Party Pong</span>
											</div>
										</div>
									</xsl:if>
								</xsl:if>
							</xsl:for-each>
						</td>
					</xsl:for-each>

					<xsl:for-each select="AjaxRequest/Tournament/Round">
						<xsl:sort select="Value" order="descending"/>
						<xsl:variable name="Value1" select="Value"></xsl:variable>

						<td class="verticallyMiddle textCenter">
							<xsl:for-each select="Match [Round = $Value1 and position() &gt; ceiling(count(../../Round[Value = $Value1]/Match) div 2)]">
								<xsl:call-template name="TeamBox">
									<xsl:with-param name="TeamName1" select="TeamName1"></xsl:with-param>
									<xsl:with-param name="TeamName2" select="TeamName2"></xsl:with-param>
								</xsl:call-template>
							</xsl:for-each>
						</td>
					</xsl:for-each>
				</tr>
			</table>
		</div>
	</xsl:template>

	<xsl:template name="TeamBox">
		<xsl:param name="TeamName1" select="'N/A'"></xsl:param>
		<xsl:param name="TeamName2" select="'N/A'"></xsl:param>

		<span>
			<div class="marginBottom5 tournamentBracketBox">
				<xsl:choose>
					<xsl:when test="$TeamName1">
						<xsl:value-of select="$TeamName1"/>
					</xsl:when>
					<xsl:otherwise>
						&#xa0;
					</xsl:otherwise>
				</xsl:choose>
			</div>
			<div class="marginBottom20 tournamentBracketBox">
				<xsl:choose>
					<xsl:when test="$TeamName2">
						<xsl:value-of select="$TeamName2"/>
					</xsl:when>
					<xsl:otherwise>
						&#xa0;
					</xsl:otherwise>
				</xsl:choose>
			</div>
		</span>
	</xsl:template>

</xsl:stylesheet>
