<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet [
	<!ENTITY e    "http://www.xs4all.nl/~pboot/p01/emblem.xml#" >
	<!ENTITY p01s "http://www.xs4all.nl/~pboot/p01/p01rdfs.rdf#" >
	<!ENTITY rdf  "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
	<!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
	<!ENTITY % charrefs SYSTEM "entities.dtd">
	%charrefs;
]>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  version="1.0"
  xmlns:saxon="http://icl.com/saxon"
  xmlns:p01s="http://www.xs4all.nl/~pboot/p01/p01rdfs.rdf#"
	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
	xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" 
  xmlns:pb="http://www.xs4all.nl/~pboot/"
  xmlns:func="http://exslt.org/functions"
  exclude-result-prefixes="saxon pb func rdf rdfs p01s"
  extension-element-prefixes="saxon pb func">

<xsl:include href="function.xsl"/>

<xsl:output method="xml" 
		omit-xml-declaration="no" 
		doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
		doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>

<xsl:variable name="textfrags" select="id(//ptr/@target)"/>

<xsl:template match="/">
	<html>
		<head>
 			<meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8"/>
			<meta name="ROBOTS" content="NOINDEX, NOFOLLOW"/>
			<title>Emblem</title>
      <style type="text/css">
					span {background-color:transparent;}
					* {font-size:11pt;}
			</style>
			<script type="text/javascript">
