";
+ sb[sb.length] = "
";
+ sb[sb.length] = "";
+ sb[sb.length] = engineLabel;
+ sb[sb.length] = " | ";
+ sb[sb.length] = generatePostionHTML(feed);
+ sb[sb.length] = " |
";
+
+ // create the actual results to display, start with the containing div
+ sb[sb.length] = "
";
+ sb[sb.length] = generateResultsListHTML(feed);
+ sb[sb.length] = "
";
+
+ // create the paging controls
+ sb[sb.length] = "
";
+ sb[sb.length] = generatePagingHTML(engineId, feed);
+ sb[sb.length] = "
";
+
+ // close the containing div
+ sb[sb.length] = "
";
+
+ // create a div element to hold the results
+ var d = document.createElement("div");
+ d.innerHTML = sb.join("");
+
+ // return the div
+ return d;
+}
+
+/**
+ * Shows another page of the current search results for the
+ * given engineId
+ */
+function showPage(engineId, url)
+{
+ // execute the query and process the results
+ YAHOO.util.Connect.asyncRequest("GET", url,
+ {
+ success: processShowPageResults,
+ failure: handleSearchError,
+ argument: [engineId]
+ },
+ null);
+}
+
+/**
+ * Processes the search results and updates the postion, result list
+ * and paging controls.
+ */
+function processShowPageResults(ajaxResponse)
+{
+ try
+ {
+ // render the results from the Ajax response
+ var engineId = ajaxResponse.argument[0];
+ var feed = ajaxResponse.responseXML.documentElement;
+
+ // if the name of the feed element is "rss", get the channel child element
+ if (feed.tagName == "rss")
+ {
+ feed = getElementByTagName(feed, "channel");
+ }
+
+ // find the position div and update the count
+ var positionDiv = document.getElementById(engineId + _resultSetPositionId);
+ if (positionDiv != null)
+ {
+ positionDiv.innerHTML = generatePostionHTML(feed);
+ }
+
+ // append the results list to the results list div
+ var resultsListDiv = document.getElementById(engineId + _resultSetListId);
+ if (resultsListDiv != null)
+ {
+ resultsListDiv.innerHTML = generateResultsListHTML(feed);
+ }
+
+ // update the paging div with new urls
+ var pagingDiv = document.getElementById(engineId + _resultSetPagingId);
+ if (pagingDiv != null)
+ {
+ pagingDiv.innerHTML = generatePagingHTML(engineId, feed);
+ }
+ }
+ catch (e)
+ {
+ handleError(e);
+ }
+}
+
+/**
+ * Generates the HTML required to display the current position i.e. "x - y of z".
+ */
+function generatePostionHTML(feed)
+{
+ var totalResults = 0;
+ var pageSize = 5;
+ var startIndex = 0;
+
+ // extract position information from results
+ var elTotalResults = getElementByTagNameNS(feed, _OS_NS_URI, _OS_NS_PREFIX, "totalResults");
+ if (elTotalResults != null)
+ {
+ totalResults = getElementText(elTotalResults);
+ }
+
+ // if there are no results just return an empty string
+ if (totalResults == 0)
+ {
+ return "";
+ }
+
+ var elStartIndex = getElementByTagNameNS(feed, _OS_NS_URI, _OS_NS_PREFIX, "startIndex");
+ if (elStartIndex != null)
+ {
+ startIndex = getElementText(elStartIndex);
+ }
+
+ var elItemsPerPage = getElementByTagNameNS(feed, _OS_NS_URI, _OS_NS_PREFIX, "itemsPerPage");
+ if (elItemsPerPage != null)
+ {
+ pageSize = getElementText(elItemsPerPage);
+ }
+
+ // calculate the number of pages the results span
+ /*var noPages = Math.floor(totalResults / pageSize);
+ var remainder = totalResults % pageSize;
+ if (remainder != 0)
+ {
+ noPages++;
+ }*/
+
+ // calculate the endIndex for this set of results
+ var endIndex = (Number(startIndex) + Number(pageSize)) - 1;
+ if (endIndex > totalResults)
+ {
+ endIndex = totalResults;
+ }
+
+ // make sure the startIndex is correct
+ if (totalResults == 0)
+ {
+ startIndex = 0;
+ }
+
+ var sb = [];
+ sb[sb.length] = startIndex;
+ sb[sb.length] = " - ";
+ sb[sb.length] = endIndex;
+ sb[sb.length] = " of ";
+ sb[sb.length] = totalResults;
+
+ return sb.join("");
+}
+
+/**
+ * Generates the HTML to display the search results from the
+ * given feed.
+ */
+function generateResultsListHTML(feed)
+{
+ var isAtom = true;
+
+ // if the name of the feed element is "channel" this is an RSS feed
+ if (feed.tagName == "channel")
+ {
+ isAtom = false;
+ }
+
+ var results = null;
+ if (isAtom)
+ {
+ results = feed.getElementsByTagName("entry");
+ }
+ else
+ {
+ results = feed.getElementsByTagName("item");
+ }
+
+ if (results == null || results.length == 0)
+ {
+ return "