<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet>
<!-- 
FUNCTIONS for Kitchen Sink v2 
Repository of active functions for the skeleton.
See _examples/functions-repository.xsl for the old version of this file - a suite of useful functions.

Variable Dependencies: (see vars xsl)
$ou:production root 
$serverType

last updated 4/2013
-->
<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="ou xsl xs fn ouc">


<!-- youTube -->

	<!-- generate an ID for the video lightbox div based on the href --> 
	<xsl:function name="ou:hrefToID">
	<!-- string to translate -->
	<xsl:param name="string"/>
		<!-- static set of characters valid for the id -->
		<xsl:variable name="allValidChars" select="'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
		<!-- get invalid characters from the link -->
		<xsl:variable name="invalidChars" select="translate($string, $allValidChars, '')"/>
		<!-- remove invalid characters from the link -->
		<xsl:variable name="id" select="translate($string,$invalidChars,'')"/> 
		<!-- return the id -->
		<xsl:value-of select="$id"/> 
	</xsl:function>
	
	<!-- extract the YouTube id from a link and remove any parameters -->
	
	<xsl:function name="ou:hrefToYouTube">
		<!-- string to translate -->
		<xsl:param name="string"/>
		<!-- substring-after -->
		<xsl:param name="after"/>
		<!-- token -->
		<xsl:param name="token"/>
		
		<!-- get information about the video -->
		<xsl:variable name="parameters" select="substring-after($string,$after)"/>
		<!-- remove extra parameters -->
		<xsl:variable name="id" select="if(contains($parameters,$token)) then(substring-before($parameters,$token)) else($parameters)"/>
		<!-- return the id -->
		<xsl:value-of select="$id"/>
	</xsl:function>
	
<!-- end youTube -->
	

<xsl:function name="ou:getWholeMonth">
<xsl:param name="cur_month"/>

	<xsl:for-each select="$months_map">
	
		<xsl:if test="$cur_month = normalize-space(substring-before(.,':'))">	
										
			<xsl:value-of select="substring-after(.,':')"/>
			
		</xsl:if>
		
	</xsl:for-each>

</xsl:function>

<xsl:function name="ou:getShortMonth">
<xsl:param name="cur_month"/>

	<xsl:for-each select="$months_map">
	
		<xsl:if test="$cur_month = normalize-space(substring-after(.,':'))">	
										
			<xsl:value-of select="substring-before(.,':')"/>
			
		</xsl:if>
		
	</xsl:for-each>

</xsl:function>


<!-- 
INCLUDE FUNCTIONS
How to use:
<xsl:copy-of select="ou:standardInclude('/_resources/includes/headcode.inc')"/>
Use to determine appropariate method of including content on a page
Contributors:
-->
		
		<xsl:function name="ou:standardInclude">
			<xsl:param name="f" />
			
			
			<xsl:choose>
				<xsl:when test="$ou:action != 'pub'">
					<xsl:copy-of select="ou:stagingInclude($f)"/>
				</xsl:when>
				<xsl:otherwise>	 	  	  	  
					<!-- change directly to ou:virtInc or ou:phpInc if serverType will not change -->
					<xsl:copy-of select="if($serverType='asp') then ou:virtInc($f) else ou:phpInc($f)"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:function>
		
				
		<xsl:function name="ou:stagingInclude">
			<xsl:param name="f"/>
			<ouc:div label="{$f}" path="{$f}"/> <!-- change to comment tags if desired with xsl:comment -->
		</xsl:function>
		
		<xsl:function name="ou:phpInc">
			<xsl:param name="f"/>
			<xsl:processing-instruction name="php"> 
				include($_SERVER['DOCUMENT_ROOT'] . "<xsl:value-of select="$f"/>");
				?</xsl:processing-instruction>		
		</xsl:function>
		
		<xsl:function name="ou:virtInc">
			<xsl:param name="f"/>
			<xsl:comment>#include virtual="<xsl:value-of select="$f"/>" </xsl:comment>
		</xsl:function>
		