function getVar (bron,zoek) {
	x1 = zoek+"="
	y1 = bron.search(x1)
	if (y1 == -1) {
		return "none"
	}
	else {	y1 = y1 + x1.length
		bron = bron.substr(y1)
		y1 = bron.search("<xsl:text disable-output-escaping="yes">&amp;</xsl:text>")
		if (y1 == -1) {
			return bron
		}
		else {	return bron.substr(0, y1)
		}
	}
}
				function outline() {
					handleRecursiveSignClassDown('http://www.xs4all.nl/~pboot/p01/p01rdfs.rdf#ImgFrag','showGreenAll')
				}
				function loadFunction() {
					s = getVar (window.location.href,'sign')
					if (s != 'none') {
						functioncall = 'functionShow' + s.substr(s.search('#') + 1) + '()'
						eval(functioncall)
						signDescWrite(s)
					}
					s = getVar (window.location.href,'class')
					if (s != 'none') {
						selectSignEmpty()
						s = 'http://www.xs4all.nl/~pboot/p01/p01rdfs.rdf#' + s
						document.formSelect.selectClass.value = s
						handleRecursiveSignClassDown(s,'createSignOption')
					}
					outline()
				}
				function handleSignClass(sclass,saction) {
					switch (saction) {
						case 'createSignOption' :
							handleSignFromClass(sclass,'createOption');
							break;
						case 'showSignAll' :
							handleSignFromClass(sclass,'showSign');
							break;
						case 'showGreenAll' :
							handleSignFromClass(sclass,'showGreen');
							break;
						default : alert ('handleSignClass unforeseen action ' + saction)
					}
				}
				function handleRecursiveSignClassDown(sclass,saction) {
					handleSignClass(sclass,saction)
					<xsl:for-each select="document('rdfstriple.xml')//statement[
						predicate/text() = 'http://www.w3.org/2000/01/rdf-schema#subClassOf' and
						object[@type='resource'] ]">
						if ('<xsl:value-of select="object/text()"/>' == sclass) {
							handleRecursiveSignClassDown('<xsl:value-of select="subject/text()"/>',saction)
						}
					</xsl:for-each>
				}
				function handleSignFromClass(sclass,saction) {
					<xsl:for-each select="document('rdftriple.xml')//statement[
						predicate/text() = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' and
						object[@type='resource'] ]">
						if ('<xsl:value-of select="object/text()"/>' == sclass) {
							switch (saction) {
								case 'createOption' :
									createOption('<xsl:value-of select="subject/text()"/>','<xsl:value-of select="pb:local-part(object/text())"/>, <xsl:value-of select="pb:local-part(subject/text())"/>')
									break;
								case 'showSign' :
									functioncall = 'functionShow' + '<xsl:value-of select="pb:local-part(subject/text())"/>' + '()'
									eval(functioncall)
									break;
								case 'showGreen' :
									functioncall = "showSVG('" + '<xsl:value-of select="pb:local-part(subject/text())"/>' + "','green')"
									eval(functioncall)
									break;
								default : alert ('handleSignFromClass unforeseen action ' + saction)
							}
						}
					</xsl:for-each>
				}
				function createOption(ovalue,otext) {
			 		o = document.getElementById('selectSign')
					e2 = document.createElement('option')
					e2.setAttribute('value',ovalue)
					e3 = o.appendChild(e2)
					e4 = document.createTextNode(otext)
					e5 = e2.appendChild(e4)
				}
				function selectClassList() {
					selectSignEmpty()
					sclass = document.formSelect.selectClass.value
					handleRecursiveSignClassDown(sclass,'createSignOption')
				}
				function selectClassShowAll() {
					removeAll()
					sclass = document.formSelect.selectClass.value
					handleRecursiveSignClassDown(sclass,'showSignAll')
				}
				function selectClassDiagram() {
					sclass = document.formSelect.selectClass.value
					window.location = 'classmodel.html?showclass=' + sclass.substr(sclass.search('#') + 1)
				}
				function signDesc() {
			 		o = document.getElementById('descdiv')
					while ( o.firstChild != null) {
				 		o2 = o.firstChild
				 		o.removeChild(o2)
					}
					ssign = document.formSelect.selectSign.value
					if (ssign != 'niente' <xsl:text disable-output-escaping="yes">&amp;&amp;</xsl:text> ssign != '' ) {
						signDescWrite(ssign)
					}
				}
				function signDescWrite(ssign) {
					filename = "outxml\\" + ssign.substr(ssign.search('#') + 1) +  ".html"
			 		o = document.getElementById('descdiv')
					i = document.createElement('iframe')
					i.setAttribute('src',filename)
					i.setAttribute('marginheight','1px')
					i.setAttribute('marginwidth','1px')
					i.setAttribute('height','400px')
					i.setAttribute('width','250px')
					i.setAttribute('frameborder','0')
					o.appendChild(i)
				}
				function selectSignShow() {
					removeAll()
					signDesc()
					ssign = document.formSelect.selectSign.value
					if (ssign != 'niente' <xsl:text disable-output-escaping="yes">&amp;&amp;</xsl:text> ssign != '' ) {
						functioncall = 'functionShow' + ssign.substr(ssign.search('#') + 1) + '()'
						eval(functioncall)
					}
				}
				function selectSignDiagram() {
					ssign = document.formSelect.selectSign.value
					if (ssign != 'niente' <xsl:text disable-output-escaping="yes">&amp;&amp;</xsl:text> ssign != '' ) {
						window.location = 'signmodel.html?show=' + ssign.substr(ssign.search('#') + 1)
					}
				}
				function selectSignEmpty() {
			 		o = document.getElementById('selectSign')
					while ( o.firstChild != null) {
				 		o2 = o.firstChild
				 		o.removeChild(o2)
					}
				}
				function removeAll() {
					clearspan()
			 		o = document.getElementById('pos')
					while ( o.firstChild != null) {
				 		o2 = o.firstChild
				 		o.removeChild(o2)
					}
				}
				function clearspan() {
					l = document.getElementsByTagName('span')
					n = l.length
					i = 0
					while (n != i) {
						l.item(i).style.backgroundColor="transparent"
						i = i + 1
					}
					l2 = document.getElementById('pv161516')
				}
				function showSVG(obj,col) {
			 		o = document.getElementById(obj)
					if ( o == null) {
						e2 = document.createElement('div')
						e2.setAttribute('id',obj)
				 		e3 = document.getElementById('pos').appendChild(e2)
						document.getElementById(obj).style.position="absolute"
						e1 = document.createElement('embed')
						e1.setAttribute('wmode','transparent')
						e1.setAttribute('width','342')
						e1.setAttribute('height','500')
						e1.setAttribute('type','image/svg+xml')
						e1.setAttribute('src',obj + col + '.svg')
			 			p = document.getElementById(obj).appendChild(e1)
					}
				}
			<xsl:apply-templates select="document('rdftriple.xml')//statement[
					predicate/text() = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type']" mode="function"/>
			</script>
		</head>
		<body id="body" onLoad="loadFunction()">
			<xsl:apply-templates select="/TEI.2/text/group/text//div[@type='pictura']" mode="pictura"/>
			<table width="395px" margin="0" padding="0">
				<tr>
					<td>
						<xsl:apply-templates select="/TEI.2/text/group/text"/>
					</td>
				</tr>
			</table>
			<hr />
			<p>How does this work?</p>
			<p>You may want to read <a href="index.html">some background</a> to know what this is all about. 
			All source files are available from that page.</p>
			<p>The green brackets in the text, and the green outlines in the picture, show the 
			presence of encoded sign fragments. In the texts, placing the mouse over a left bracket will
			tell you something about the text fragment: it will give its internal identifier and tell in 
			what lowest-level signs the text fragment is used. In the image, alt texts will do something similar.</p>
			<p>Clicking the mouse on a right bracket, or somewhere in the picture, will take you to the sign 
			model page. You'll be shown a diagram of the current text or image fragment hyperlinked 
			to other signs and sign classes.</p>
			<p>In the selection lists in the right bottom corner you can select a sign class, and then either:</p>
			<ul>
				<li>list the corresponding sign instances in the sign selection list;</li>
				<li>highlight all instances in text and/or image (this will highlight the sign vehicles rather than 
				the signs themselves, of course);</li>
				<li>view a diagram of the sign definition.</li>
			</ul>
			<p>The sign selection list has similar functions.</p>
			<p>I am very much interested in <A HREF="mailto:pboot@xs4all.nl">your feedback</A>.</p>
			<hr />
			<!-- p>
				<a href="http://validator.w3.org/check/referer"><img
					src="http://www.w3.org/Icons/valid-xhtml10"
					alt="Valid XHTML 1.0!" height="31" width="88"/>
				</a>
			</p -->
			<div style="z-index:2;top:30px;left:400px;position:absolute" id="pos">
				<span style="display:none">a</span>
			</div>
			<div style="z-index:2;top:30px;left:750px;position:absolute" id="descdiv">
				<span style="display:none">a</span>
			</div>
		</body>
	</html>
