<?xml version="1.0"?>
<?xml-source-file e:\data\fop\boxFO.xml?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:math="http://exslt.org/math" exclude-result-prefixes="math xalan"
	xmlns:exsl="http://exslt.org/common" xmlns:xalan="http://xml.apache.org/xalan"  extension-element-prefixes="exsl">
	<xsl:output indent="yes" media-type="text/xml" method="xml"/>
	<xsl:template match="Box-Studio-Movie">
	     <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
		     <fo:layout-master-set>
				<fo:simple-page-master master-name="only" page-height="11in" page-width="8in" margin-top="0.2in" margin-bottom="0.2in" margin-left=".5in" margin-right="0in">
					<fo:region-body margin-top="80pt" margin-bottom="10pt" region-name="xsl-region-body"/>
					<fo:region-before extent="130pt" overflow="visible" region-name="header"/>
					<fo:region-after display-align="after" extent="20.7pt" overflow="visible" region-name="footer"/>
				</fo:simple-page-master>
			</fo:layout-master-set>
			<fo:page-sequence master-reference="only" initial-page-number="1">
				<fo:static-content flow-name="header">
					<fo:block margin-right=".25in" text-align="right" font-size="36pt">
						<fo:inline  color="rgb(0,0,128)">Box Office Analysis</fo:inline>
					</fo:block>
					<fo:block margin-right=".25in" text-align="right" font-size="36pt">
						<fo:inline color="red">FO Edition</fo:inline>
					</fo:block>
				</fo:static-content>
				<fo:static-content flow-name="footer">
					<fo:block text-align="right" font-size="12pt" font-weight="bold">
						Page <fo:page-number/>
					</fo:block>
				</fo:static-content>
				<fo:flow flow-name="xsl-region-body">
					<xsl:apply-templates select="Studio-Movie/Studio">
					    <xsl:sort data-type="number" order="descending" select="sum(Category/Movie/receipts)"/>
					</xsl:apply-templates>
				</fo:flow>
			</fo:page-sequence>
		</fo:root>
	</xsl:template>

	<xsl:template match="Studio">
	<fo:block keep-together.within-page="always"> 	
	<xsl:variable name="catTotals">
	   <xsl:for-each select="Category">
	   <Cate xmlns="">
	      <Sums>
		     <xsl:value-of select="sum(Movie/receipts)"/>
		  </Sums>
		  <CatName>
		     <xsl:value-of select="name"/>
		  </CatName>
	   </Cate>
	   </xsl:for-each>
	</xsl:variable>
	
	<xsl:variable name="maxValue">
	    <xsl:call-template name="math:max">
		<xsl:with-param name="nodes" select="xalan:nodeset($catTotals)/Cate/Sums"/>
		</xsl:call-template>
	</xsl:variable>
	
	<xsl:variable name="maxCat" select="xalan:nodeset($catTotals)/Cate[Sums = $maxValue]/CatName"/>
	
	<xsl:variable name="totalCatReceipts">
	   <xsl:value-of select="sum(Category/Movie/receipts)"/>
	</xsl:variable>
	
	    <fo:table space-before="10pt" space-after="10pt">
		    <fo:table-column column-width=".5in"/>
			<fo:table-column column-width="2.25in"/>
			<fo:table-column column-width="3in"/>
			<fo:table-column column-width="1.5in"/>
			<fo:table-body>
			    <fo:table-row color="yellow" background-color="rgb(0,0,128)">
				   <fo:table-cell>
				       <fo:block font-size="14pt">#<xsl:value-of select="position()"/></fo:block>
				   </fo:table-cell>
				   <fo:table-cell>
				       <fo:block font-size="14pt"><xsl:value-of select="studio-name"/></fo:block>
				   </fo:table-cell>
				   <fo:table-cell>
				       <fo:block font-size="12pt">Total Box Office Receipts Analyzed</fo:block>
				   </fo:table-cell>
				   <fo:table-cell>
				       <fo:block font-size="12pt" text-align="right">$<xsl:value-of select="format-number(sum(Category/Movie/receipts),'#,###')"/></fo:block>
				   </fo:table-cell>
				</fo:table-row>
			</fo:table-body>
		</fo:table>
		<fo:table>
			<fo:table-column column-width="2.75in"/>
			<fo:table-column column-width="4.5in"/>
			<fo:table-body>
			    <fo:table-row>
				    <fo:table-cell>
					<xsl:variable name="dataset">
					   <xsl:for-each select="Category">
						     <xsl:sort data-type="number" order="descending" select="sum(Movie/receipts)"/>
							 <xsl:value-of select="format-number(sum(Movie/receipts) div $totalCatReceipts * 100,'##')"/>,</xsl:for-each></xsl:variable>
					<xsl:variable name="series">
					    <xsl:for-each select="Category">
						     <xsl:sort data-type="number" order="descending" select="sum(Movie/receipts)"/>
							 <xsl:value-of select="format-number(sum(Movie/receipts) div $totalCatReceipts * 100,'##')"/>%,</xsl:for-each></xsl:variable>
					<fo:chart font-size="6pt" chart-title="Sum of movie.GROSS" chart-title-position="BOTTOM" chart-type="pie3d" dataset="{$dataset}" category-names="{$series}" width="1.7in" height="1.4in"/>
					   
					</fo:table-cell>
					<fo:table-cell>
						<fo:table>
							<fo:table-column column-width="3.75in"/>
							<fo:table-column column-width=".75in"/>
							<fo:table-body font-weight="bold" font-size="10pt">
								<fo:table-row>
									<fo:table-cell number-columns-spanned="2" color="white" background-color="rgb(0,0,128)">
									    <fo:block line-height="8pt" space-before="2pt">Summary Analysis</fo:block>
									</fo:table-cell>
								</fo:table-row>
								<fo:table-row font-size="9pt" >
								    <fo:table-cell border-bottom="double rgb(0,0,128) thick">
									    <fo:block space-before="15pt" color="rgb(0,0,128)">Highest Revenue Category</fo:block>
									</fo:table-cell>
								    <fo:table-cell border-bottom="double rgb(0,0,128) thick">
									    <fo:block space-before="15pt" color="rgb(0,0,128)" text-align="right"><xsl:value-of select="$maxCat"/></fo:block>
									</fo:table-cell>
								</fo:table-row>
								<fo:table-row font-size="9pt">
								    <fo:table-cell>
									    <fo:block space-before="2pt"><xsl:value-of select="$maxCat"/> receipts as % of Total <xsl:value-of select="studio-name"/> Receipts:</fo:block>
									</fo:table-cell>
								    <fo:table-cell>
									    <fo:block space-before="2pt" text-align="right"><xsl:value-of select="format-number((xalan:nodeset($catTotals)/Cate[CatName = $maxCat]/Sums div $totalCatReceipts) * 100,'##')"/>%</fo:block>
									</fo:table-cell>
								</fo:table-row>
							</fo:table-body>
						</fo:table>
						<xsl:variable name="maxMovieRec">
						    <xsl:call-template name="math:max">
							     <xsl:with-param name="nodes" select="Category[name=$maxCat]/Movie/receipts"/>
						    </xsl:call-template>
						</xsl:variable>
						<fo:table>
							<fo:table-column column-width="2.5in"/>
							<fo:table-column column-width="2in"/>
							<fo:table-body>
								<fo:table-row font-size="9pt">
								    <fo:table-cell border-bottom="double rgb(0,0,128) thick">
									    <fo:block space-before="5pt" color="rgb(0,0,128)">Highest Revenue Film in <xsl:value-of select="$maxCat"/></fo:block>
									</fo:table-cell>
								    <fo:table-cell border-bottom="double rgb(0,0,128) thick">
									    <fo:block space-before="5pt" color="rgb(0,0,128)" text-align="right"><xsl:value-of select="Category[name=$maxCat]/Movie[receipts=$maxMovieRec]/name"/></fo:block>
									</fo:table-cell>
								</fo:table-row>
								<fo:table-row font-size="9pt">
								    <fo:table-cell>
									    <fo:block space-before="2pt">Box Office Receipts of <xsl:value-of select="Category[name=$maxCat]/Movie[receipts=$maxMovieRec]/name"/></fo:block>
									</fo:table-cell>
								    <fo:table-cell>
									    <fo:block space-before="2pt" text-align="right">$<xsl:value-of select="Category[name=$maxCat]/Movie[receipts=$maxMovieRec]/receipts"/></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 space-before="10pt">
		    <fo:table-column column-width="7.25in"/>
			<fo:table-body>
			     <fo:table-row>
				     <fo:table-cell background-color="rgb(0,0,128)" color="white">
					     <fo:block font-weight="bold" font-size="10pt" line-height="8pt" space-before="2pt" >Detailed Analysis of <xsl:value-of select="studio-name"/> Box Office Receipts:</fo:block>
					</fo:table-cell>
				</fo:table-row>
			</fo:table-body>
		</fo:table>
		<fo:table>
		    <fo:table-column column-width=".5in"/>
		    <fo:table-column column-width="1.25in"/>
		    <fo:table-column column-width="1in"/>
		    <fo:table-column column-width=".8in"/>
		    <fo:table-column column-width=".5in"/>
		    <fo:table-column column-width="1.75in"/>
		    <fo:table-column column-width="1.35in"/>
			<fo:table-body>
			     <fo:table-row>
				     <fo:table-cell border-bottom="solid rgb(0,128,128) thin" number-columns-spanned="3">
					     <fo:block space-before="15pt" color="rgb(0,128,128)" font-size="10pt" font-weight="bold">Box Office Receipts by Category</fo:block>
					</fo:table-cell>
				</fo:table-row>
				<xsl:apply-templates select="Category">
				    <xsl:with-param name="total" select="$totalCatReceipts"/>
				    <xsl:sort data-type="number" order="descending" select="sum(Movie/receipts)"/>
				</xsl:apply-templates>
			</fo:table-body>
		</fo:table>
	</fo:block> 
	</xsl:template>

	<xsl:template match="Category">
	    <xsl:param name="total"/>
	    <fo:table-row font-size="9pt" font-weight="bold">
		    <fo:table-cell color="maroon">
			    <fo:block space-before="2pt" space-after="2pt" ><xsl:value-of select="format-number((sum(Movie/receipts) div $total) * 100, '##')"/>%</fo:block>
			</fo:table-cell>
		    <fo:table-cell>
			    <fo:block space-before="2pt" space-after="2pt" ><xsl:value-of select="name"/></fo:block>
			</fo:table-cell>
		    <fo:table-cell>
			    <fo:block space-before="2pt" space-after="2pt" >$<xsl:value-of select="format-number(sum(Movie/receipts),'#,###')"/></fo:block>
			</fo:table-cell>
			<xsl:if test="position()=1">
				<fo:table-cell number-columns-spanned="4" border-bottom="solid rgb(0,128,128) thin">
					<fo:block color="rgb(0,128,128)" font-size="10pt" font-weight="bold">Box Office Receipts by Film:</fo:block>
				</fo:table-cell>
			</xsl:if>
		</fo:table-row>
		<xsl:apply-templates select="Movie">
            <xsl:with-param name="total" select="$total"/>		
		    <xsl:sort data-type="number" order="descending" select="receipts"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<xsl:template match="Movie">
	   <xsl:param name="total"/>
	   <fo:table-row font-size="9pt" font-weight="bold">
	       <fo:table-cell/>
		   <fo:table-cell/>
		   <fo:table-cell/>
		   <fo:table-cell>
		       <fo:block space-before="1pt" space-after="1pt" text-align="left">$<xsl:value-of select="format-number(receipts,'#,###')"/></fo:block>
			</fo:table-cell>
			<fo:table-cell color="maroon">
			   <fo:block space-before="1pt" space-after="1pt" text-align="center"><xsl:value-of select="format-number((receipts div $total)*100,'##')"/>%</fo:block></fo:table-cell>
			<fo:table-cell>
			   <fo:block space-before="1pt" space-after="1pt" text-align="left"><xsl:value-of select="name"/></fo:block></fo:table-cell>
			<fo:table-cell>
			   <fo:block space-before="1pt" space-after="1pt" text-align="right"><xsl:value-of select="star"/></fo:block></fo:table-cell>
		</fo:table-row>
	</xsl:template>
		
<xsl:template name="math:max">
   <xsl:param name="nodes" select="/.." />
   <xsl:choose>
      <xsl:when test="not($nodes)">NaN</xsl:when>
      <xsl:otherwise>
         <xsl:for-each select="$nodes">
            <xsl:sort data-type="number" order="descending" />
            <xsl:if test="position() = 1">
               <xsl:value-of select="number(.)" />
            </xsl:if>
         </xsl:for-each>
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>	
	
	</xsl:stylesheet>