<!-- 
PCF PARAMS
An extremely useful function for getting page properties without needing to type the full xpath.
How to use:
The pcf has a parameter, name="pagetype". To get the value and store it in an XSL param : 
<xsl:param name="pagetype" select="ou:pcfparam('pagetype')"/> 
Use wherever you need it
Contributors: Chul Kim
-->
		<xsl:param name="pcfparams" select="/*:document/config//parameter"/> <!-- save all page properties in a variable -->

		<xsl:function name="ou:pcfparam">
			<xsl:param name="name"/>
			<xsl:variable name="parameter" select="$pcfparams[@name=$name]"/>
			<xsl:choose>
				<xsl:when test="$parameter/@type = 'select' or $parameter/@type = 'radio'">
					<xsl:value-of select="$parameter/option[@selected = 'true']/@value"/>
				</xsl:when>
				<xsl:when test="$parameter/@type = 'checkbox'">
					<xsl:copy-of select="$parameter/option[@selected = 'true']/@value"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$parameter/text()"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:function>
		
<!-- 
PROPS PARAMS 
Essentially a clone of pcfparams
Use this to grab parameters from your section properties files.


	<xsl:param name="props-file" select="'_props.pcf'"/> 	
	<xsl:param name="props-path" select="concat($ou:root, $ou:site, $dirname, $props-file)"/>
	
Note: $props-path is defined in vars xsl, and this variable is shared with breadcrumbs.xsl
Contributors: Caroline Spooner
-->		

		<xsl:param name="propsparams" select="document($props-path)//parameter"/> <!-- save all section properties in a variable -->	

		<xsl:function name="ou:propsparam">
			<xsl:param name="name"/>
			
			<xsl:variable name="parameter" select="$propsparams[@name=$name]"/>
			<xsl:choose>
				<xsl:when test="$parameter/@type = 'select' or $parameter/@type = 'radio'">
					<xsl:value-of select="$parameter/option[@selected = 'true']/@value"/>
				</xsl:when>
				<xsl:when test="$parameter/@type = 'checkbox'">
					<xsl:copy-of select="$parameter/option[@selected = 'true']/@value"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$parameter/text()"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:function>	
		
		<xsl:function name="ou:propsparam">
		<xsl:param name="name"/>
		<xsl:param name="path"/>
		
		<xsl:if test="doc-available($path)">					
			<xsl:variable name="parameter" select="document($path)/directory/config//parameter[@name=$name]"/>
			
			<xsl:choose>
				<xsl:when test="$parameter/@type = 'select' or $parameter/@type = 'radio'">
					<xsl:value-of select="$parameter/option[@selected = 'true']/@value"/>
				</xsl:when>
				<xsl:when test="$parameter/@type = 'checkbox'">
					<xsl:copy-of select="$parameter/option[@selected = 'true']/@value"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$parameter/text()"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:if>
		
		</xsl:function>	
		
<!-- 
ASSIGN VARIABLE
Concisely assign fallback variables to prevent unexpected errors in development and post implementation.

How to use:
<xsl:param name="galleryType" select="ou:assignVariable('galleryType','PrettyPhoto')"/>	
<xsl:variable name="navfile" select="ou:assignVariable('page-nav',$ou:navigation,$local-nav)"/>

Third parameter may be a string or variable. The first parameter must always be a string, as required by the pcfparam function.

Second version requires overwriteDirectory variable, which is typically defined in vars xsl.

<xsl:variable name="overwriteDirectory" select="ou:assignVariable('overwriteDirectory','no')"/> 


Note: requires the function ou:pcfparams
Contributors: Caroline Spooner 
-->
		
		<xsl:function name="ou:assignVariable"> <!-- test if page property has value, give default value if none -->
			<xsl:param name="var"/>
			<xsl:param name="fallback"/>
			
			<xsl:variable name="pcf-var" select="ou:pcfparam($var)"/>
			
			<xsl:choose>	
				<xsl:when test="string-length($pcf-var) > 0"><xsl:value-of select="$pcf-var"/></xsl:when>
				<xsl:otherwise><xsl:value-of select="$fallback"/></xsl:otherwise>
			</xsl:choose>	
			
		</xsl:function>		
		
		<xsl:function name="ou:assignVariable"> <!-- pcf - dir var - fallback precedence, also tests if there is a value for each -->
			<xsl:param name="var"/>
			<xsl:param name="dir-var"/>
			<xsl:param name="fallback"/>
						
			<xsl:variable name="pcf-var" select="ou:pcfparam($var)"/>
			
			<xsl:choose>	
				<xsl:when test="string-length($pcf-var) > 0"><xsl:value-of select="$pcf-var"/></xsl:when>
				<xsl:when test="string-length($dir-var) > 0" ><xsl:value-of select="$dir-var"/></xsl:when> 
				<xsl:otherwise><xsl:value-of select="$fallback"/></xsl:otherwise>
			</xsl:choose>	
		
		</xsl:function>
		