</xsl:template>

<xsl:template match="bibl">
	<xsl:call-template name="haakjeopenen"/>
	<span id="{@id}"><xsl:apply-templates/></span>
	<xsl:call-template name="haakjesluiten"/>
	<br/>
</xsl:template>

<xsl:template match="cit">
	<xsl:call-template name="haakjeopenen"/>
	<span id="{@id}"><xsl:apply-templates/></span>
	<xsl:call-template name="haakjesluiten"/>
	<br/>
</xsl:template>

<xsl:template match="div">
	<div>
		<xsl:call-template name="haakjeopenen"/>
		<span id="{@id}"><xsl:apply-templates/></span>
		<xsl:call-template name="haakjesluiten"/>
	</div>
</xsl:template>

<xsl:template match="div[@type='pictura']"/>

<xsl:template match="div[@type='pictura']" mode="pictura">
	<div style="z-index:1;top:30px;left:400px;position:absolute">
		<xsl:apply-templates select="descendant::figure"/>
		<div>
			<a href="javascript:removeAll()">Remove highlights</a>
			<xsl:text> </xsl:text>
			<a href="javascript:outline()">Outline image fragments</a>
			<br/>
			<form name="formSelect">
				<table>
					<colgroup span="2" valign="top"/>
					<tr>
						<td>Select sign class...</td>
						<td>
							<select name="selectClass">
								<xsl:apply-templates select="document('rdfstriple.xml')//statement[
									predicate/text() = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' and
									object/text() = 'http://www.w3.org/2000/01/rdf-schema#Class'
									]/subject" mode="option">
									<xsl:sort select="pb:local-part(text())"/>
								</xsl:apply-templates>
							</select>
							<br/>
							<input type="button" name="selectClassButtonList" value="List" onclick="selectClassList()"/>
							<input type="button" name="selectClassButtonShowAll" value="Highlight instances" onclick="selectClassShowAll()"/>
							<input type="button" name="selectClassButtonDiagram" value="Diagram" onclick="selectClassDiagram()"/>
						</td>
					</tr>
					<tr>
						<td>Select sign...</td>
						<td>
							<select name="selectSign" width="30" onBlur="signDesc()">
								<option value="niente">&lt;empty&gt;</option>
							</select>
							<br/>
							<input type="button" name="selectSignButtonShow" value="Highlight" onclick="selectSignShow()"/>
							<input type="button" name="selectSignButtonDiagram" value="Diagram" onclick="selectSignDiagram()"/>
						</td>
					</tr>
				</table>
			</form>
		</div>
	</div>
</xsl:template>

