<?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">
	<xsl:output method="xml" indent="yes" encoding="UTF-8" />
	
	<xsl:template match="document">
		<fo:root>
			<fo:layout-master-set>
				<!-- This sets up the page dimensions, margins, and layout -->
				<fo:simple-page-master master-name="A4-portrait" page-height="29.7cm" page-width="21.0cm" margin="2cm">
					<fo:region-body margin-top="1.6cm" margin-bottom="1.6cm"/>
					<fo:region-before extent="1.5cm"/>
					<fo:region-after extent="1.5cm"/>
				</fo:simple-page-master>
			</fo:layout-master-set>
			<fo:page-sequence master-reference="A4-portrait">
				<fo:static-content flow-name="xsl-region-before">
					<!-- Here we are creating a header for the page with a grey background -->
					<fo:block background-color="grey" text-align="center" font-size="24pt" font-weight="bold">4-H Impact</fo:block>
				</fo:static-content>
				<fo:static-content flow-name="xsl-region-after">
					<fo:block border-top-style="solid" border-top-width="thin" padding-top="5pt" font-size="10pt" text-align="center"><fo:inline font-weight="bold">University of Arkansas Cooperative Extension Service </fo:inline> • 2301 South University Ave • Little Rock, AR 72204</fo:block>
					<fo:block font-size="10pt" text-align="center">Tel: (501) 671-2000 </fo:block>
				</fo:static-content>
				<fo:flow flow-name="xsl-region-body">
					<fo:block>
						<xsl:apply-templates select="title"/>title
						<xsl:apply-templates select="content"/>end of content
						<xsl:apply-templates select="content/contact"/>contact
						<xsl:apply-templates select="content/maincontent/image"/>image
						<xsl:apply-templates select="becky"/>shouldn't find this one.
						
					</fo:block>
				</fo:flow>
			</fo:page-sequence>
		</fo:root>
	</xsl:template>
	
	<!-- This takes the title from the PCF and styles the text with 18pt and spacing afterwards -->
	<xsl:template match="title">
		<fo:block font-size="18pt" space-after="0.5cm">
			<xsl:apply-templates/>title
		</fo:block>
	</xsl:template>
	
	
	
	<!-- This matches the content region of the PCF -->
	<xsl:template match="content">
		<fo:block>
			<xsl:apply-templates/>
		</fo:block>
	</xsl:template>
	
	<!-- When the XSL finds a paragraph tag it will place a 0.5 centimeter space beneith each one -->
	<xsl:template match="p">
		<fo:block space-after="0.5cm">
			<xsl:apply-templates/>
		</fo:block>
	</xsl:template>
	
	<!-- When the XSL finds an image it use the formatting below to style it -->
	<xsl:template match="img">image match!!!!!!!!
		<fo:external-graphic src="url('http://www.uaex.uada.edu/_resources/images/mastergardener_large.jpg')" width="300px" height="75px"/>
		
	</xsl:template>
	
	<xsl:template match="a">
	  
		<xsl:choose>
	    <xsl:when test="@name">
	      <xsl:if test="not(name(following-sibling::*[1]) = 'h1')">
	        <fo:block line-height="0" space-after="0pt" 
	          font-size="0pt" id="{@name}"/>
	      </xsl:if>
	    </xsl:when>
	    <xsl:when test="@href">
	      <fo:basic-link color="blue">
	    
			  <xsl:choose>
				  
	          <xsl:when test="starts-with(@href, '#')">
	            <xsl:attribute name="internal-destination">
	              <xsl:value-of select="substring(@href, 2)"/>
	            </xsl:attribute>
	          </xsl:when>
				  
	          <xsl:otherwise>
	            <xsl:attribute name="external-destination">
	              <xsl:value-of select="@href"/>
	            </xsl:attribute>
	          </xsl:otherwise>
				  
	        </xsl:choose>
		
			  <xsl:apply-templates select="*|text()"/>
	      </fo:basic-link>
	      <xsl:if test="starts-with(@href, '#')">
	        <xsl:text> on page </xsl:text>
	        <fo:page-number-citation ref-id="{substring(@href, 2)}"/>
	      </xsl:if>
	    </xsl:when>
	  </xsl:choose>
	</xsl:template>
	
	
		
	<!-- When the XSL finds an image it use the formatting below to style it -->
	<xsl:template match="img">
		<xsl:param name="path_from_root" />

		<xsl:variable name="src">
			<xsl:choose>
				<xsl:when test="substring-before(@src,'://') = 'http'"><xsl:value-of select="@src" /></xsl:when>
				<xsl:otherwise>
					<xsl:choose>
						<xsl:when test="substring(@src, 1, 1) != '/' ">http://uaex.uada.edu<xsl:value-of select="$path_from_root" />/<xsl:value-of select="@src" /></xsl:when>
						<xsl:otherwise>http://www.uaex.uada.edu<xsl:value-of select="@src" /></xsl:otherwise>
					</xsl:choose>
					
					
					
					
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		
		<xsl:variable name="width">
			<xsl:if test="@width">
				<xsl:choose>
					<xsl:when test="@width > 500">500px</xsl:when>
					<xsl:otherwise><xsl:value-of select="@width" />px</xsl:otherwise>
				</xsl:choose>
			</xsl:if>
		</xsl:variable>

		<xsl:variable name="height">
			<xsl:if test="@height"><xsl:value-of select="@height" />px</xsl:if>
		</xsl:variable>
		
		
		

		
		<fo:external-graphic
			background-color="rgb(0, 255, 0)"
			src="url(https://www.uaex.uada.edu/_resources/images/mastergardener_large.jpg)"				 			
			scaling="uniform"
			scaling-method="integer-pixels"
			content-width="{$width}"
			width="{$width}"
			content-height="{$height}"
			height="{$height}" />
			
	</xsl:template>
	
	<!-- Each line break will have a 0.5 centimeter space beneath it -->
	<xsl:template match="br">
		<fo:block space-after="0.5cm" />
	</xsl:template>
  
</xsl:stylesheet>