<!-- 
TEST VARIABLE
A simpler version of Assign Variable, if you are not dealing with page properties.
Contributors: Caroline Spooner
-->		
		
		<xsl:function name="ou:testVariable"> <!-- test if variable has value, give default value if none -->
			<xsl:param name="var"/>
			<xsl:param name="fallback"/>
		
			<xsl:choose>	
				<xsl:when test="string-length($var) > 0"><xsl:value-of select="$var"/></xsl:when>
				<xsl:otherwise><xsl:value-of select="$fallback"/></xsl:otherwise>
			</xsl:choose>	
			
		</xsl:function>	
		
<!--
FIND PREVIOUS DIRECTORY
Used by breadcrumbs xsl.
Contributors: 
-->
		
		<xsl:function name="ou:findPrevDir"> <!-- outputs parent directory path with trailing '/': /path/to/parent/ -->
		<xsl:param name="path" />
		<xsl:variable name="tokenPath" select="tokenize(substring($path, 2), '/')[if(substring($path,string-length($path)) = '/') then position() != last() else position()]" />
		<xsl:variable name="newPath" select="concat('/', string-join(remove($tokenPath, count($tokenPath)), '/') ,'/')"/>
		<xsl:value-of select="if($newPath = '//') then '/' else $newPath" />
		</xsl:function>
		
<!-- 
WNL ARCHIVE FUNCTIONS	
Used by archive xsl.
Can be adapted for various WNL purposes.
Contributors:
-->

<xsl:param name="months" select="('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')"/>
<xsl:param name="months_map" select="('Jan:January', 'Feb:Febuary', 'Mar:March', 'Apr:April', 'May:May', 'Jun:June', 'Jul:July', 'Aug:August', 'Sep:September', 'Oct:October', 'Nov:November', 'Dec:December')"/>

<!-- Translate Numbers to Month -->
<xsl:function name="ou:translateMonth">
	<xsl:param name="month" as="xs:integer"/>
	 <xsl:for-each select="$months">
			<xsl:if test="$month eq position()">
				<xsl:value-of select="."/>
			</xsl:if>
	 </xsl:for-each>
</xsl:function>

<!-- WNL RSS by Year and Month -->
<xsl:function name="ou:rss-by-month-year">
	<xsl:param name="rss" />
	<xsl:param name="year" as="xs:integer"/>
	<xsl:param name="month" as="xs:string"/>
	<xsl:for-each select="doc($rss)//item">
			  <xsl:sort select="ou:rfc-to-dateTime(./pubDate)" order="descending" />
			  
			 <xsl:if test="(year-from-dateTime(ou:rfc-to-dateTime(pubDate)) = $year) and (ou:translateMonth(month-from-dateTime(ou:rfc-to-dateTime(pubDate))) = $month)">
			 	<month>
			 		  <month><xsl:value-of select="ou:translateMonth(month-from-dateTime(ou:rfc-to-dateTime(pubDate)))"/></month>
					  <year><xsl:value-of select="year-from-dateTime(ou:rfc-to-dateTime(pubDate))"/></year>
					  <date><xsl:value-of select="day-from-dateTime(ou:rfc-to-dateTime(pubDate))"/></date>
					  <title><xsl:value-of select="./title"/></title>
					  <description><xsl:value-of select="./description"/></description>
					  <author><xsl:value-of select="./author"/></author>
					  <link><xsl:value-of select="./link"/></link>
				</month>
			 </xsl:if>
	</xsl:for-each>
</xsl:function>

