<?xml version="1.0" encoding="UTF-8"?>
<!-- 
Recursive Template Matches
These templates ensures that all content is copied, or applied to any existing template matches

Contributors: Vinit Varghese, Caroline Spooner
Last Updated: 7/10/13
-->
<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">
	
<!-- *Based on* Brandon's Updated Recursive Template Match (Jan 2013) Compatible with OUC Tags -->
<!-- Also copy elements applied with homepage-gallery mode unless an exception exists -->
 <xsl:template match="element()" mode="copy homepage-gallery homepage-4h-gallery js-img fs-img generic-table"> 
   <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="copy homepage-gallery homepage-4h-gallery js-img fs-img generic-table">
	<xsl:copy />
</xsl:template> 

<!-- Use this simpler one for Non-OUC (2012/2013)
<xsl:template match="attribute()|element()|text()|comment()" mode="copy">
  <xsl:copy>
         <xsl:apply-templates select="attribute()|node()" mode="copy"/>
  </xsl:copy>
</xsl:template> -->

<xsl:template match="ouc:*[$ou:action !='edt']" mode="copy">
       <xsl:apply-templates mode="copy" />
</xsl:template> 

<!--  FIX PHP for HTML OUTPUT -->
<xsl:template match="processing-instruction()" mode="copy">
    <xsl:processing-instruction name="php">
        <xsl:copy-of select="."/>
    ?</xsl:processing-instruction>
</xsl:template>

<!-- SELF CLOSING ELEMENTS MATCH to remove OUC namespace when output method is HTML -->
<xsl:template match="meta | br | hr | img" mode="copy">
  <xsl:element name="{local-name()}" ><xsl:for-each select="./attribute()"><xsl:attribute name="{name()}"><xsl:value-of select="."></xsl:value-of></xsl:attribute></xsl:for-each></xsl:element>
</xsl:template>

<!-- Copy the contents of the ouc:properties nodes if any  -->
<xsl:template match="ouc:properties" mode="copy">
    <xsl:apply-templates select="attribute()|node()"  mode="copy"/>
</xsl:template>

<!-- (refer to func spec pg#6) change links to the default page of a section to root-relative folder -->
<xsl:template match="@href" mode="#all">
	<xsl:attribute name="href" select="if(contains(.,'default.aspx')) then(substring-before(.,'default.aspx')) else(.)"/>
</xsl:template>
  
<!-- visual block -->
<xsl:template match="img[@class='image-to-block']" mode="#all">
  <div class="visual-block">
    <div class="photo">
     <img>
       <xsl:apply-templates select="attribute()|node()" mode="#current"/>
     </img>
    </div>
  </div> 
</xsl:template>

<xsl:template match="img[@class='image-to-div-nocf']" mode="#all">
    <div class="photo">
   	 	<img>
      	 <xsl:apply-templates select="attribute()|node()" mode="#current"/>
       	</img>
    </div>
</xsl:template>


<xsl:template match="img[@class='image-to-div']" mode="#all">
    <div class="photo">
   	 <img>
      	 <xsl:apply-templates select="attribute()|node()" mode="#current"/>
       </img>
    </div>
    <div class="cf"><!-- --></div>
</xsl:template>


<!-- lightbox -->
<xsl:template match="a[@class='open-lightbox']" mode="#all">
<!-- href indicates where the content is -->
<!-- common.xsl uses the href and creates a div with the contents from the file indicated by the href -->
<!-- id indicates what the ID of that div created by common.xsl is -->
<!-- the href of the link is then switched to the id -->

<xsl:choose>
	<xsl:when test="@id"> <!-- when lightbox is using internal content from an include file -->
		<a href="#{@id}">
			<xsl:apply-templates select="attribute()[name()!='id' and name()!='href']|node()" mode="copy"/>
		</a>
	</xsl:when>
	<xsl:otherwise> <!-- lightbox is external -->
		<a href="#{ou:hrefToID(@href)}">
			<xsl:apply-templates select="attribute()[name()!='id' and name()!='href']|node()" mode="copy"/>
		</a>
	</xsl:otherwise>
	
</xsl:choose>
	
</xsl:template>

  
  <!-- GENERIC TABLE -->

	<!-- add wrapper div to all tables -->
	<xsl:template match="table" mode="copy">
		<div class="table-holder">
			<table>
				<xsl:apply-templates select="attribute()|node()" mode="generic-table"/>			
			</table>
		</div>
	</xsl:template>
	
	<!-- make captions hidden -->
	<xsl:template match="caption" mode="generic-table">
		<caption class="hidden">
			<xsl:apply-templates select="attribute()|node()" mode="copy"/>
		</caption>
	</xsl:template>
	
	<!-- make sure that thead only has th elements -->
	<xsl:template match="thead/tr/td" mode="generic-table">
		<th>
			<xsl:apply-templates select="attribute()|node()" mode="copy"/>
		</th>
	</xsl:template>
	
	
	<!-- Javascript Image Size Optimization (used by various table transformations and editable regions -->
	
  
		<xsl:template match="a"  mode="fs-img js-img">
			<a>
				<xsl:apply-templates select="attribute()[name()!='width' and name()!='height']" mode="copy"/>
				<xsl:apply-templates select="img"  mode="#current"/>
			</a>
		</xsl:template>
		
		<!-- FS IMG -->
		
		<xsl:template match="img"  mode="fs-img">
			<xsl:copy-of select="ou:javascript-image('fs-img',.)"/>
		</xsl:template>
		
		<!-- REGULAR IMG -->
				
		<xsl:template match="img"  mode="js-img">
			<xsl:copy-of select="ou:javascript-image('img',.)"/>
		</xsl:template>
		
		<xsl:function name="ou:javascript-image">
		<xsl:param name="class"/>
		<xsl:param name="this"/>
		
			<img>
				<xsl:apply-templates select="$this/attribute()[name()!='class' and name()!='width' and name()!='height']" mode="copy"/>
				<xsl:attribute name="data-src" select="$this/@src"/>
				<xsl:attribute name="data-aspect-ratio" select="'auto'"/>
				<xsl:attribute name="class" select="$class"/>
			</img>
			<noscript>
			<img>
				<xsl:apply-templates select="$this/attribute()[name()!='width' and name()!='height']" mode="copy"/>
				<xsl:for-each select="$this/attribute()[name()!='width' and name()!='height']">
					<xsl:attribute name="{name()}" select="."/>
				</xsl:for-each>		
				<xsl:attribute name="width" select="if($this/@width) then ($this/@width) else ('104')"/>
				<xsl:attribute name="height" select="if($this/@height) then ($this/@height) else ('104')"/>
			</img>
			</noscript>
		</xsl:function>
		
</xsl:stylesheet>
