42bf1ed3dd3cad910b839ccefa93827cb4678f01
[koha.git] / koha-tmpl / opac-tmpl / prog / en / js / google-jackets.js
1 if (typeof KOHA == "undefined" || !KOHA) {
2     var KOHA = {};
3 }
4
5 /**
6  * A namespace for Google related functions.
7  */
8 KOHA.Google = {
9
10
11     /**
12      * Search all:
13      *    <div title="biblionumber" id="isbn" class="gbs-thumbnail"></div>
14      * and run a search with all collected isbns to Google Book Search.
15      * The result is asynchronously returned by Google and catched by
16      * gbsCallBack().
17      */
18     GetCoverFromIsbn: function() {
19         var bibkeys = [];
20         $(".gbs-thumbnail").each(function(i) {
21             bibkeys.push(this.id); // id=isbn
22         });
23         bibkeys = bibkeys.join(',');
24         var scriptElement = document.createElement("script");
25         scriptElement.setAttribute("id", "jsonScript");
26         scriptElement.setAttribute("src",
27             "http://books.google.com/books?bibkeys=" + escape(bibkeys) +
28             "&jscmd=viewapi&callback=KOHA.Google.gbsCallBack");
29         scriptElement.setAttribute("type", "text/javascript");
30         document.documentElement.firstChild.appendChild(scriptElement);
31
32     },
33
34     /**
35      * Add cover pages and links to Google detail in <div
36      */
37     gbsCallBack: function(booksInfo) {
38         for (id in booksInfo) {
39             var book = booksInfo[id];
40             $("#"+book.bib_key).each(function() {
41                 var a = document.createElement("a");
42                 a.href = book.info_url;
43                     var img = document.createElement("img");
44                                 if(typeof(book.thumbnail_url) != "undefined"){
45                         img.src = book.thumbnail_url;
46                             a.appendChild(img);
47                         $(this).append(a);
48                                 }
49             });
50         }
51     }
52 };