<!-- RFC date time converter -->
<xsl:function name="ou:rfc-to-dateTime" as="xs:dateTime*">
 <xsl:param name="date-time" as="xs:string"/>
	 <xsl:analyze-string select="$date-time" regex="((Mon|Tue|Wed|Thu|Fri|Sat|Sun), )?([0-3]?[0-9]) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ([1-9][0-9]{{3}}) ([0-2][0-9]:[0-5][0-9](:[0-5][0-9])?) (((\+|-)[0-1][0-9])([0-5][0-9]))">
		  <xsl:matching-substring>
			   <xsl:variable name="year" select="regex-group(5)"/>
			   <xsl:variable name="month-name" select="regex-group(4)"/>
			   <xsl:variable name="month">
				    <xsl:for-each select="$months">
					     <xsl:if test=". eq $month-name">
					      	<xsl:value-of select="format-number(position(), '00')"/>
					     </xsl:if>
				    </xsl:for-each>
			   </xsl:variable>
			   <xsl:variable name="day-of-month" select="regex-group(3)"/>
			   <xsl:variable name="day" select="substring(concat('0', $day-of-month), string-length($day-of-month))"/>
			   <xsl:variable name="time" select="regex-group(6)"/>
			   <xsl:variable name="zone" select="concat(regex-group(9), ':', regex-group(11))"/>
			   <xsl:value-of select="concat($year, '-', $month, '-', $day, 'T', $time, $zone)"/>
		  </xsl:matching-substring>
	 </xsl:analyze-string>
</xsl:function>


<!-- 
FOLDER/FILENAME FUNCTIONS
Used by archive xsl.
Can be adapeted to find information about the current folder, section, 
and to automatically generate friendly page and folder names.
 -->

<!-- GET CURRENT FOLDER -->
<xsl:function name="ou:current_folder">
	<xsl:param name="string"/>
	<xsl:variable name="chars" select="tokenize(substring-after($string,'/'), '/')"/>
	<xsl:for-each select="$chars">
	  <xsl:if test="position()=last()">
		<xsl:variable name="result"><xsl:value-of select="."/></xsl:variable>
	 	<!-- <xsl:value-of select="ou:capital(replace($result,'-',' '))" /> -->
	 	<!-- copy the when statement for more cleaning options -->
	 	<xsl:choose>
		 	<xsl:when test="contains($result,'_')">
		 		<xsl:value-of select="ou:clean($result,'_')"/>
	      	</xsl:when>
	      	<xsl:when test="contains($result,'-')">
		 		<xsl:value-of select="ou:clean($result,'-')"/>
	      	</xsl:when>
	      	<xsl:otherwise>
	      		<xsl:value-of select="ou:capital($result)"/>
	      	</xsl:otherwise>
      	</xsl:choose>
      </xsl:if>
	</xsl:for-each>
</xsl:function>

<!-- CAPITALIZE THE FIRST LETTER OF EVERY WORD -->
<xsl:function name="ou:capital">
    <xsl:param name="string"/>
	<xsl:variable name="chars" select="tokenize($string, ' ')"/>
	 <xsl:for-each select="$chars">
     		<xsl:variable name="key"><xsl:value-of select="."/></xsl:variable>
         	<xsl:variable name="firstletter1"><xsl:value-of select="upper-case(substring($key,1,1))" /></xsl:variable>
			<xsl:variable name="rest1"><xsl:value-of select="lower-case(substring($key,2))" /></xsl:variable>
			<xsl:variable name="result"><xsl:value-of select="concat($firstletter1,$rest1,' ')" /> </xsl:variable>
			<xsl:value-of select="$result" />
     </xsl:for-each>
</xsl:function>

<!-- Clean content by replacing content with space -->
<xsl:function name="ou:clean">
    <xsl:param name="string"/>
    <xsl:param name="deliminator"/>
	<xsl:variable name="chars" select="tokenize($string,$deliminator)"/>
	 <xsl:for-each select="$chars">
     		<xsl:variable name="key"><xsl:value-of select="."/></xsl:variable>
         	<xsl:variable name="firstletter1"><xsl:value-of select="upper-case(substring($key,1,1))" /></xsl:variable>
			<xsl:variable name="rest1"><xsl:value-of select="lower-case(substring($key,2))" /></xsl:variable>
			<xsl:variable name="result"><xsl:value-of select="concat($firstletter1,$rest1,' ')" /> </xsl:variable>
			<xsl:value-of select="$result" />
     </xsl:for-each>
</xsl:function>
		
</xsl:stylesheet>
