﻿/******************************************************************************* 
 *  Copyright 2007 Amazon Technologies, Inc.  
 *  Licensed under the Apache License, Version 2.0 (the "License"); 
 *  
 *  You may not use this file except in compliance with the License. 
 *  You may obtain a copy of the License at: http://aws.amazon.com/apache2.0
 *  This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
 *  CONDITIONS OF ANY KIND, either express or implied. See the License for the 
 *  specific language governing permissions and limitations under the License.
 * ***************************************************************************** 
 *    __  _    _  ___ 
 *   (  )( \/\/ )/ __)
 *   /__\ \    / \__ \
 *  (_)(_) \/\/  (___/
 * 
 *  API Version: 2007-07-16
 *  Generated: Thu Aug 02 10:15:38 PDT 2007 
 * 
 */
function gE(x){return document.getElementById(x);}
function ctn(x){ return document.createTextNode(x);}
function cel(x){ return document.createElement(x);}
function addEvent(obj,type,fn){
  if(obj.addEventListener){obj.addEventListener(type,fn,false);}
  else if(obj.attachEvent){
    obj["e"+type+fn] = fn;
    obj.attachEvent("on"+type,function(){obj["e"+type+fn]();});
  }
}
function adjustSalesRank(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
{ 
        if (isNaN(parseInt(num))) return "NaN";
  var tmpNum = num;
  var iSign = num < 0 ? -1 : 1;
  tmpNum *= Math.pow(10,decimalNum);
  tmpNum = Math.round(Math.abs(tmpNum));
  tmpNum /= Math.pow(10,decimalNum);
  tmpNum *= iSign;
  var tmpNumStr = new String(tmpNum);
  if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
    if (num > 0)
      tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
    else
      tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
  if (bolCommas && (num >= 1000 || num <= -1000)) {
    var iStart = tmpNumStr.indexOf(".");
    if (iStart < 0)
      iStart = tmpNumStr.length;
    iStart -= 3;
    while (iStart >= 1) {
      tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
      iStart -= 3;
    }   
  }
  if (bolParens && num < 0)
    tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";
  return tmpNumStr;
}
  function JSONscriptRequest(fullUrl) {
    this.fullUrl = fullUrl;
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    this.headLoc = document.getElementsByTagName("head").item(0);
    this.scriptId = 'azScriptId' + JSONscriptRequest.scriptCounter++;
  }
  JSONscriptRequest.scriptCounter = 1;
  JSONscriptRequest.prototype.buildScriptTag = function () {
    this.scriptObj = document.createElement("script");
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("src", this.fullUrl);
    this.scriptObj.setAttribute("id", this.scriptId);
  }
  JSONscriptRequest.prototype.removeScriptTag = function () {
    this.headLoc.removeChild(this.scriptObj);
  }
  JSONscriptRequest.prototype.addScriptTag = function () {
    this.headLoc.appendChild(this.scriptObj);
  }
  var amzJSONCallback = function(tmpData){
    for(i=0; i<tmpData.Items.length-1; i++){
      var tmpItem = tmpData.Items[i];
      var strInfo = "";
      if (tmpItem.averagerating && tmpItem.averagerating >= 1.5 && tmpItem.totalcustomerreviews) {
          var strReviewWord = "review" + (tmpItem.totalcustomerreviews > 1 ? "s" : "");
          var strHoverText = "Click to see " + tmpItem.totalcustomerreviews + " " + strReviewWord + 
                             ", which gave this " + tmpItem.averagerating.replace(/\.0$/,"") + " out of 5 stars";
          var strHRef = "http://www.amazon.com/gp/redirect.html?ie=UTF8&location=http%3A%2F%2Fwww.amazon.com%2Freview%2Fproduct%2F" + tmpItem.asin + "%2F&tag=" + strAmazonID + "&linkCode=";
          strInfo += "<a href='" + strHRef + "'><img src='http://firstbooklook.com/images/stars" + tmpItem.averagerating*10 + 
                     ".gif' border='0' width='55px' height='12px' style='padding: 0; margin: 0;' title='" + strHoverText + "'></a> <a href='" + 
                     strHRef + "' title='" + strHoverText + "'>(" + tmpItem.totalcustomerreviews + " " + strReviewWord + ")</a> ";
      }
      if (tmpItem.salesrank) strInfo += (strInfo.length ? "&nbsp;" : "") + " Amazon rank #" + adjustSalesRank(tmpItem.salesrank,0,true,false,true) + " ";
      var gEl = gE("id" + tmpItem.asin);
      if(gEl) gEl.innerHTML = strInfo;
      
    }
  }
function processing() {
    if (strAmazonQueries.length == 0) return;
        aObj = new JSONscriptRequest(strAmazonQueries.shift());
    aObj.buildScriptTag();
    aObj.addScriptTag();
    if (strAmazonQueries.length > 0) setTimeout("processing()",1000);
        
}
addEvent(window,"load",processing);

