<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ou="http://omniupdate.com/XSL/Variables"
    xmlns:fn="http://omniupdate.com/XSL/Functions" xmlns:ouc="http://omniupdate.com/XSL/Variables"
    exclude-result-prefixes="xs ou fn ouc">
    
    <!--
    
 	TABLE OF CONTENTS 
    
    0. General Matches 
    1. Special Transformations (Accordion/Executive Team/Resource Detail)
    2. Main Site/4h Content (Image Text BLock/Media List/Publications List/Contact)
    3. County Page (Events)
    4. UAEX Home
    5. 4H Home
    6. Divisions Home

    
    -->

	<!-- TABLE TRANSFORMATIONS RECURSIVE TEMPLATE MATCH -->
	<!-- used by various templates in this stylesheet. copies all content, except for those with an exception defined -->
	
	<xsl:template match="element()" mode="cntntlnks ul-to-dl resize169"> 
	   <xsl:element name="{name()}">
	          <xsl:apply-templates select="attribute()|node()" mode="#current"/> <!-- re-use mode -->
	   </xsl:element>
	</xsl:template>
	
	<xsl:template match="attribute()|text()|comment()" mode="cntntlnks ul-to-dl resize169">
		<xsl:copy />
	</xsl:template> 
	



	<!-- 
	
	Section 1
	
	Special
	
	TABLE TRANSFORMATIONS 
	
	* Simple Accordion
	* Complex Accordion
	* Executive Team Listing
	* Resource Detail
	* Resource Detail (text)
	
	-->
	
	<!-- BEGIN ACCORDION -->

	<!-- BEGIN Simple ACCORDION -->
	
	<xsl:template match="table[@class='accordion']" mode="copy">
	
		<ul class="slide-list accordion">
			<xsl:for-each select="tbody/tr">
				<xsl:if test="position() mod 2 = 1">
				<xsl:variable name="nextPosition" select="number(position()) +1"/>	
				<li>
					<a class="opener" href="#">
						<xsl:apply-templates select="td/node()[name()!='a']" mode="copy"/>
					</a>
					<div class="slide">			
						<xsl:apply-templates select="./../tr[$nextPosition]/td[1]/node()" mode="copy"/>
					</div>
				</li>
				</xsl:if>
			</xsl:for-each>
		</ul>
	
	</xsl:template>
	
	<!-- BEGIN Class/inner structure for Simple Accordion -->
	
	
	<xsl:template match="table[@class='accordion']//ul/li" mode="copy">
		<li>
			<span>
				<xsl:apply-templates select="node()" mode="copy"/>
			</span>
		</li>
	</xsl:template>
	

	
	
	<!-- END Class/inner structure templates for Simple Accordion -->
	
	<!-- BEGIN Complex Accordion -->
	
	<xsl:template match="table[@class='accordion-complex'][1]" mode="copy">
			<xsl:call-template name="accordion"/>	<!-- see special-templates.xsl --> 
	</xsl:template>
	
	<xsl:template match="table[@class='accordion-complex'][position()>1]" mode="copy"/>
	
	
	<!-- END Complex Accordion -->
	
	
	<!-- EXECUTIVE TEAM LISTING -->
	
	<xsl:template match="table[@class='executive-team'][1]" mode="copy"> <!-- if one is found -->
	
		<xsl:if test="$ou:action='prv' or $ou:action='edit'">
		<p class="omniupdate message success">The Executive Team Listing is displayed below based on table contents. 
		Table information will be grouped together, in original order.</p>
		</xsl:if>
	
		<div class="team-holder"> <!-- wrapper div -->
		
			<xsl:apply-templates select="/*:document/content//table[@class='executive-team']" mode="executive"/> <!-- copy all -->
		
		</div>
	
	</xsl:template>
	
	<xsl:template match="table[@class='executive-team'][position()>1]" mode="copy"/> <!-- do nothing, the match for [1] will handle it -->
	
	<xsl:template match="table" mode="executive">
	
		<div class="col">
		
		<xsl:for-each select="tbody/tr">	
		<xsl:variable name="thirdRow" select="position()"/>
		
		<xsl:if test="$thirdRow mod 3 = 0">
		
		<xsl:variable name="secondRow" select="$thirdRow - 1"/>
		<xsl:variable name="firstRow" select="$thirdRow - 2"/>
		
	
			<section class="team-block">
			
				<div class="photo">
					<!-- profile image -->
					<xsl:for-each select="./../tr[$firstRow]/td[1]">
						<xsl:apply-templates select="a|img" mode="js-img"/>
					</xsl:for-each>
				</div>
				
				<div class="description">
					<header class="heading">
						<xsl:apply-templates select="./../tr[$firstRow]/td[2]/node()" mode="copy"/>
					</header>
					<address>
						<xsl:apply-templates select="./../tr[$secondRow]/td[2]/node()" mode="copy"/>
					</address>
					<address>
						<xsl:apply-templates select="./td[2]/node()" mode="copy"/>
					</address>
				</div>
				
			</section>
		
		</xsl:if>	
		</xsl:for-each>
		
		</div>
	
	</xsl:template>


	<!-- 1 and 2 Column Resource Detail -->
	
	<xsl:template match="table[@class='rsrc-dtl']" mode="copy">
	<section class="resource-detail">
	<h2><xsl:apply-templates select="caption/text()" mode="copy"/></h2>
		<!-- if the left column has text content then add the padding class -->
		<div class="{if(tbody/tr/td[1]/p/text()) then ('column leftcolpadding') else('column')}">
			<xsl:apply-templates select="tbody/tr/td[1]/node()" mode="copy"/>		
		</div>
		<xsl:if test="tbody/tr/td[2]">
			<div class="column">
				<xsl:apply-templates select="tbody/tr/td[2]/node()" mode="copy"/>	
				
			</div>	
		</xsl:if>
		<div class="cf"><!--clearfix--></div>
	</section>
	</xsl:template>
	
	<xsl:template match="table[@class='rsrc-dtl']//img" mode="copy">
	<div class="photo">
		<xsl:apply-templates select="." mode="js-img"/>
	</div>
	<div class="cf"></div>
	</xsl:template>
	
	<xsl:template match="table[@class='rsrc-dtl']//a" mode="copy">
	<xsl:choose>
		<xsl:when test=".//img">
		<div class="photo">
			<xsl:apply-templates select="." mode="js-img"/>
		</div>
		<div class="cf"></div>
		</xsl:when>
		<xsl:otherwise>
			<a>
				<xsl:apply-templates select="attribute()|node()" mode="copy"/>
			</a>
		</xsl:otherwise>	
	</xsl:choose>
	</xsl:template>



	<!--
	
	Section 2
	
	Main Content Area Transformations for dedicated editable regions
	
	Shared by other templates where appropriate
	
	* Left/Right 20%/40%/50% Image Floats
	* Image and Text Content Image Block 
	* List 1: Publications/Content 
	* List 2: Additional/Media
	* Contact 
	
	-->


	<!-- 
	
	LEFT, RIGHT, 20%,40%,50% WIDTH FLOATED IMAGES WITH CAPTIONS 
	
	-->
	
    <xsl:template match="table[@class='right-image-caption30' or @class='left-image-caption30' or @class='right-image-caption40' or @class='right-image-caption20' or @class='left-image-caption40' or @class='left-image-caption20' or @class='left-image-caption50' or @class='right-image-caption50']" mode="copy">
        <div class="{@class}">
            <xsl:choose>
                <xsl:when test="tbody/tr[1]/td//a/img">
                    <div class="photo">
                        <a href="{tbody/tr[1]/td//a/@href}">
                            <img class="fs-img" src="{tbody/tr[1]/td//a/img/@src}"
                                data-src="{tbody/tr[1]/td//a/img/@src}" data-aspect-ratio="auto"
                                alt="{tbody/tr[1]/td//a/img/@src}"/>
                            <noscript>
                                <img class="fs-img" src="{tbody/tr[1]/td//a/img/@src}" width="320"
                                    height="218" alt="{tbody/tr[1]/td//a/img/@src}"/>
                            </noscript>
                        </a>
                    </div>
                </xsl:when>
                <xsl:when test="tbody/tr[1]/td//img">
                    <div class="photo">

                        <img class="fs-img" src="{tbody/tr[1]/td//img/@src}"
                            data-src="{tbody/tr[1]/td//img/@src}" data-aspect-ratio="auto"
                            alt="{tbody/tr[1]/td//img/@src}"/>
                        <noscript>
                            <img class="fs-img" src="{tbody/tr[1]/td//img/@src}" width="{@width}"
                                height="218" alt="{tbody/tr[1]/td//img/@src}"/>
                        </noscript>

                    </div>
                </xsl:when>
                <xsl:otherwise> Please insert a valid image in this placeholder </xsl:otherwise>
            </xsl:choose>
            <xsl:if test="not(normalize-space(string-join(tbody/tr[2]/td//node()))='')">
                <div class="caption">
