Modify GoogleJacket display
[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         $("div [id^=gbs-thumbnail]").each(function(i) {
21             bibkeys.push($(this).attr("class")); // 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                                 if (typeof(book.thumbnail_url) != "undefined") {
44                         var img = document.createElement("img");
45                         img.src = book.thumbnail_url;
46                                         $(this).append(img);
47                     $(this).append(
48                         '<div style="margin-bottom:5px; margin-top:-5px;font-size:9px">' +
49                         '<a href="' + 
50                         book.info_url + 
51                         '">Source Google</a></div>' 
52                         );
53                                 } else {
54                                         var message = document.createElement("span");
55                                         $(message).attr("class","no-image");
56                                         $(message).html(NO_GOOGLE_JACKET);
57                                         $(this).append(message);
58                                 }
59             });
60         }
61     }
62 };