<?xml version="1.0" encoding="UTF-8" ?>
<Module>

	<ModulePrefs
		title="Find Popular Items on eBay"
		height="400"
		author="Nemo Chen"
		author_email="nemo_v_chen@hotmail.com"
		screenshot="http://pics.ebaystatic.com/aw/pics/logos/logoEbay_x45.gif"
		thumbnail="http://pics.ebaystatic.com/aw/pics/logos/logoEbay_x45.gif"
		description="Find Popular Items on eBay">

		<Require feature="dynamic-height" />
		<Require feature="views" />

	</ModulePrefs>
	
	<UserPref name="queryKeywords" display_name="QueryKeywords : " default_value="ipad" />
	<UserPref name="maxResults" display_name="Max Results : " default_value="3" />

	<Content type="html">
				<![CDATA[
					<style type="text/css">
						table {
							border-collapse:collapse;
							background:#EFF4FB repeat-x;
							border-left:1px solid #6678b1;
							border-right:1px solid #6678b1;
							border-bottom:1px solid #6678b1;
							border-top:1px solid #6678b1;
							font: normal 12px arial, verdana, helvetica, sans-serif;
							text-align: left;
						}
						td {
							border-bottom: 1px thin #333;
							color: #669;
							padding: 4px 6px;
						}
						table a {
							<!--color:navy;-->
							color:blue;
							text-decoration:none;
						}
						table a:link {}
						table a:visited {
							font-weight:normal;
							color:blue;
						}
						table a:hover {
							border-bottom: 1px dashed #bbb;
						}

					</style>
					
					<div id="form_div">
						<FORM NAME="myform" ACTION="" METHOD="GET">
							<table border="0" width="100%">
								<tr>
									<td width="20%">Search: </td>
									<td width="80%">
										<input name="queryKeywords" type="text" value="__UP_queryKeywords__">
										<input name="maxResults" type="hidden" value="__UP_maxResults__">
										<INPUT TYPE="submit" NAME="button" Value="Query" onClick="doFindPopularItemsRequestForm(this.form);return false;">
									</td>	
								</tr>
							</table>
						</FORM>
					</div>

					<div id="content_div"></div>

					<script type="text/javascript">
					
						var appId = 'eBay929a8-96bf-4ad8-a71c-94de77a7c9e';
						var responseFormat = 'JSON';
						var gMaxResults = 0;

						function doFindPopularItemsRequestForm(form) {
							var maxResults = form.maxResults.value; 
							var queryKeywords = form.queryKeywords.value; 
							doFindPopularItemsRequest(queryKeywords, maxResults);
						}
						
						function doDefaultFindPopularItemsRequest() {
							var maxResults;
							var queryKeywords;
							
							if ('__UP_maxResults__' != null) {
								maxResults = __UP_maxResults__;
							} else {
								maxResults = 3;
							}
							
							if ('__UP_queryKeywords__' != null) {
								queryKeywords = '__UP_queryKeywords__';
							} else {
								queryKeywords = 'ipad';
							}
							
							doFindPopularItemsRequest(queryKeywords, maxResults);
						}
						
						function doFindPopularItemsRequest(queryKeywords, maxResults) {					

							gMaxResults = maxResults;
							queryKeywords = escape(queryKeywords);
							maxResults = escape(maxResults);					
						
							// request
							var params = {};
							params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
							
							var url = 
								"http://open.api.ebay.com/shopping?callname=FindPopularItems" +
									   "&responseencoding=" + responseFormat + 
									   "&appid=" + appId +
									   "&siteid=0" +
									   "&version=627" +
									   "&MaxEntries=" + maxResults +
									   "&QueryKeywords=" + queryKeywords;
							
							gadgets.io.makeRequest(url, doFindPopularItemsResponse, params);
						};
						
						function doFindPopularItemsResponse(obj) {					
							
							var ack = obj.data.Ack;
							if ('Failure' == ack) {
								document.getElementById("content_div").innerHTML = "";
								adjustHeight();
								return;
							}
						
							var html = [];
							var items = obj.data.ItemArray.Item || [];
							
							if(items != null) {
								html.push('<table border="0" width="100%">');
								for (var i = 0; i < items.length; ++i)
								{
									var item = items[i];
									
									var currencySymbol = item.ConvertedCurrentPrice.CurrencyID;
									if (currencySymbol == 'USD') {
										currencySymbol = '$';
									}
									
									var listingType = item.ListingType;
									var bidCountLabel = 'Bid';
									
									if (listingType == 'StoresFixedPrice' || listingType == 'FixedPriceItem') {
										bidCountLabel = "Sold";
									}
									
									if (item != null)
									{
										html.push('<tr>');
										html.push('<td rowspan=5><img src=' + item.GalleryURL + '></td>');
										html.push('<td colspan=2><a href=' + item.ViewItemURLForNaturalSearch + ' target="_blank"><b>' + item.Title + '</b></a></td>');
										html.push('</tr>');	
									
										html.push('<tr>');
										html.push('<td>CurrentPrice:</td>');
										html.push('<td>' + currencySymbol + "&nbsp;" + item.ConvertedCurrentPrice.Value + ' </td>');
										html.push('</tr>');
										
										html.push('<tr>');
										html.push('<td>' + bidCountLabel + ':</td>');
										if (item.BidCount && item.BidCount != null) {
											html.push('<td>' + item.BidCount + ' </td>');
										} else {
											html.push('<td>&nbsp;</td>');
										}								
										html.push('</tr>');
										
										html.push('<tr>');
										html.push('<td>Watched:</td>');
										html.push('<td>' + item.WatchCount + ' </td>');
										html.push('</tr>');

										html.push('<tr>');
										html.push('<td>EndTime:</td>');
										html.push('<td>' + item.EndTime + ' </td>');
										html.push('</tr>');								
										
										html.push('<tr>');
										html.push('<td colspan=3><hr/></td>');
										html.push('</tr>');
									}
								}
								html.push('</table>');

							} else {
								html.push('No Result.<br/><br/>');
							}
							document.getElementById("content_div").innerHTML = html.join("");

							adjustHeight();
							
						};
						
						function adjustHeight() {
							if (gadgets && gadgets.window) {
								gadgets.window.adjustHeight();
							}
						}
						
						gadgets.util.registerOnLoadHandler(doDefaultFindPopularItemsRequest);
					</script>

				]]>

	</Content>

</Module>