<!--                    <xsl:value-of select="tbody/tr[2]/td/node()"/>-->
                   <xsl:apply-templates select="tbody/tr[2]/td/node()" mode="copy"/>
                </div>
            </xsl:if>
            <xsl:apply-templates select="tbody/tr[position()>2]/td/node()" mode="copy"/>
        </div>
    </xsl:template>
    
	<!-- multipurpose table to ul so that complex html can be contained in a li element -->
	<xsl:template match="table[@class='table-to-ul']">
	<ul>
		<xs:for-each select="tbody/tr">
			<li>
				<xsl:apply-templates select="td/node()" mode="copy"/>
			</li>
		</xs:for-each>		
	</ul>
	</xsl:template>
	
	
	<!-- for the containment of html elements such as <section>, <article>, etc -->
	
	<xsl:template match="table[@class='html-holder']" mode="copy">
	<xsl:apply-templates select="tbody/tr/td/node()" mode="copy"/>	
	</xsl:template>
	
	<!-- 
	
	EDITABLE REGION
	MAIN SITE CONTENT TRANSFORMATIONS 
	
	-->
    
    
	<!-- 
	
	Image Blocks
	
	An image on the left side and text content on the right.
	
	-->
	

	<xsl:template match="table[@class='mscp-imgtxtblck']" mode="copy">
		<xsl:choose>
			<xsl:when test="$pageType!='counties_home'">
				<xsl:for-each select="tbody/tr">
					<article class="{
						if ((ancestor::maincontent and not(ancestor::div[@class='column'])) or $pageColumns='3' or $pageType='counties_programs') 
						then('content-article') 
						else ('article')
					}">
					<!-- use wider width class for the 3 column layout, as only one column of image blocks are displayed on 3 col layouts-->
						<div class="photo">
							<xsl:apply-templates select="td[1]/node()" mode="copy"/> <!-- javascript optimzied image size -->			
						</div>
						
						<div class="description"><!-- anything that is not an image -->	
							<xsl:apply-templates select="td[2]/node()" mode="copy"/>
						</div>
						
					</article>
				</xsl:for-each>
			</xsl:when>
			
			<xsl:otherwise>
				<xsl:for-each select="tbody/tr">
					<div class="news-area">
					
						<div class="photo">
								<xsl:apply-templates select="td[1]/node()" mode="copy"/>
						</div>
						
						<div class="holder"><!-- anything that is not an image -->	
							<xsl:apply-templates select="td[2]/node()" mode="copy"/>
						</div>
						
					</div>
				</xsl:for-each>
			</xsl:otherwise>
		</xsl:choose>
	
	</xsl:template>
	
	<!-- 
	
	Lists
	
	-->
	
	<!-- PUBLICATIONS/CONTENT LINKS BEGIN (Page 2) -->

	<xsl:template match="table[@class='mscp-cntntlnks']" mode="copy">
	<xsl:choose>
	
		<!-- landing pages only have one column -->
		<xsl:when test="$pageType='landing'">
		
			<ul class="publications-list">
				<xsl:for-each select="tbody/tr/td">
					<li>
						<xsl:apply-templates select="node()" mode="cntntlnks"/>
					</li>
				</xsl:for-each>
			</ul>
			
		</xsl:when>
	
		<!-- if there are two columns -->
		<xsl:when test="tbody/tr[1]/td[2]">
		
			<div class="row">
				<!-- create a ul for each column -->
				<ul class="publications publications">		
					<!-- copy tds in left column except those that don't have elements -->
					<xsl:for-each select="tbody/tr/td[position() mod 2 != 0]">				
						<li>		
							<xsl:choose>
								<xsl:when test=".//img">
									<div class="holder">	
										<!-- template with special case for images -->	
										<xsl:apply-templates select="node()" mode="cntntlnks"/>			
									</div>									
								</xsl:when>
								<xsl:otherwise>
									<xsl:apply-templates select="node()" mode="cntntlnks"/>			
								</xsl:otherwise>
							</xsl:choose>						
						</li>		
					</xsl:for-each>		
				</ul>		
				<!-- copy tds in right column except those that don't have elements -->
				<ul class="publications publications-alt">	
					<xsl:for-each select="tbody/tr/td[position() mod 2 = 0]">		
						<li>		
							<xsl:choose>
								<xsl:when test=".//img">
									<div class="holder">	
										<!-- template with special case for images -->	
										<xsl:apply-templates select="node()" mode="cntntlnks"/>			
									</div>									
								</xsl:when>
								<xsl:otherwise>
									<xsl:apply-templates select="node()" mode="cntntlnks"/>			
								</xsl:otherwise>
							</xsl:choose>		
						</li>		
					</xsl:for-each>	
				</ul>
			</div>
		</xsl:when>

	
		<!-- if there is only one column -->
		<xsl:otherwise>
		
			<ul class="publications-content">
			
				<xsl:for-each select="tbody/tr/td">	
					<li>	
						<xsl:choose>
							<xsl:when test=".//img">
								<div class="holder">	
									<!-- template with special case for images -->	
									<xsl:apply-templates select="node()" mode="cntntlnks"/>			
								</div>									
							</xsl:when>
							<xsl:otherwise>
								<xsl:apply-templates select="node()" mode="cntntlnks"/>			
							</xsl:otherwise>
						</xsl:choose>	
					</li>	
				</xsl:for-each>
		
			</ul>
		
		</xsl:otherwise>	
		
	</xsl:choose>	
	</xsl:template>
	
	<!-- special case for links with images, wrap those in a div -->
	<xsl:template match="element()[name()!='img']" mode="cntntlnks">
		<xsl:choose>
			<xsl:when test="img">		
				<!-- if anchor has and image, wrap the area in a div -->
				<div class="{if($pageType='landing') then('image-holder') else('image')}">
					<xsl:apply-templates select="." mode="copy"/>
				</div>		
			</xsl:when>
			<xsl:otherwise>
				<!-- copy normally -->
				<xsl:apply-templates select="." mode="copy"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<!-- special case for stand alone images, not in a link -->
	<xsl:template match="img" mode="cntntlnks">
				<div class="{if($pageType='landing') then('image-holder') else('image')}">
					<xsl:apply-templates select="." mode="copy"/>
				</div>		
	</xsl:template>
	
	<!-- ADDITIONAL/MEDIA LINKS BEGIN (Page 2) -->

	<xsl:template match="table[@class='mscp-addtnl-lnks']" mode="copy">
		<nav class="media-list">
			<xsl:apply-templates select="tbody/tr/td/node()" mode="copy"/>
		</nav>
	</xsl:template>
	
	<!-- END ADDITIONAL LINKS -->
	
	<!-- CONTACT -->
	<xsl:template match="table[@class='mscp-cntct']" mode="copy">
		<address>	
			<xsl:for-each select="tbody/tr/td">		
			
			<xsl:choose>
			
			<!-- Place each cell in a row, unless that cell is the last one on a county page -->
			<xsl:when test="position()!=last() or not(contains($pageType,'counties'))">
				<div class="row">
					<xsl:apply-templates select="node()" mode="ul-to-dl"/>
				</div>
			</xsl:when>
			
			<xsl:otherwise>
					<xsl:apply-templates select="node()" mode="ul-to-dl"/>
			</xsl:otherwise>
			
			</xsl:choose>
				
			</xsl:for-each>
		</address>	
	</xsl:template>
	
	<!-- convert unordered list into contact definition lists -->
	<xsl:template match="ul" mode="ul-to-dl">
		<dl class="contact-list">
			<xsl:apply-templates select="attribute()" mode="copy"/>
			<xsl:for-each select="li">
			<xsl:choose>
			<xsl:when test="not(element()) and contains(text(),':')">
				<dt><xsl:copy-of select="substring-before(text(),':')" />:</dt>
				<dd><xsl:copy-of select="substring-after(text(),':')" /></dd>
			</xsl:when>
			<xsl:otherwise>
				<dt></dt>
				<dd><xsl:apply-templates select="attribute()|node()" mode="copy"/></dd>
			</xsl:otherwise>
			</xsl:choose>
			</xsl:for-each>
		</dl>
	</xsl:template>
	<!-- END CONTACT -->
	
	
	
	
	<!-- 
	
	Section 3
	
	COUNTY PAGE TABLE TRANSFORMATIONS
	
	* County Events (Shared globally)
	
	-->
	 
	 <!-- COUNTY PROGRAMS -->

	<xsl:template match="table[@class='cnty-evnts']" mode="copy">
		
		<ul class="event-list">
			<xsl:for-each select="tbody/tr">
				<li>
					<em class="date"><span><xsl:copy-of select="upper-case( string-join(td[1]/text())  )"/></span><xsl:apply-templates select="td[2]/text()"/></em>
					<xsl:apply-templates select="td[3]/node()" mode="copy"/>
				</li>
			</xsl:for-each>
		</ul>
		
	</xsl:template>
	
	
	<!-- END COUNTY PROGRAMS -->
	
	
	 <!--
	 
	
	 HOME PAGE TABLE TRANSFORMATIONS
	 
	 
	 -->
	 
	 <!-- 
	 
	 Section 4
	 
	 UAEX HOME PAGE
	 
	 * Row 1
	 * Row 2
	 * Row 3
	 
	 -->
	
	<xsl:template match="table[@class='hr1-imgtxtblck']" mode="copy">
	<xsl:for-each select="tbody/tr">
		<div class="holder">	
			<div class="photo">
				<xsl:for-each select="td[1]">
					<xsl:apply-templates select="node()" mode="js-img"/>
				</xsl:for-each>
			</div>
			
			<div class="description"><!-- anything that is not an image -->	
				<xsl:apply-templates select="td[2]/node()" mode="copy"/>
			</div>
			
		</div>
	</xsl:for-each>
	</xsl:template>
	
	<xsl:template match="table[@class='hr2-imgtxtblck']" mode="copy">
	<xsl:for-each select="tbody/tr">
		<article class="article">
			<div class="photo">
				<xsl:for-each select="td[1]">
					<xsl:apply-templates select="node()" mode="copy"/>
				</xsl:for-each>
			</div>
			
			<div class="description"><!-- anything that is not an image -->	
				<xsl:apply-templates select="td[2]/node()" mode="copy"/>
			</div>
			
		</article>
	</xsl:for-each>
	</xsl:template>
	
	<xsl:template match="table[@class='hr3-imgtxtblck']" mode="copy">
	<xsl:for-each select="tbody/tr">
		<article class="article g1">	
			<div class="photo">
				<xsl:for-each select="td[1]">
					<xsl:apply-templates select="node()" mode="copy"/>
				</xsl:for-each>
			</div>
			
			<div class="description"><!-- anything that is not an image -->	
				<xsl:apply-templates select="td[2]/node()" mode="copy"/>
			</div>
			
		</article>
	</xsl:for-each>
	</xsl:template>
	
	<!-- END UAEX HOME PAGE -->
	 
	 <!-- 
	 
	 Section 5
	 
	 4H HOME PAGE 
	 
	 * Image and List Content Block
	 * Image and Text Content block
	 
	 -->

	<xsl:template match="table[@class='h4h-imglstblck']" mode="copy">
	<xsl:for-each select="tbody/tr">
			<div class="photo">
				<xsl:for-each select="td[1]">
					<xsl:apply-templates select="node()" mode="copy"/>
				</xsl:for-each>
			</div>
			
			<nav class="nav"><!-- anything that is not an image -->	
				<xsl:apply-templates select="td[2]/node()" mode="copy"/>
			</nav>
	</xsl:for-each>
	</xsl:template>
	
	<xsl:template match="table[@class='h4hr3-imgtxtblck']" mode="copy">
	<xsl:for-each select="tbody/tr">
		<div class="article-holder">	
			<div class="photo">
				<xsl:for-each select="td[1]">
					<xsl:apply-templates select="node()" mode="js-img"/>
				</xsl:for-each>
			</div>
			
			<div class="description"><!-- anything that is not an image -->	
				<xsl:apply-templates select="td[2]/node()" mode="copy"/>
			</div>
			
		</div>
	</xsl:for-each>
	</xsl:template>
	
	<!-- END 4H HOME PAGE -->
	 
	 <!-- 
	 
	 Section 6
	 
	 DIVISIONS HOME PAGE 
	 
	 * Image and Text Content Block
	 * Info "Websites" List
	 
	 -->

	<xsl:template match="table[@class='hdvns-imgtxtblck']" mode="copy">
	<xsl:for-each select="tbody/tr">
	<div class="person-block g1">
			<div class="photo">			
					<xsl:apply-templates select="td[1]/node()" mode="js-img"/>
			</div>
			
			<div class="description"><!-- anything that is not an image -->	
				<xsl:apply-templates select="td[2]/node()" mode="copy"/>
			</div>
	</div>
			
	</xsl:for-each>
	</xsl:template>
	
	<!-- division web site list -->
	<xsl:template match="table[@class='hdvns-inflst']" mode="copy">
	<ul class="info-list">
	<xsl:for-each select="tbody/tr/td">
		<li>		
			<xsl:apply-templates select="node()" mode="copy"/>
		</li>
	</xsl:for-each>
	</ul>
	</xsl:template>
	
	<!-- END DIVISIONS HOME PAGE -->
	 
	
	<!-- 
	
	END HOME PAGES 
	
	-->

    
    
</xsl:stylesheet>
