<!DOCTYPE xsl:stylesheet SYSTEM "http://commons.omniupdate.com/dtd/standard.dtd" >
<!-- 
FUNCTIONS REPOSITROY
A repository of multipurpose functions without documentation
-->
<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:err="http://www.w3.org/2005/xqt-errors"
  xmlns:ouc="http://omniupdate.com/XSL/Variables"
  exclude-result-prefixes="ou xsl xs fn ouc">
	  	  
<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')"/>

<xsl:function name="ou:navigation">
<xsl:param name="navfile"/>
<xsl:variable name="dirname">
	<xsl:choose>
		<xsl:when test="string-length($ou:dirname) = 1">
			<xsl:value-of select="$ou:dirname" />
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="concat($ou:dirname,'/')" />
		</xsl:otherwise>
	</xsl:choose>
</xsl:variable>
<xsl:variable name="navigation" select="concat($dirname,$navfile)" />
<xsl:value-of select="concat($dirname,$navfile)"/>

</xsl:function>

<xsl:function name="ou:faculty-listing">
	<xsl:param name="fac_loc"/>
	<xsl:param name="fac_loc_pro"/>
	<xsl:param name="file_type"/>
	<xsl:try>
	<xsl:variable name="all"><xsl:copy-of select="doc($fac_loc)/list"/></xsl:variable>

		<xsl:for-each select="$all/list/file">
			 <xsl:sort select="." order="ascending" />
			 <xsl:variable name="file_name"><xsl:value-of select="."/></xsl:variable>
			 <xsl:if test="not(($file_name='_leftnav.inc') or ($file_name='_properties.inc') or ($file_name='index.pcf'))">
			 <xsl:variable name="xml_available" as="xs:boolean"><xsl:copy-of select="unparsed-text-available(concat($fac_loc_pro,'/',concat(substring-before($file_name,'.pcf'),'.xml')))"/></xsl:variable>
			 <xsl:variable name="html_available" as="xs:boolean"><xsl:copy-of select="unparsed-text-available(concat($fac_loc_pro,'/',concat(substring-before($file_name,'.pcf'),'.',$file_type)))"/></xsl:variable>
			 <xsl:if test="((boolean($html_available)) and boolean($xml_available))">	 
				 <faculty>	
					<title><xsl:value-of select="doc(concat($fac_loc_pro,'/',concat(substring-before($file_name,'.pcf'),'.xml')))/root/title/node()"/></title>
					<image><xsl:value-of select="doc(concat($fac_loc_pro,'/',concat(substring-before($file_name,'.pcf'),'.xml')))/root/image/node()"/></image>
				 	<bio><xsl:value-of disable-output-escaping="yes" select="doc(concat($fac_loc_pro,'/',concat(substring-before($file_name,'.pcf'),'.xml')))/root/bio/node()"/></bio>
				 	<link><xsl:value-of select="concat($fac_loc_pro,'/',concat(substring-before($file_name,'.pcf'),'.html'))"/></link>
				 </faculty>
			 </xsl:if>
			 </xsl:if>
		</xsl:for-each>
		<xsl:catch>
			<!-- <error><xsl:value-of select="$err:description"/></error> -->
		</xsl:catch>
	</xsl:try>
</xsl:function>

<xsl:function name="ou:faculty-listing">
	<xsl:param name="fac_loc"/>
	<xsl:param name="fac_loc_pro"/>
	<xsl:copy-of select="ou:faculty-listing($fac_loc,$fac_loc_pro,'html')"/>
</xsl:function>

<!-- BREADCRUMBS WITH PREDEFINED TITLE -->
<xsl:function name="ou:breadcrumbs-title">
 <xsl:param name="path" as="xs:string"/>
  <xsl:param name="file" as="xs:string"/>
 <xsl:param name="title" as="xs:string"/>

<xsl:param name="deliminator"/>
 <xsl:copy-of select="ou:breadcrumbs($path,$file,$deliminator)"/>

 <li><xsl:value-of select="normalize-space($title)"/></li><!-- Style the output breadcrumbs based on request -->

   
</xsl:function>


