<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY amp   "&#38;">
<!ENTITY copy   "&#169;">
<!ENTITY gt   "&#62;">
<!ENTITY hellip "&#8230;">
<!ENTITY laquo  "&#171;">
<!ENTITY lsaquo   "&#8249;">
<!ENTITY lsquo   "&#8216;">
<!ENTITY lt   "&#60;">
<!ENTITY nbsp   "&#160;">
<!ENTITY quot   "&#34;">
<!ENTITY raquo  "&#187;">
<!ENTITY rsaquo   "&#8250;">
<!ENTITY rsquo   "&#8217;">
]>
<!--
Implementations Skeleton - 01/10/2017

HOME PAGE
A complex page type.

Contributors: Your Name Here
Last Updated: Enter Date Here
-->
<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"
	expand-text="yes">

	<xsl:import href="common.xsl"/>

	<xsl:variable name="sitemap" select="document(concat($domain, '/sitemap.xml'))" />
	<xsl:variable name="root" select="concat($ou:root, $ou:site)" />

	<xsl:variable name="pages">
		<pages>
			<xsl:for-each select="$sitemap/*:urlset/*:url/*:loc[not(ends-with(text(), 'xml')) and not(ends-with(text(), 'inc')) and not(contains(text(), 'props.html'))]">
				<xsl:sort select="." order="ascending"/>
				<page published="{unparsed-text-available(.)}">
					<xsl:variable name="local-location" select="replace(text(), $domain, $root)" />
					<xsl:variable name="location-pcf">
						<xsl:for-each select="tokenize($local-location, '/')">
							<xsl:variable name="pos" select="position()" />
							<xsl:choose>
								<!-- output the elemtn if not last -->
								<xsl:when test="not(position() = last())"><xsl:value-of select="." />/</xsl:when>
								<xsl:when test="position() = last()">
									<!-- if last remove extension and replace -->
									<xsl:for-each select="tokenize(., '\.')[position() != last()]">
										<xsl:value-of select="." />
									</xsl:for-each>
									<xsl:value-of select="'.pcf'" />
								</xsl:when>
							</xsl:choose>
						</xsl:for-each>
					</xsl:variable>
					<staging-location>
						<xsl:variable name="staging">
							<xsl:for-each select="tokenize($location-pcf, '/')[position() != last()]"><xsl:value-of select="." />/</xsl:for-each>
						</xsl:variable>
						<xsl:variable name="staging-clean" select="replace($staging, $root, '')" />
						<xsl:attribute name="slash-count" select="string-length($staging-clean) - string-length(translate($staging-clean, '/', ''))" />
						<xsl:value-of select="$staging-clean" />
					</staging-location>
					<title>
						<xsl:choose>
							<xsl:when test="doc-available($location-pcf)">
								<xsl:variable name="cur-doc" select="document($location-pcf)/document/ouc:properties/title" />
								<xsl:attribute name="first-letter" select="upper-case(substring($cur-doc,1,1))"/>
								<xsl:value-of select="$cur-doc" />
							</xsl:when>
							<xsl:otherwise>
								<xsl:attribute name="available">false</xsl:attribute>
							</xsl:otherwise>
						</xsl:choose>
					</title>
					<url><xsl:value-of select="text()" /></url>
				</page>
			</xsl:for-each>
		</pages>
	</xsl:variable>

	<xsl:variable name="all-pages">
		<pages>
			<xsl:for-each select="$pages/pages/page">
				<xsl:sort select="staging-location/text()" />
				<xsl:sort select="url/text()" />
				<xsl:copy-of select="." />
			</xsl:for-each>
		</pages>
	</xsl:variable>

	<xsl:template name="page-content">
		<xsl:variable name="col-med" select="if (ou:pcf-param('aside') = 'display') then 'col-md-10 col-sm-push-2' else 'col-md-12'" />

		<section class="page-header background-color-secondary custom-text-color-white">
			<div class="container">
				<div class="row">
					<div class="col-md-12">

						<xsl:call-template name="breadcrumb" />

					</div>
				</div>
			</div>
		</section>
		<div class="container">
			<div class="row">

				<div class="{$col-med} interior-content match-height">

					<xsl:if test="ou:pcf-param('heading') != ''">

						<div class="heading heading-border heading-middle-border">

							<h1>{ou:pcf-param('heading')}</h1>

						</div>

					</xsl:if>

					<xsl:apply-templates select="ouc:div[@label = 'maincontent']" />

					<xsl:call-template name="site-map" />

				</div>

				<xsl:if test="ou:pcf-param('aside') = 'display'">

					<div class="col-md-2 interior-navigation match-height col-sm-pull-10">
						<aside class="sidebar">

							<xsl:if test="doc-available($props-path)">

								<span class="heading-primary">{ou:pcf-param('breadcrumb', doc($props-path))}</span>

							</xsl:if>

							<xsl:if test="ou:pcf-param('nested-navigation') = 'display'">

								<xsl:call-template name="nested-navigation" />

							</xsl:if>

							<xsl:if test="ou:pcf-param('region-aside') = 'display'">

								<xsl:apply-templates select="ouc:div[@label = 'region-aside']" />

							</xsl:if>

						</aside>
					</div>

				</xsl:if>

			</div>
		</div>

	</xsl:template>

	<xsl:template name="site-map">
		<xsl:choose>
			<xsl:when test="not($sitemap = '')">
				<xsl:if test="not($is-pub)">
					<div class="alert alert-info">
						<h2>System Message</h2>
						<p>Pages and directories can be excluded from this page by selecting "Exclude From Site Map" in access settings.</p>
						<p>Pages with empty titles, and pages that have not been published are excluded as well.</p>
						<p>After changes are made, the sitemap will need to be regenreated and this page republished.</p>
					</div>
				</xsl:if>
				<xsl:choose>
					<xsl:when test="$page-type = 'a-to-z'">
						<xsl:call-template name="a-to-z" />
					</xsl:when>
					<xsl:when test="$page-type = 'sitemap'">
						<xsl:call-template name="sitemap-output" />
					</xsl:when>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise>
				<xsl:if test="not($is-pub)">
					<p>Please generate a sitemap for the current site.</p>
				</xsl:if>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="page" mode="sub-level">
		<xsl:param name="count" select="staging-location/@slash-count" />
		<xsl:param name="current-dir" select="staging-location/text()" />
		<xsl:param name="index-file-name" select="'default.aspx'" />
		<xsl:variable name="pos" select="position()" />
		<li data-count="{$count}">
			<a href="{url}" data-val="{$current-dir}"><xsl:value-of select="title" /></a>
		</li>
		<!-- 		<p><xsl:value-of select="$pos" /></p> -->
		<!-- if there is a folder that needs to be nested -->
		<xsl:if test="following-sibling::page[staging-location/@slash-count = $count + 1 or staging-location/@slash-count = $count][contains(staging-location/text(), $current-dir)] and $pos = last()">
			<!-- loop over the default -->
			<xsl:for-each select="following-sibling::page[staging-location/@slash-count = $count + 1][contains(url, $index-file-name)][contains(staging-location/text(), $current-dir)]">
				<xsl:variable name="sub-cur" select="staging-location" />
				<xsl:variable name="sub-cur-count" select="staging-location/@slash-count" />
				<li data-count="{$sub-cur-count}">
					<a href="{url}"><xsl:value-of select="title" /></a>
					<ul>
						<xsl:apply-templates select="preceding-sibling::page[staging-location/@slash-count = $count + 1 or staging-location/@slash-count = $count][contains(staging-location/text(), $sub-cur)][not(contains(url, $index-file-name))]" mode="#current" />
						<xsl:apply-templates select="following-sibling::page[staging-location/@slash-count = $count + 1 or staging-location/@slash-count = $count][contains(staging-location/text(), $sub-cur)][not(contains(url, $index-file-name))]" mode="#current" />
					</ul>
				</li>
			</xsl:for-each>
		</xsl:if>

	</xsl:template>

	<xsl:template name="sitemap-output">
		<ul>
			<!-- apply templates to the root level pages -->
			<xsl:apply-templates select="$all-pages/pages/page[@published='true'][normalize-space(title/@first-letter) != ''][staging-location/@slash-count = 1]" mode="sub-level"/>
		</ul>
	</xsl:template>

	<!-- -->
	<xsl:template name="a-to-z">
		<ul class="list-inline">
			<xsl:for-each-group select="$pages/pages/page[@published='true'][normalize-space(title/@first-letter) != '']" group-by="title/@first-letter">
				<xsl:sort select="current-grouping-key()"/>
				<li><a href="#letter-{current-grouping-key()}"><xsl:value-of select="current-grouping-key()"/></a></li>
			</xsl:for-each-group>
		</ul>
		<xsl:for-each-group select="$pages/pages/page[@published='true'][title/@first-letter != '']" group-by="title/@first-letter">
			<xsl:sort select="current-grouping-key()"/>
			<p id="letter-{current-grouping-key()}"><xsl:value-of select="current-grouping-key()"/></p>
			<ul>
				<xsl:for-each select="current-group()">
					<xsl:sort select="title" />
					<li class="azpagelink"><a href="{url}"><xsl:value-of select="title"/></a></li>
				</xsl:for-each>
			</ul>
		</xsl:for-each-group>
	</xsl:template>

</xsl:stylesheet>