<xsl:template match="statement" mode="function">
	<xsl:variable name="id" select="pb:local-part(subject/text())"/>
	function 
			<xsl:text>functionShow</xsl:text>
			<xsl:value-of select="$id"/>
			<xsl:text>() {</xsl:text>
			<xsl:choose>
  				<xsl:when test="object/text() = 'http://www.xs4all.nl/~pboot/p01/p01rdfs.rdf#TextFrag'">
					<xsl:variable name="file" select="pb:global-part(subject/text())"/>
					<xsl:message>file <xsl:value-of select="$file"/></xsl:message>
					<xsl:message>id <xsl:value-of select="$id"/></xsl:message>
					<xsl:apply-templates select="document($file)//*[@id=$id]" mode="show"/>
				</xsl:when>
				<xsl:when test="object/text() = 'http://www.xs4all.nl/~pboot/p01/p01rdfs.rdf#ImgFrag'">
					<xsl:text>showSVG('</xsl:text>
					<xsl:value-of select="$id"/>
					<xsl:text>','red')</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:for-each select="//statement[
						subject/text() = current()/subject/text() and
						not(predicate/text() = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type')
						and object[@type='resource']]">
<!-- <xsl:message><xsl:value-of select="subject/text()"/> <xsl:value-of select="predicate/text()"/> <xsl:value-of select="object/text()"/> </xsl:message> -->
						<xsl:for-each select="//statement[
							subject/text() = current()/object/text() and
							predicate/text() = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type']">
								functionShow<xsl:value-of select="pb:local-part(subject/text())"/>()
						</xsl:for-each>
					</xsl:for-each>
				</xsl:otherwise>
			</xsl:choose>
	}
</xsl:template>

<xsl:template match="*" mode="option">
	<option value="{text()}"><xsl:value-of select="pb:local-part(text())" /></option>
</xsl:template>

<xsl:template match="figure">
	<div>
		<map name="imgmap">
			<xsl:for-each select="//xptr">
				<area>
					<xsl:attribute name="alt">
						<xsl:text>Image fragment with id: </xsl:text>
						<xsl:value-of select="@id"/>
						<xsl:text>. </xsl:text>
						<xsl:text>Functions in next-level sign(s): </xsl:text>
					 	<xsl:variable name="res" select="pb:fullname(@id,'data')"/>
		 				<xsl:message><xsl:value-of select="$res" /></xsl:message>
					 	<xsl:for-each select="document($rdffile)//statement[object/text() = $res]">
		 					<xsl:value-of select="pb:local-part(subject/text())"/>
							<xsl:text> with meaning </xsl:text>
					 		<xsl:value-of select="//statement[
		 						subject/text() = current()/subject/text() and 
						 		predicate/text() = 'http://www.xs4all.nl/~pboot/p01/p01rdfs.rdf#SignMeaningProp'
						 		]/object/text()"/>
					 	</xsl:for-each>
					</xsl:attribute>
					<xsl:attribute name="href">
						<xsl:text>signmodel.html?show=</xsl:text>
						<xsl:value-of select="@id"/>
					</xsl:attribute>
					<xsl:attribute name="shape">
						<xsl:choose>
							<xsl:when test="contains(@from,'(2d)')">
								<xsl:text>rect</xsl:text>
							</xsl:when>
							<xsl:otherwise>
								<xsl:text>poly</xsl:text>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:attribute>
					<xsl:attribute name="coords">
						<xsl:choose>
							<xsl:when test="contains(@from,'(2d)')">
								<xsl:value-of select="substring-before(substring-after(@from,'space (2d) ('),') (')"/>
								<xsl:text>,</xsl:text>
								<xsl:value-of select="substring-before(substring-after(substring-after(@from,'space (2d) ('),') ('),')')"/>
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="substring-before(substring-after(@from,'space (html-polygon) ('),')')"/>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:attribute>
				</area>
			</xsl:for-each>
		</map>
		<img src="{unparsed-entity-uri(@entity)}" alt="pictura" usemap="#imgmap"/>
	</div>
</xsl:template>

<xsl:template match="l">
	<span id="{@id}"><xsl:apply-templates/></span><br/>
</xsl:template>

<xsl:template match="lg[parent::div]">
	<xsl:call-template name="haakjeopenen"/>
	<span id="{@id}"><xsl:apply-templates/></span>
	<xsl:call-template name="haakjesluiten"/>
</xsl:template>

<xsl:template match="p">
		<xsl:call-template name="haakjeopenen"/>
		<span id="{@id}"><xsl:apply-templates/></span>
		<xsl:call-template name="haakjesluiten"/>
		<br/>
</xsl:template>

<xsl:template match="quote">
	<xsl:call-template name="haakjeopenen"/>
	<span id="{@id}"><xsl:apply-templates/></span>
	<xsl:call-template name="haakjesluiten"/>
