/**
 * This function is the call-back function for the JSON scripts which 
 * executes a Google book search response.
 *
 * @param {JSON} booksInfo is the JSON object pulled from the Google books service.
 */
function listEntries(booksInfo) {

  var mainDiv = document.createElement("div");
  
  for (i in booksInfo) {
    var book = booksInfo[i];
    //alert(book.preview);
    //alert(book.info_url);
    //alert(book.thumbnail_url);
    //alert(book.bib_key);

    // Clear any old data to prepare to display the Loading... message.
    var div = document.getElementById(book.bib_key);
    if (div.firstChild) div.removeChild(div.firstChild);

    // Create a DIV for each book
    var thumbnailDiv = document.createElement("div");
    //thumbnailDiv.className = "thumbnail";

    var title;
    if (book.preview == "partial") {
      title = "Eingeschränkte Vorschau über Google Buchsuche (in neuem Fenster)";
    } else if (book.preview == "full") {
      title = "Vollständige Ansicht über Google Buchsuche (in neuem Fenster)";
    } else if (book.preview == "noview") {
      title = "Weitere Infos via Google Buchsuche (in neuem Fenster)";
    } else {
      title = "Google Buchsuche (in neuem Fenster)";
    }

    // Add a link to each book's informtaion page
    var a = document.createElement("a");
    //a.href = book.info_url;
    a.href = "/cgi-bin/redirect.pl?url=" + encodeURI(book.info_url);
    a.target = "_blank";
    //a.innerHTML = book.bib_key ;
    a.innerHTML = '';

    // Display a thumbnail of the book's cover
    if (book.thumbnail_url) {
      var img = document.createElement("img");
      //img.src = book.thumbnail_url;    
      img.src = book.thumbnail_url.replace(/zoom=5/g, "zoom=1");
      img.style.border = "1px solid #999";
      img.title = title;
      a.appendChild(img);
    }

    // Display a Google-button 
    if (book.preview != "noview") {
      var br = document.createElement("br");
      a.appendChild(br);

      var imgButton = document.createElement("img");
      imgButton.src = '/USB/img/gbs_preview_button1.gif';
      imgButton.style.marginTop = '0.5em';
      imgButton.style.marginLeft = '1.5em';
      a.appendChild(imgButton);
    }

    thumbnailDiv.appendChild(a);

    // Alert the user that the book is not previewable
    //var p = document.createElement("p");
    //p.innerHTML = book.preview;
    //if (p.innerHTML == "noview"){
    //    p.style.fontWeight = "bold";
    //    p.style.color = "#f00";    
    //}
    //thumbnailDiv.appendChild(p);

    mainDiv.appendChild(thumbnailDiv);
  }
  
  if (div) div.appendChild(mainDiv);

}

/**
 * Customization of listEntries()
 * If a CONTENTdm-Image exists, do not show a book cover from the Google books service
 */
function listEntriesWithoutImage(booksInfo) {

  var mainDiv = document.createElement("div");
  
  for (i in booksInfo) {
    var book = booksInfo[i];

    // Clear any old data to prepare to display the Loading... message.
    var div = document.getElementById(book.bib_key);
    if (div.firstChild) div.removeChild(div.firstChild);

    // Create a DIV for each book
    var thumbnailDiv = document.createElement("div");

    var title;
    if (book.preview == "partial") {
      title = "Eingeschränkte Vorschau über Google Buchsuche (in neuem Fenster)";
    } else if (book.preview == "full") {
      title = "Vollständige Ansicht über Google Buchsuche (in neuem Fenster)";
    } else if (book.preview == "noview") {
      title = "Weitere Infos via Google Buchsuche (in neuem Fenster)";
    } else {
      title = "Google Buchsuche (in neuem Fenster)";
    }

    // Add a link to each book's informtaion page
    var a = document.createElement("a");
    //a.href = book.info_url;
    a.href = "/cgi-bin/redirect.pl?url=" + encodeURI(book.info_url);
    a.target = "_blank";
    //a.innerHTML = book.bib_key ;
    a.innerHTML = '';

    // Display a thumbnail of the book's cover
    //if (book.thumbnail_url) {
    //  var img = document.createElement("img");
    //  //img.src = book.thumbnail_url;    
    //  img.src = book.thumbnail_url.replace(/zoom=5/g, "zoom=1");
    //  img.style.border = "1px solid #999";
    //  img.title = title;
    //  a.appendChild(img);
    //}

    // Display a Google-button 
    if (book.preview != "noview") {
      var br = document.createElement("br");
      a.appendChild(br);

      var imgButton = document.createElement("img");
      imgButton.src = '/USB/img/gbs_preview_button1.gif';
      imgButton.style.marginTop = '0.5em';
      imgButton.style.marginLeft = '1.5em';
      imgButton.title = title;
      a.appendChild(imgButton);
      thumbnailDiv.appendChild(a);
    }

    mainDiv.appendChild(thumbnailDiv);
  }
  
  if (div) div.appendChild(mainDiv);

}

/**
 *
 * @param {DOM object} query The form element containing the
 *                     input parameters "isbns"
 */
function searchgbs(isbn, cdmUrl) {
  // Return if different books have the same ISBN [pk]
  multiple_isbn = isbn.match(/^(3453520130|3886800563|3518067443|3518065823|0810103249)$/);
  if (multiple_isbn) {
    return 0;
  }

  // Clear any old data to prepare to display the Loading... message.
  var div = document.getElementById(isbn);
  if (div.firstChild) div.removeChild(div.firstChild);

  // Show a "Loading..." indicator.
  var div = document.getElementById(isbn);
  var p = document.createElement('p');
  p.appendChild(document.createTextNode('Loading...'));
  //div.appendChild(p);

  // Delete any previous Google Booksearch JSON queries.
  var jsonScript = document.getElementById("jsonScript");
  if (jsonScript) {
    jsonScript.parentNode.removeChild(jsonScript);
  }
      
  // Add a script element with the src as the user's Google Booksearch query. 
  // JSON output is specified by including the alt=json-in-script argument
  // and the callback funtion is also specified as a URI argument.
  var scriptElement = document.createElement("script");
  scriptElement.setAttribute("id", "jsonScript");
  scriptElement.setAttribute("type", "text/javascript");
  if (cdmUrl) {
    scriptElement.setAttribute("src", "http://books.google.com/books?bibkeys=" + escape(isbn) + "&jscmd=viewapi&callback=listEntriesWithoutImage");
  } else {
    scriptElement.setAttribute("src", "http://books.google.com/books?bibkeys=" + escape(isbn) + "&jscmd=viewapi&callback=listEntries");
  }
  // make the request to Google booksearch
  document.documentElement.firstChild.appendChild(scriptElement);
}

