<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	version="1.0">
<xsl:output method="text" media-type="text/javascript"/>
	<xsl:param name="Total" select="ProductInfo/TotalResults" />
	<xsl:param name="keyword" select="ProductInfo/Request/Args/Arg[contains(@name,'Search')]/@value"/>
	<xsl:param name="mode" select="ProductInfo/Request/Args/Arg[@name='mode']/@value"/>
	<xsl:param name="offset" select="ProductInfo/Request/Args/Arg[@name='page']/@value"/>
	<xsl:param name="t" select="ProductInfo/Request/Args/Arg[@name = 't']/@value" />
<xsl:template match="/">
<xsl:variable name="query">
<xsl:value-of select="$mode"/>_<xsl:value-of select="$keyword"/>_<xsl:value-of select="$offset"/>
</xsl:variable>
AWSearch.results["<xsl:value-of select="$query"/>"] = {
 total : <xsl:choose>
  <xsl:when test="$Total != ''"><xsl:value-of select="$Total"/></xsl:when>
  <xsl:otherwise>0</xsl:otherwise>
 </xsl:choose>
 ,asid : "<xsl:value-of select="$t"/>"
 ,items:[
	<xsl:apply-templates select="ProductInfo/Details"/>
	"end"
 ]
};
if(AWSearch.callbacks["<xsl:value-of select="$query"/>"]){
 AWSearch.callbacks["<xsl:value-of select="$query"/>"](AWSearch.results["<xsl:value-of select="$query"/>"])
}
</xsl:template>
<!-- 商品検索結果の生成 -->
<xsl:template match="Details">
{
  asin  : "<xsl:value-of select="Asin"/>"
 ,title : "<xsl:value-of select="translate(ProductName,'&quot;&#x0A;','” ')"/>"
<!-- ,ImageUrl   : "<xsl:value-of select="ImageUrlMedium"/>"-->
 ,creator: <xsl:choose>
		<xsl:when test="Catalog = 'Music'">
			["<xsl:value-of select="Artists/Artist"/>"]
		</xsl:when>
		<xsl:when test="Catalog = 'Book'">
			[<xsl:apply-templates select="Authors"/>]
		</xsl:when>
		<xsl:when test="Catalog = 'DVD'">
			[<xsl:apply-templates select="Starring"/>]
		</xsl:when>
		<xsl:otherwise>
			[]
		</xsl:otherwise>
	</xsl:choose>
<!-- 製造業者 -->
<xsl:choose>
	<xsl:when test="Manufacturer">
	,Manufacturer: "<xsl:value-of select="Manufacturer"/>"
	</xsl:when>
	<xsl:otherwise></xsl:otherwise>
</xsl:choose>

<!-- 価格情報 -->
<xsl:call-template name="OurPrice">
	<xsl:with-param name="OurPrice" select="substring(OurPrice, 2)"/>
	<xsl:with-param name="ListPrice" select="substring(ListPrice, 2)"/>
	<xsl:with-param name="UsedPrice" select="substring(UsedPrice, 2)"/>
</xsl:call-template>

<!-- 発売日 -->
	<xsl:choose>
	<xsl:when test="ReleaseDate">
	,pubDate : "<xsl:value-of select="ReleaseDate"/>"
	</xsl:when>
	<xsl:otherwise>
	</xsl:otherwise>
</xsl:choose>

<!-- 売り上げランクがあれば表示 -->
<xsl:choose>
	<xsl:when test="SalesRank">
	,rank:<xsl:value-of select="translate(SalesRank/text(),',','')"/>
	</xsl:when>
	<xsl:otherwise></xsl:otherwise>
</xsl:choose>

<!-- ゲーム機種、メディア -->
<xsl:choose>
	<xsl:when test="Catalog = 'Video Games'">
	,Platform: "<xsl:value-of select="Platforms/Platform/text()"/>"
	</xsl:when>
	<xsl:otherwise>
	,Media : "<xsl:value-of select="Media/text()"/>"
	</xsl:otherwise>
</xsl:choose>
	<xsl:if test="Catalog = 'Music'">
		<xsl:apply-templates select="Features"/>
	</xsl:if>

<!-- 在庫状況 -->
<xsl:choose>
	<xsl:when test="Availability = 'このページは在庫状況に応じて更新されますので、購入をお考えの方は定期的にご覧ください。'">
		,isExist : false
	</xsl:when>
	<xsl:otherwise>
		,isExist : true
		,stat: "<xsl:value-of select="Availability"/>"
	</xsl:otherwise>
</xsl:choose>

<!-- アダルトフラグ -->
<xsl:if test="Features/Feature = 'アダルト'">
	,isAdult : true
</xsl:if>

<!-- 評価 -->
<xsl:if test="Reviews/AvgCustomerRating">
	,rate : <xsl:value-of select="Reviews/AvgCustomerRating/text()" />
</xsl:if>

<!-- レビュー -->
,reviews:[<xsl:apply-templates select="Reviews"/>]
},
</xsl:template>
<!-- /商品検索結果の生成 -->



<!-- 価格情報の生成 -->
<xsl:template name="OurPrice">
	<xsl:param name="OurPrice"  select="'1.00'"/>
	<xsl:param name="ListPrice" select="'1.00'"/>
	<xsl:param name="UsedPrice"/>
	<xsl:choose>
		<xsl:when test="$OurPrice != ''">
		,OurPrice:<xsl:value-of select="translate($OurPrice,',','')"/>
		</xsl:when>
		<xsl:otherwise>
		</xsl:otherwise>
	</xsl:choose>
	<!-- 定価があれば -->
	<xsl:choose>
		<xsl:when test="$ListPrice != ''">
		,ListPrice:<xsl:value-of select="translate($ListPrice,',','')"/>
		</xsl:when>
		<xsl:otherwise>
		</xsl:otherwise>
	</xsl:choose>

	<!-- 中古があれば -->
	<xsl:if test="$UsedPrice">
		,UsedPrice:<xsl:value-of select="translate($UsedPrice,',','')"/>
	</xsl:if>
</xsl:template>



<!-- 輸入品表示 -->
<xsl:template match="Features">
	<xsl:for-each select="Feature">
		<xsl:choose>
		<xsl:when test="text() = 'from US'">
		,fromUS: true
		</xsl:when>
		<xsl:when test="text() = 'from UK'">
		,fromUK: true
		</xsl:when>
		<xsl:when test="text() = 'Import'">
		,Import: true
		</xsl:when>
		</xsl:choose>
	</xsl:for-each>
</xsl:template>



<!-- 出演タレント、著者表示 -->
<xsl:template match="Starring | Authors">
	<xsl:for-each select="Actor | Author">
		<xsl:choose>
			<xsl:when test="not(position() = last())">
				"<xsl:value-of select="."/>"<xsl:text>,</xsl:text>
			</xsl:when>
			<xsl:otherwise>
				"<xsl:value-of select="."/>"
			</xsl:otherwise>
		</xsl:choose>
	</xsl:for-each>
	<xsl:if test="Actor">
	</xsl:if>
</xsl:template>

<!-- レビュータイトルの表示 -->
<xsl:template match="Reviews">
<xsl:for-each select="CustomerReview">
 {description:"<xsl:value-of select="Summary"/>",rate:<xsl:value-of select="Rating"/>
  ,comment : "<xsl:value-of select="translate(Comment,'&quot;','”')"/>"
 }<xsl:choose>
	<xsl:when test="not(position() = last())">,</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