</xsl:template>

<xsl:template match="seg">
	<xsl:call-template name="haakjeopenen"/>
	<span id="{@id}" style="background-color:transparent"><xsl:apply-templates/></span>
	<xsl:call-template name="haakjesluiten"/>
</xsl:template>

<xsl:template match="text">
	<xsl:call-template name="haakjeopenen"/>
	<div id="{@id}"><xsl:apply-templates/></div>
	<xsl:call-template name="haakjesluiten"/>
</xsl:template>

<xsl:template match="ptr" mode="show">
	<xsl:message>ptr <xsl:value-of select="@id"/></xsl:message>
	<xsl:apply-templates select="id(@target)" mode="show"/>
</xsl:template>

<xsl:template match="seg|bibl|l|lg|div|p|cit|text|quote" mode="show">
  <xsl:text>document.getElementById('</xsl:text>
	<xsl:value-of select="@id"/>
	<xsl:text>').style.backgroundColor="red";</xsl:text>
</xsl:template>

<xsl:template name="haakjeopenen">
 	<xsl:variable name="id" select="@id"/>
	<xsl:if test="saxon:exists($textfrags, saxon:expression('@id=$id'))">
	 	<xsl:variable name="spanid" select="concat(@id,'lb')"/>
	 	<xsl:variable name="spanidh" select="concat(@id,'lbh')"/>
		<span style="color:green" id="{$spanid}">
			<xsl:attribute name="onmouseover">
				<xsl:text>document.getElementById('</xsl:text>
				<xsl:value-of select="$spanidh"/>
				<xsl:text>').style.display='block'</xsl:text>
			</xsl:attribute>
			<xsl:attribute name="onmouseout">
				<xsl:text>document.getElementById('</xsl:text>
				<xsl:value-of select="$spanidh"/>
				<xsl:text>').style.display='none'</xsl:text>
			</xsl:attribute>
			<xsl:text>[</xsl:text>
		</span>
		<span style="display:none;color:green" id="{$spanidh}">
		 	<!-- xsl:variable name="fid" select="//ptr[saxon:exists(id(@target), saxon:expression('@id=$id'))]/@id"/ -->
		 	<xsl:variable name="fid">
		 		<xsl:variable name="idstr" select="concat(' ',concat($id,' '))"/>
		 		<xsl:for-each select="//ptr">
			 		<xsl:variable name="targetstr" select="concat(' ',concat(@target,' '))"/>
			 		<xsl:if test="contains($targetstr,$idstr)">
			 			<xsl:value-of select="@id"/>
			 		</xsl:if>
		 		</xsl:for-each>
		 	</xsl:variable>
			<xsl:text>Text fragment with id: </xsl:text>
			<xsl:value-of select="$fid"/>
			<br/>
			<xsl:text>Functions in next-level sign(s):</xsl:text>
			<br/>
		 	<xsl:variable name="res" select="pb:fullname($fid,'data')"/>
		 	<xsl:message><xsl:value-of select="$res" /></xsl:message>
		 	<xsl:for-each select="document($rdffile)//statement[object/text() = $res]">
		 		<xsl:value-of select="pb:local-part(subject/text())"/>
				<xsl:text> with meaning </xsl:text>
		 		<xsl:value-of select="//statement[
		 			subject/text() = current()/subject/text() and 
			 		predicate/text() = 'http://www.xs4all.nl/~pboot/p01/p01rdfs.rdf#SignMeaningProp'
			 		]/object/text()"/>
		 	</xsl:for-each>
		</span>
	</xsl:if>
</xsl:template>

<xsl:template name="haakjesluiten">
  <xsl:variable name="id" select="@id"/>
	<xsl:if test="saxon:exists($textfrags, saxon:expression('@id=$id'))">
		<a style="color:green">
			<xsl:attribute name="href">
				<xsl:text>signmodel.html?show=</xsl:text>
		 		<xsl:variable name="idstr" select="concat(' ',concat($id,' '))"/>
		 		<xsl:for-each select="//ptr">
			 		<xsl:variable name="targetstr" select="concat(' ',concat(@target,' '))"/>
			 		<xsl:if test="contains($targetstr,$idstr)">
			 			<xsl:value-of select="@id"/>
			 		</xsl:if>
		 		</xsl:for-each>
			</xsl:attribute>
			<xsl:text>]</xsl:text>
		</a>
	</xsl:if>
</xsl:template>

</xsl:stylesheet>
