<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"
	xmlns:ccal="http://ccal.chinesebay.com/ccal/">
	<xsl:template match="/">
		<xsl:variable name="yearvalue" select="ccal:year/@value" />
		<xsl:variable name="paperspecified" select="ccal:year/@papertype" />
		<xsl:variable name="borderwidth">0.1mm</xsl:variable>
		<xsl:variable name="headercolor">pink</xsl:variable>
		<xsl:variable name="papertype">
			<xsl:choose>
				<xsl:when test="contains($paperspecified,'A4') or contains($paperspecified,'a4')">A4Month</xsl:when>
				<xsl:otherwise>LetterMonth</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="calheight">
			<xsl:choose>
				<xsl:when test="contains($papertype,'A4Month')">18.5cm</xsl:when>
				<xsl:otherwise>7.5in</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
			<fo:layout-master-set>
				<fo:simple-page-master master-name="A4Month" margin-right="1.25cm" margin-left="1.25cm" margin-bottom="1.25cm"
					margin-top="1.25cm" page-height="21cm" page-width="29.7cm">
					<fo:region-body margin-top="0cm" />
					<fo:region-before extent="0cm" />
					<fo:region-after extent="0cm" />
				</fo:simple-page-master>
				<fo:simple-page-master master-name="LetterMonth" margin-right="0.5in" margin-left="0.5in" margin-bottom="0.5in"
					margin-top="0.5in" page-height="8.5in" page-width="11in">
					<fo:region-body margin-top="0in" />
					<fo:region-before extent="0in" />
					<fo:region-after extent="0in" />
				</fo:simple-page-master>
			</fo:layout-master-set>
			<fo:page-sequence master-reference="{$papertype}">
				<fo:flow flow-name="xsl-region-body">
					<xsl:for-each select="ccal:year/ccal:month">
						<fo:table width="100%" table-layout="fixed">
							<fo:table-column column-width="100%" />
							<fo:table-body>
								<fo:table-row>
									<fo:table-cell height="{$calheight}" display-align="after">
										<fo:block line-height="24pt" text-align="center">
											<fo:inline font-size="20pt" font-family="sans-serif">
												<xsl:value-of select="@name" />&#160;<xsl:value-of select="$yearvalue" />&#160;&#160;
											</fo:inline>
											<fo:inline font-size="20pt" font-family="SimSun">
												<xsl:value-of select="@cname" />
											</fo:inline>
										</fo:block>
										<fo:table width="100%" table-layout="fixed">
											<fo:table-column column-width="proportional-column-width(7)" />
											<fo:table-column column-width="proportional-column-width(7)" />
											<fo:table-column column-width="proportional-column-width(7)" />
											<fo:table-column column-width="proportional-column-width(7)" />
											<fo:table-column column-width="proportional-column-width(7)" />
											<fo:table-column column-width="proportional-column-width(7)" />
											<fo:table-column column-width="proportional-column-width(7)" />
											<fo:table-body>
												<fo:table-row>
													<fo:table-cell border-width="{$borderwidth}" border-style="solid" background-color="{$headercolor}"
														height="{$calheight} div 20" display-align="center">
														<fo:block text-align="center" color="red">
															<fo:inline font-size="12pt" font-family="sans-serif">Sun&#160;&#160;&#160;&#160;&#160;&#160;</fo:inline>
															<fo:inline font-size="12pt" font-family="SimSun">日</fo:inline>
														</fo:block>
													</fo:table-cell>
													<fo:table-cell border-width="{$borderwidth}" border-style="solid" background-color="{$headercolor}"
														height="{$calheight} div 20" display-align="center">
														<fo:block text-align="center">
															<fo:inline font-size="12pt" font-family="sans-serif">Mon&#160;&#160;&#160;&#160;&#160;&#160;</fo:inline>
															<fo:inline font-size="12pt" font-family="SimSun">一</fo:inline>
														</fo:block>
													</fo:table-cell>
													<fo:table-cell border-width="{$borderwidth}" border-style="solid" background-color="{$headercolor}"
														height="{$calheight} div 20" display-align="center">
														<fo:block text-align="center">
															<fo:inline font-size="12pt" font-family="sans-serif">Tue&#160;&#160;&#160;&#160;&#160;&#160;</fo:inline>
															<fo:inline font-size="12pt" font-family="SimSun">二</fo:inline>
														</fo:block>
													</fo:table-cell>
													<fo:table-cell border-width="{$borderwidth}" border-style="solid" background-color="{$headercolor}"
														height="{$calheight} div 20" display-align="center">
														<fo:block text-align="center">
															<fo:inline font-size="12pt" font-family="sans-serif">Wed&#160;&#160;&#160;&#160;&#160;&#160;</fo:inline>
															<fo:inline font-size="12pt" font-family="SimSun">三</fo:inline>
														</fo:block>
													</fo:table-cell>
													<fo:table-cell border-width="{$borderwidth}" border-style="solid" background-color="{$headercolor}"
														height="{$calheight} div 20" display-align="center">
														<fo:block text-align="center">
															<fo:inline font-size="12pt" font-family="sans-serif">Thu&#160;&#160;&#160;&#160;&#160;&#160;</fo:inline>
															<fo:inline font-size="12pt" font-family="SimSun">四</fo:inline>
														</fo:block>
													</fo:table-cell>
													<fo:table-cell border-width="{$borderwidth}" border-style="solid" background-color="{$headercolor}"
														height="{$calheight} div 20" display-align="center">
														<fo:block text-align="center">
															<fo:inline font-size="12pt" font-family="sans-serif">Fri&#160;&#160;&#160;&#160;&#160;&#160;</fo:inline>
															<fo:inline font-size="12pt" font-family="SimSun">五</fo:inline>
														</fo:block>
													</fo:table-cell>
													<fo:table-cell border-width="{$borderwidth}" border-style="solid" background-color="{$headercolor}"
														height="{$calheight} div 20" display-align="center">
														<fo:block text-align="center" color="green">
															<fo:inline font-size="12pt" font-family="sans-serif">Sat&#160;&#160;&#160;&#160;&#160;&#160;</fo:inline>
															<fo:inline font-size="12pt" font-family="SimSun">六</fo:inline>
														</fo:block>
													</fo:table-cell>
												</fo:table-row>
												<xsl:variable name="nWeeks" select="count(./ccal:week)" />
												<xsl:for-each select="ccal:week">
													<fo:table-row>
														<xsl:for-each select="ccal:day">
															<fo:table-cell border-width="{$borderwidth}" border-style="solid" height="{$calheight} * 6 div 7 div {$nWeeks}">
																<xsl:choose>
																	<xsl:when test="position()=1 or @isholiday='true'">
																		<fo:block text-align="center" color="red">
																			<xsl:call-template name="daytemp">
																				<xsl:with-param name="calheight" select="$calheight" />
																				<xsl:with-param name="nWeeks" select="$nWeeks" />
																			</xsl:call-template>
																		</fo:block>
																	</xsl:when>
																	<xsl:when test="position()=7">
																		<fo:block text-align="center" color="green">
																			<xsl:call-template name="daytemp">
																				<xsl:with-param name="calheight" select="$calheight" />
																				<xsl:with-param name="nWeeks" select="$nWeeks" />
																			</xsl:call-template>
																		</fo:block>
																	</xsl:when>
																	<xsl:otherwise>
																		<fo:block text-align="center">
																			<xsl:call-template name="daytemp">
																				<xsl:with-param name="calheight" select="$calheight" />
																				<xsl:with-param name="nWeeks" select="$nWeeks" />
																			</xsl:call-template>
																		</fo:block>
																	</xsl:otherwise>
																</xsl:choose>
															</fo:table-cell>
														</xsl:for-each>
													</fo:table-row>
												</xsl:for-each>
											</fo:table-body>
										</fo:table>
									</fo:table-cell>
								</fo:table-row>
							</fo:table-body>
						</fo:table>
					</xsl:for-each>
				</fo:flow>
			</fo:page-sequence>
		</fo:root>
	</xsl:template>
	<xsl:template name="daytemp" match="ccal:day">
		<xsl:param name="calheight">1in</xsl:param>
		<xsl:param name="nWeeks">6</xsl:param>
		<fo:table width="100%" table-layout="fixed">
			<fo:table-column column-width="proportional-column-width(10)" />
			<fo:table-column column-width="80%" />
			<fo:table-column column-width="proportional-column-width(10)" />
			<fo:table-body>
				<fo:table-row>
					<fo:table-cell height="{$calheight} * 6 div 7 div {$nWeeks}">
						<fo:block />
					</fo:table-cell>
					<fo:table-cell display-align="before">
						<fo:table width="100%" table-layout="fixed">
							<fo:table-column column-width="100%" />
							<fo:table-body>
								<fo:table-row>
									<fo:table-cell height="{$calheight} div 21">
										<fo:table width="100%" table-layout="fixed">
											<fo:table-column column-width="35%" />
											<fo:table-column column-width="65%" />
											<fo:table-body>
												<fo:table-row>
													<fo:table-cell height="{$calheight} div 21" display-align="after">
														<fo:block font-size="18pt" font-family="sans-serif" text-align="center"><xsl:value-of select="@value" />&#160;</fo:block>
													</fo:table-cell>
													<fo:table-cell display-align="after">
														<fo:table width="100%" table-layout="fixed">
															<fo:table-column column-width="100%" />
															<fo:table-body>
																<fo:table-row>
																	<fo:table-cell display-align="after">
																		<fo:block text-align="left" line-height="10pt" font-size="8pt" font-family="sans-serif">
																			<xsl:choose>
																				<xsl:when test="@cdate &gt; 1 and @cdate &lt; 10">&#160;&#160;</xsl:when>
																				<xsl:otherwise>&#160;</xsl:otherwise>
																			</xsl:choose>
																			<xsl:choose>
																				<xsl:when test="@value=1 or @cdate=1">
																					<xsl:value-of select="@cmonth" />
																					<xsl:value-of select="@leap" />.
																					<xsl:value-of select="@cdate" />
																				</xsl:when>
																				<xsl:otherwise>
																					<xsl:value-of select="@cdate" />
																				</xsl:otherwise>
																			</xsl:choose>
																		</fo:block>
																	</fo:table-cell>
																</fo:table-row>
																<fo:table-row>
																	<fo:table-cell height="{$calheight} div 42" display-align="before">
																		<fo:block text-align="left" line-height="10pt" font-size="8pt" font-family="SimSun">
																			<xsl:choose>
																				<xsl:when test="@value=1 or @cdate=1">
																					<xsl:value-of select="@cmonthname" />
																					<xsl:value-of select="@cdatename" />
																				</xsl:when>
																				<xsl:otherwise>
																					<xsl:value-of select="@cdatename" />
																				</xsl:otherwise>
																			</xsl:choose>
																		</fo:block>
																	</fo:table-cell>
																</fo:table-row>
															</fo:table-body>
														</fo:table>
													</fo:table-cell>
												</fo:table-row>
											</fo:table-body>
										</fo:table>
									</fo:table-cell>
								</fo:table-row>
								<fo:table-row>
									<fo:table-cell height="{$calheight} div ({$nWeeks} * 2)" display-align="after">
										<fo:block font-size="8pt" font-family="SimSun" text-align="left">
											<xsl:value-of select="@note" />
										</fo:block>
									</fo:table-cell>
								</fo:table-row>
							</fo:table-body>
						</fo:table>
					</fo:table-cell>
					<fo:table-cell>
						<fo:block />
					</fo:table-cell>
				</fo:table-row>
			</fo:table-body>
		</fo:table>
	</xsl:template>
</xsl:stylesheet>
