<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<xsl:template match="/">

<xsl:variable name="LayerId"><xsl:value-of select="/imagemap/@layerid"/></xsl:variable>

<!--
  - There can be "global" styles for popups and "local" styles for popups.
  - This builds the CSS Strings for the "global" styles.
  -->
<xsl:variable name="globalTitleStyle">
	<xsl:call-template name="getStyleString">
		<xsl:with-param name="attrList" select="/imagemap/style/title/attr"/>
		<xsl:with-param name="ListPosition" select="1"/>
	</xsl:call-template>
</xsl:variable>

<xsl:variable name="globalContentsStyle">
	<xsl:call-template name="getStyleString">
		<xsl:with-param name="attrList" select="/imagemap/style/contents/attr"/>
		<xsl:with-param name="ListPosition" select="1"/>
	</xsl:call-template>
</xsl:variable>

<xsl:variable name="globalParentStyle">
	<xsl:call-template name="getStyleString">
		<xsl:with-param name="attrList" select="/imagemap/style/parent/attr"/>
		<xsl:with-param name="ListPosition" select="1"/>
	</xsl:call-template>
</xsl:variable>

<!--
  - Generate the Image Map!
  -->
<parent>

<map name="{$LayerId}-Imagemap" id="{$LayerId}-Imagemap">
	<xsl:for-each select="//popup">
		<area shape="{./@shape}" coords="{./@coords}" id="{$LayerId}-Area-{position()}" index="{position()}" root="{$LayerId}"/>
	</xsl:for-each>
</map>

<!--
  - Generate the Popups!
  -->
<xsl:for-each select="//popup">
	<!-- Get the "local" styles for the title and contents div's -->
	<xsl:variable name="localTitleStyle">
		<xsl:call-template name="getStyleString">
			<xsl:with-param name="attrList" select="./style/title/attr"/>
			<xsl:with-param name="ListPosition" select="1"/>
		</xsl:call-template>
	</xsl:variable>
	<xsl:variable name="localContentsStyle">
		<xsl:call-template name="getStyleString">
			<xsl:with-param name="attrList" select="./style/contents/attr"/>
			<xsl:with-param name="ListPosition" select="1"/>
		</xsl:call-template>
	</xsl:variable>

	<!-- Render the popup -->
	<div class="popupParent" id="{$LayerId}-Popup-{position()}" style="{$globalParentStyle}">
		<a><img src="shared/images/remove.png" class="closeButton" relation="{$LayerId}-Popup-{position()}"/></a>

		<div class="popupTitle" style="{$globalTitleStyle}{$localTitleStyle}" id="{$LayerId}-Popup-{position()}-Title">
			<xsl:value-of select="./title"/>
		</div>
		<div class="popupContents" style="{$globalContentsStyle}{$localContentsStyle}" id="{$LayerId}-Popup-{position()}-Contents">
			<xsl:for-each select="./item">
				<b class="itemTitle"><xsl:value-of select="@title"/></b>
				<!-- $target will be either _blank or the target specified by the element -->
				<xsl:variable name="target">
					<xsl:call-template name="getTarget">
					<xsl:with-param name="targetString" select="./@target"/>
					</xsl:call-template>
				</xsl:variable>
				<xsl:choose>
					<xsl:when test="@type = 'zoomto' or @type = 'pan'">
						<a href="javascript:changePointScale({@x},{@y},2)"><xsl:value-of select="."/></a>
					</xsl:when>
					<xsl:when test="@type = 'internal'">
						<a href="javascript:{@command}"><xsl:value-of select="."/></a>
					</xsl:when>
					<xsl:when test="@type = 'image'">
						<xsl:choose>
							<xsl:when test="@href"><a target="{$target}" href="{@href}"><img width="{@width}" height="{@height}" src="{@src}"/></a></xsl:when>
							<xsl:otherwise><img width="{@width}" height="{@height}" src="{@src}"/></xsl:otherwise>
						</xsl:choose>
					</xsl:when>
					<xsl:otherwise>
						<xsl:choose>
							<xsl:when test="@href"><a target="{$target}" href="{@href}"><xsl:value-of select="."/></a></xsl:when>
							<xsl:otherwise><span class="itemValue"><xsl:value-of select="."/></span></xsl:otherwise>
						</xsl:choose>

					</xsl:otherwise>
				</xsl:choose>
				<br/>
			</xsl:for-each>
		</div>
	</div>

</xsl:for-each>
</parent>
</xsl:template>

<!--
  - This allows me to set a variable equal to either "_blank" or the @target value
  -->
<xsl:template name="getTarget">
	<xsl:param name="targetString"/>
	<xsl:choose>
		<xsl:when test="$targetString"><xsl:value-of select="$targetString"/></xsl:when>
		<xsl:otherwise>_blank</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--
  - Builds a CSS string based on the attr name/value pairs
  -->
<xsl:template name="getStyleString">
	<xsl:param name="attrList"/>
	<xsl:param name="ListPosition"/>
	<xsl:param name="BuildString"/>
	<xsl:choose>
	<xsl:when test="$ListPosition &lt;= count($attrList)">
		<xsl:call-template name="getStyleString">
			<xsl:with-param name="attrList" select="$attrList"/>
			<xsl:with-param name="ListPosition" select="$ListPosition+1"/>
			<xsl:with-param name="BuildString" select="concat($BuildString,$attrList[$ListPosition]/@name,':',$attrList[$ListPosition]/@value,';')"/>
		</xsl:call-template>
	</xsl:when>
	<xsl:otherwise>
		<xsl:value-of select="$BuildString"/>
	</xsl:otherwise>
	</xsl:choose>
</xsl:template>

</xsl:stylesheet>