<!-- BREADCRUMBS AND NO TITLE -->
<xsl:function name="ou:breadcrumbs">
	 <xsl:param name="path" as="xs:string"/>
	 <xsl:param name="file" as="xs:string"/>
	 <xsl:param name="deliminator"/>
	 <xsl:if test="$path ne '/'">
	
	  <xsl:variable name="list" select="tokenize(substring($path, 2), '/')"/>
	  <xsl:variable name="parent" select="concat('/', string-join(remove($list, count($list)), '/'))"/>

	   <xsl:copy-of select="ou:breadcrumbs($parent,$file,$deliminator)"/>
	 </xsl:if>
	<!-- <xsl:variable name="href" select="if ($path eq '/') then $path else concat($path, '/')"/>-->
	
	<xsl:variable name="base_href">
	   <xsl:choose>
	  	<xsl:when test="$path eq '/'">
	  		<xsl:value-of select="concat($path, '')"/>
	 	</xsl:when>
	 	<xsl:otherwise>
	 		<xsl:value-of select="concat($path, '/')"/>
	 	</xsl:otherwise>
	  </xsl:choose>
	 </xsl:variable>
	
	 <xsl:variable name="href">
	  <xsl:choose>
	  	<xsl:when test="$path eq '/'">
	  		<xsl:value-of select="concat($ou:httproot,'')"/><!-- If the staging ftp path differs from the production ftp path -->
	 	</xsl:when>
	 	<xsl:otherwise>
	 		<xsl:value-of select="concat($path, '/')"/><!-- If the staging ftp path differs from the production ftp path -->
	 	</xsl:otherwise>
	  </xsl:choose>
	 </xsl:variable>
	
	  <li><a href="{$href}">
	    <xsl:variable name="list1" select="tokenize(substring($path, 2), '/')"/>
	    <xsl:variable name="parent1" select="concat('/', string-join(remove($list1, count($list1)), '/'))"/>
	   <xsl:variable name="b_name"><xsl:value-of select="doc(concat($ou:root,$ou:site,$base_href,$file))//parameter[@name='breadcrumbs']/node()"/></xsl:variable>
	   <xsl:choose>
	   	<xsl:when test="string-length(normalize-space($b_name)) lt 1">
				<xsl:choose>
					<xsl:when test="normalize-space($path) = '/'">Home</xsl:when>
					<xsl:otherwise>
						<xsl:copy-of select="ou:current_folder($path)"/>
					</xsl:otherwise>
				</xsl:choose>
					
	   	</xsl:when>
	   	<xsl:otherwise> 
				<xsl:value-of select="($b_name)" />
	   	</xsl:otherwise>
	   </xsl:choose> 
	  </a><xsl:copy-of select="$deliminator"/></li>
</xsl:function>


<!-- 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>

<!-- 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>

<!-- 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 Month -->
<xsl:function name="ou:rss-by-month">
	<xsl:param name="rss" />
	<xsl:param name="month" as="xs:string"/>
	<xsl:for-each select="$rss/channel/item">
			  <xsl:sort select="ou:rfc-to-dateTime(./pubDate)" order="descending" />
			 <!-- <pubdate><xsl:value-of select="ou:rfc-to-dateTime(pubDate)"/></pubdate>-->		
			 <xsl:if test="ou:translateMonth(month-from-dateTime(ou:rfc-to-dateTime(pubDate))) = $month">
			 	<p>
			 		 <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>
				</p>
			 </xsl:if>
	</xsl:for-each>
</xsl:function>

<!-- WNL RSS by Year -->
<xsl:function name="ou:rss-by-year">
	<xsl:param name="rss" />
	<xsl:param name="year" as="xs:integer"/>
	<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">
			 	<year>
			 		  <month><xsl:value-of select="ou:translateMonth(month-from-dateTime(ou:rfc-to-dateTime(pubDate)))"/></month>
					  <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>
				</year>
			 </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>

<xsl:function name="ou:strim" as="xs:string">
	<xsl:param name="string" as="xs:string?"/> 
	<xsl:param name="length" as="xs:integer"/> 
		<xsl:sequence select="substring($string,1,$length)"/>
</xsl:function>

<xsl:function name="ou:forloop">
<xsl:param name="i" /> 
<xsl:param name="count" />
	<xsl:if test="$i &lt;= $count"> 
	<br /> <b><xsl:value-of select="$i" />.</b>Hello
	</xsl:if> 
	<xsl:if test="$i &lt;= $count">
		<xsl:copy-of select="ou:forloop($i + 1,$count)"/>
	</xsl:if>
</xsl:function>

<!-- get Parent -->
<xsl:function name="ou:getParent">
    <xsl:param name="path" as="xs:string"/>
    <xsl:variable name="path-length" select="string-length($path)"/>
    <xsl:variable name="ends-with-slash" select="ends-with($path,'/')"/>
    <xsl:variable name="final-path">
     <xsl:choose>
       <xsl:when test="$ends-with-slash">      
         <xsl:value-of select="substring($path,1,($path-length)-1)"/>     
       </xsl:when>
       <xsl:otherwise>
        <xsl:value-of select="$path"/>     
       </xsl:otherwise>
     </xsl:choose>
   </xsl:variable>
    <xsl:variable name="last" select="tokenize($final-path, '/')[last()]"/>
   <xsl:value-of select="replace($final-path,$last,'')"/>
</xsl:function>


</xsl:stylesheet>
