]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/rdetail.js
Internationalize the MFHD strings in the OPAC, using Dojo
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / rdetail.js
1 /* */
2
3
4 detachAllEvt('common', 'run');
5 attachEvt("common", "run", rdetailDraw);
6 attachEvt("rdetail", "recordDrawn", rdetailBuildStatusColumns);
7 attachEvt("rdetail", "recordDrawn", rdetailBuildInfoRows);
8 attachEvt("rdetail", "recordDrawn", rdetailGetPageIds);
9
10 var record = null;
11 var cp_statuses = null;
12 var recordsCache = [];
13
14 var copyRowParent = null;
15 var copyRow = null;
16 var statusRow = null;
17 var numStatuses = null;
18 var defaultCN;
19 var callnumberCache = {};
20 var rdetailLocalOnly = true;
21 var globalCNCache = {};
22 var localTOC;
23 var cachedRecords;
24 var _statusPositions = {};
25
26 var rdetailShowLocal = true;
27 var rdetailShowCopyLocation = true;
28 var googleBookPreview = true;
29 var displaySerialHoldings = true;
30 var opac_strings;
31
32 /* serials are currently the only use of Dojo strings in the OPAC */
33 if (displaySerialHoldings) {
34         dojo.requireLocalization("openils.opac", "opac");
35         opac_strings = dojo.i18n.getLocalization("openils.opac", "opac");
36 }
37
38 var nextContainerIndex;
39
40 function rdetailReload() {
41         var args = {};
42         args[PARAM_LOCATION] = getNewSearchLocation();
43         args[PARAM_DEPTH] = depthSelGetDepth();
44         goTo(buildOPACLink(args));
45 }
46
47 var nextRecord;
48 var prevRecord;
49
50 var rdetailPrev = null;
51 var rdetailNext = null;
52 var rdetailStart = null;
53 var rdetailEnd = null;
54
55
56
57 /* looks to see if we have a next and/or previous record in the
58    record cache, if so, set up the nav links */
59 function rdetailSetPaging(ids) {
60
61         cachedRecords = {};
62         cachedRecords.ids = ids;
63
64         for( var i = 0; i < cachedRecords.ids.length; i++ ) {
65                 var rec = cachedRecords.ids[i];
66                 if( rec == getRid() ) {
67                         if( i > 0 ) prevRecord = cachedRecords.ids[i-1];
68                         if( i < cachedRecords.ids.length - 1 )
69                                 nextRecord = cachedRecords.ids[i+1];
70                         break;
71                 }
72         }
73
74         $('np_offset').appendChild(text(i + 1));
75         $('np_count').appendChild(text(getHitCount()));
76
77         if(prevRecord) {
78                 unHideMe($('np_table'));
79                 unHideMe($('np_prev'));
80                 unHideMe($('np_start'));
81                 rdetailPrev = function() { _rdetailNav(prevRecord); };
82                 rdetailStart = function() { _rdetailNav(cachedRecords.ids[0]); };
83         }
84
85         if(nextRecord) {
86                 unHideMe($('np_table'));
87                 unHideMe($('np_next'));
88                 unHideMe($('np_end'));
89                 rdetailNext = function() { _rdetailNav(nextRecord); };
90                 rdetailEnd = function() { _rdetailNav(cachedRecords.ids[cachedRecords.ids.length-1]); };
91         }
92
93         runEvt('rdetail', 'nextPrevDrawn', i, cachedRecords.ids.length);
94 }
95
96
97 function _rdetailNav(id, offset) {
98         var args = {};
99         args[PARAM_RID] = id;
100         goTo(buildOPACLink(args));
101 }
102
103 function rdetailDraw() {
104
105         detachAllEvt('common','depthChanged');
106         detachAllEvt('common','locationUpdated');
107         attachEvt('common','depthChanged', rdetailReload);
108         attachEvt('common','locationUpdated', rdetailReload);
109         attachEvt('common','holdUpdated', rdetailReload);
110         attachEvt('common','holdUpdateCanceled', rdetailReload);
111
112         copyRowParent = G.ui.rdetail.cp_info_row.parentNode;
113         copyRow = copyRowParent.removeChild(G.ui.rdetail.cp_info_row);
114         statusRow = G.ui.rdetail.cp_status.parentNode;
115         statusRow.id = '__rdsrow';
116
117         G.ui.rdetail.cp_info_local.onclick = rdetailShowLocalCopies;
118         G.ui.rdetail.cp_info_all.onclick = rdetailShowAllCopies;
119
120         if(getLocation() == globalOrgTree.id())
121                 hideMe(G.ui.rdetail.cp_info_all);
122
123         var req = new Request(FETCH_RMODS, getRid());
124         req.callback(_rdetailDraw);
125         req.send();
126
127         if (displaySerialHoldings) {
128                 var req = new Request(FETCH_MFHD_SUMMARY, getRid());
129                 req.callback(_holdingsDraw);
130                 req.send();
131         }
132
133         detachAllEvt("result", "idsReceived");
134         G.evt.result.hitCountReceived = [];
135         G.evt.result.recordReceived = [];
136         G.evt.result.copyCountsReceived = [];
137         G.evt.result.allRecordsReceived = [];
138 }
139
140 function rdetailGetPageIds() {
141         attachEvt("result", "idsReceived", rdetailSetPaging );
142         resultFetchAllRecords = true;
143         rresultCollectIds(true);
144 }
145
146
147 function buildunAPISpan (span, type, id) {
148         var cgi = new CGI();
149         var d = new Date();
150
151         addCSSClass(span,'unapi-id');
152
153         span.setAttribute(
154                         'title', 'tag:' + cgi.server_name + ',' +
155                         d.getFullYear() + ':' + type + '/' + id
156                         );
157 }
158
159 function rdetailViewMarc(r,id) {
160         hideMe($('rdetail_extras_loading'));
161         $('rdetail_view_marc_box').innerHTML = r.getResultObject();
162
163         var div = elem('div', { "class" : 'hide_me' });
164         var span = div.appendChild( elem('abbr') );
165
166         buildunAPISpan( span, 'biblio-record_entry', record.doc_id() );
167
168         $('rdetail_view_marc_box').insertBefore(span, $('rdetail_view_marc_box').firstChild);
169 }
170
171
172 function rdetailShowLocalCopies() {
173         rdetailShowLocal = true;
174         rdetailBuildInfoRows();
175         hideMe(G.ui.rdetail.cp_info_local);
176         unHideMe(G.ui.rdetail.cp_info_all);
177         hideMe(G.ui.rdetail.cp_info_none); 
178 }
179
180 function rdetailShowAllCopies() {
181
182         rdetailShowLocal = false;
183         rdetailBuildInfoRows();
184         hideMe(G.ui.rdetail.cp_info_all);
185         unHideMe(G.ui.rdetail.cp_info_local);
186         hideMe(G.ui.rdetail.cp_info_none); 
187 }
188
189 function OpenMarcEditWindow(pcrud, rec) {
190         /*
191            To run in Firefox directly, must set signed.applets.codebase_principal_support
192            to true in about:config
193          */
194         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
195         win = window.open('/xul/server/cat/marcedit.xul'); // XXX version?
196         dojo.require('openils.PermaCrud');
197
198         win.xulG = {
199                 record : {marc : rec.marc()},
200                 save : {
201                         label: opac_strings.SAVE_MFHD_LABEL,
202                         func: function(xmlString) {
203                                 rec.marc(xmlString);
204                                 rec.ischanged(true);
205                                 pcrud.update(rec);
206                         },
207                 }
208         };
209 }
210
211 function loadMarcEditor(recId) {
212         var pcrud = new openils.PermaCrud({"authtoken": G.user.session});
213         var rec = pcrud.retrieve("sre", recId);
214         if (rec) {
215                 OpenMarcEditWindow(pcrud, rec);
216         }
217 }
218
219 /*
220  * This function could be written much more intelligently
221  * Limited brain power means that I'm brute-forcing it for now
222  */
223 function _holdingsDraw(h) {
224         holdings = h.getResultObject();
225         if (!holdings) { return null; }
226
227         dojo.forEach(holdings, _holdingsDrawMFHD);
228 }
229
230 function _holdingsDrawMFHD(holdings, entryNum) {
231         if (!orgIsMine(findOrgUnit(holdings.owning_lib()), findOrgUnit(getLocation()))) {
232                 return null;
233         }
234         var hh = holdings.holdings();
235         var hch = holdings.current_holdings();
236         var hs = holdings.supplements();
237         var hcs = holdings.current_supplements();
238         var hi = holdings.indexes();
239         var hci = holdings.current_indexes();
240         var ho = holdings.online();
241         var hm = holdings.missing();
242         var hinc = holdings.incomplete();
243
244         if (    hh.length == 0 && hch.length == 0 && hs.length == 0 &&
245                 hcs.length == 0 && hi.length == 0 && hci.length == 0 &&
246                 ho.length == 0 && hm.length == 0 && hinc.length == 0
247         ) {
248                 return null;
249         }
250
251         dojo.place("<table><caption id='mfhdHoldingsCaption' class='rdetail_header color_1'>" +
252                 opac_strings.HOLDINGS_TABLE_CAPTION + "</caption><tbody id='rdetail_holdings_tbody_" +
253                 entryNum + "'></tbody></table>", "rdetail_details_table", "after"
254         );
255         if (hh.length > 0) { _holdingsDrawMFHDEntry(entryNum, opac_strings.HOLDINGS, hh); }
256         if (hch.length > 0) { _holdingsDrawMFHDEntry(entryNum, opac_strings.CURRENT_HOLDINGS, hch); }
257         if (hs.length > 0) { _holdingsDrawMFHDEntry(entryNum, opac_strings.SUPPLEMENTS, hs); }
258         if (hcs.length > 0) { _holdingsDrawMFHDEntry(entryNum, opac_strings.CURRENT_SUPPLEMENTS, hcs); }
259         if (hi.length > 0) { _holdingsDrawMFHDEntry(entryNum, opac_strings.INDEXES, hi); }
260         if (hci.length > 0) { _holdingsDrawMFHDEntry(entryNum, opac_strings.CURRENT_INDEXES, hci); }
261         if (ho.length > 0) { _holdingsDrawMFHDEntry(entryNum, opac_strings.ONLINE_VOLUMES, ho); }
262         if (hm.length > 0) { _holdingsDrawMFHDEntry(entryNum, opac_strings.MISSING_VOLUMES, hm); }
263         if (hinc.length > 0) { _holdingsDrawMFHDEntry(entryNum, opac_strings.INCOMPLETE_VOLUMES, hm); }
264
265         if (isXUL()) {
266                 dojo.require('openils.Event');
267                 dojo.require('openils.PermaCrud');
268                 dojo.place("<span> - </span><a class='classic_link' href='javascript:loadMarcEditor(" +
269                         holdings.id() + ")'>" + opac_strings.EDIT_LABEL + "</a>", 
270                         "mfhdHoldingsCaption", "last"
271                 );
272         }
273 }
274
275 function _holdingsDrawMFHDEntry(entryNum, entryName, entry) {
276         var flatEntry = entry.toString().replace(/,/g, ', ');
277         dojo.place("<tr><td> </td><td nowrap='nowrap' class='rdetail_desc'>" + entryName + "</td><td class='rdetail_item'>" + flatEntry + "</td></tr>", "rdetail_holdings_tbody_" + entryNum, "last");
278 }
279
280 function _rdetailDraw(r) {
281         record = r.getResultObject();
282
283         runEvt('rdetail', 'recordRetrieved', record.doc_id());
284
285         G.ui.rdetail.title.appendChild(text(record.title()));
286         buildSearchLink(STYPE_AUTHOR, record.author(), G.ui.rdetail.author);
287         G.ui.rdetail.isbn.appendChild(text(cleanISBN(record.isbn())));
288         G.ui.rdetail.edition.appendChild(text(record.edition()));
289         G.ui.rdetail.pubdate.appendChild(text(record.pubdate()));
290         G.ui.rdetail.publisher.appendChild(text(record.publisher()));
291         $('rdetail_physical_desc').appendChild(text(record.physical_description()));
292         r = record.types_of_resource();
293         if(r) {
294                 G.ui.rdetail.tor.appendChild(text(r[0]));
295                 setResourcePic( G.ui.rdetail.tor_pic, r[0]);
296         }
297         G.ui.rdetail.abstr.appendChild(text(record.synopsis()));
298
299         try{
300                 if(record.isbn()) {
301                         if(ENABLE_ADDED_CONTENT_ATTRIB_LINKS) {
302                                 unHideMe($('rdetail.jacket_attrib_div'));
303                                 var href = $('rdetail.jacket_attrib_link').getAttribute('href') +cleanISBN(record.isbn());
304                                 $('rdetail.jacket_attrib_link').setAttribute('href', href);
305                         }
306                         rdetailCheckForGBPreview();
307
308                 } else {
309                         hideMe($("rdetail.jacket_attrib_div"));
310                         hideMe($("rdetail_img_link"));
311                 }
312         } catch(E) {}
313
314
315         // see if the record has any external links 
316         var links = record.online_loc();
317         for( var i = 0; links && links.length > 0 && i < links.length; i = i + 3 ) {
318                 var href = links[i];
319                 // avoid matching "HTTP: The Complete Reference"
320                 if( href.match(/https?:\/|ftps?:\/|mailto:/i) ) {
321                         unHideMe($('rdetail_online_row'));
322                         // MODS can contain a display label (used for the text of the link)
323                         // as well as a note about the URL; many legacy systems conflate the
324                         // two and generate MARC records that expect the note to be used as
325                         // the text of the link, with no display label; here's the canonical
326                         // format:
327                         //
328                         // 856 40 $uhttp://localhost$yDisplay label$zPublic note
329                         //
330                         // Note that the MARC21slim2MODS XSL concatenates $3 and $y together
331                         // (as $y was defined later in MARC21's life as the display label)
332                         var displayLabel = '' + links[i+1];
333                         var note = '' + links[i+2];
334                         if(!displayLabel || displayLabel.match(/https?:\/|ftps?:\/|mailto:/i)) {
335                                 if(!note || note.match(/https?:\/|ftps?:\/|mailto:/i)) {
336                                         displayLabel = href;
337                                 } else {
338                                         displayLabel = note;
339                                 }
340                         }
341                         $('rdetail_online').appendChild(elem('a', {href:href,'class':'classic_link'}, displayLabel));
342                         if (!note && note != displayLabel) {
343                                 $('rdetail_online').appendChild(elem('span', {'class':'url_note'}, ' - ' + note));
344                         }
345                         $('rdetail_online').appendChild(elem('br'));
346                 }
347         }
348
349         // Fill in our unAPI ID, if anyone cares
350         var abbrs = document.getElementsByTagName('abbr');
351         var span;
352         for (var i = 0; i < abbrs.length; i = i + 1) {
353                 if (abbrs[i].getAttribute('name') == 'unapi') {
354                         span = abbrs[i];
355                         break;
356                 }
357         }
358         buildunAPISpan( span, 'biblio-record_entry', record.doc_id() );
359
360         $('rdetail_place_hold').setAttribute(
361                         'href','javascript:holdsDrawEditor({record:"'+record.doc_id()+'",type:"T"});');
362
363         $('rdetail_img_link').setAttribute('href', buildISBNSrc(cleanISBN(record.isbn()), 'large'));
364         G.ui.rdetail.image.setAttribute("src", buildISBNSrc(cleanISBN(record.isbn())));
365         runEvt("rdetail", "recordDrawn");
366         recordsCache.push(record);
367
368         rdetailSetExtrasSelector();
369
370         var breq = new Request(FETCH_BRE, [getRid()]);
371         breq.callback( rdetailCheckDeleted );
372         breq.send();
373
374         resultBuildCaches( [ record ] );
375         resultDrawSubjects();
376         resultDrawSeries();
377
378         // grab added content 
379         acCollectData(cleanISBN(record.isbn()), rdetailhandleAC);
380 }
381
382
383
384 function rdetailCheckDeleted(r) {
385         var br = r.getResultObject()[0];
386         if( isTrue(br.deleted()) ) {
387                 hideMe($('rdetail_place_hold'));
388                 $('rdetail_more_actions_selector').disabled = true;
389                 unHideMe($('rdetail_deleted_exp'));
390         }
391 }
392
393 function rdetailSetExtrasSelector() {
394         if(!grabUser()) return;
395         unHideMe($('rdetail_more_actions'));
396
397         var req = new Request( 
398                         FETCH_CONTAINERS, G.user.session, G.user.id(), 'biblio', 'bookbag' );
399         req.callback(rdetailAddBookbags);
400         req.send();
401 }
402
403 function rdetailAddBookbags(r) {
404
405         var containers = r.getResultObject();
406         var selector = $('rdetail_more_actions_selector');
407         var found = false;
408         var index = 3;
409         doSelectorActions(selector);
410
411         for( var i = 0; i != containers.length; i++ ) {
412                 found = true;
413                 var container = containers[i];
414                 insertSelectorVal( selector, index++, container.name(), 
415                                 "container_" + container.id(), rdetailAddToBookbag,  1 );
416         }
417
418         nextContainerIndex = index;
419 }
420
421 var _actions = {};
422 function rdetailNewBookbag() {
423         var name = prompt($('rdetail_bb_new').innerHTML,"");
424         if(!name) return;
425
426         var id;
427         if( id = containerCreate( name ) ) {
428                 alert($('rdetail_bb_success').innerHTML);
429                 var selector = $('rdetail_more_actions_selector');
430                 insertSelectorVal( selector, nextContainerIndex++, name, 
431                                 "container_" + id, rdetailAddToBookbag, 1 );
432                 setSelector( selector, 'start' );
433         }
434 }
435
436
437 function rdetailAddToBookbag() {
438         var selector = $('rdetail_more_actions_selector');
439         var id = selector.options[selector.selectedIndex].value;
440         setSelector( selector, 'start' );
441
442         if( containerCreateItem( id.substring(10), record.doc_id() )) {
443                 alert($('rdetail_bb_item_success').innerHTML);
444         }
445 }
446
447
448 var rdetailMarcFetched = false;
449 function rdetailShowExtra(type, args) {
450
451         hideMe($('rdetail_copy_info_div'));
452         hideMe($('rdetail_reviews_div'));
453         hideMe($('rdetail_toc_div'));
454         hideMe($('rdetail_anotes_div'));
455         hideMe($('rdetail_excerpt_div'));
456         hideMe($('rdetail_preview_div'));
457         hideMe($('rdetail_marc_div'));
458         hideMe($('cn_browse'));
459         hideMe($('rdetail_cn_browse_div'));
460         hideMe($('rdetail_notes_div'));
461
462         removeCSSClass($('rdetail_copy_info_link'), 'rdetail_extras_selected');
463         removeCSSClass($('rdetail_viewcn_link'), 'rdetail_extras_selected');
464         removeCSSClass($('rdetail_reviews_link'), 'rdetail_extras_selected');
465         removeCSSClass($('rdetail_toc_link'), 'rdetail_extras_selected');
466         removeCSSClass($('rdetail_excerpt_link'), 'rdetail_extras_selected');
467         removeCSSClass($('rdetail_preview_link'), 'rdetail_extras_selected');
468         removeCSSClass($('rdetail_anotes_link'), 'rdetail_extras_selected');
469         removeCSSClass($('rdetail_annotation_link'), 'rdetail_extras_selected');
470         removeCSSClass($('rdetail_viewmarc_link'), 'rdetail_extras_selected');
471
472         switch(type) {
473
474                 case "copyinfo": 
475                         unHideMe($('rdetail_copy_info_div')); 
476                         addCSSClass($('rdetail_copy_info_link'), 'rdetail_extras_selected');
477                         break;
478
479                 case "reviews": 
480                         addCSSClass($('rdetail_reviews_link'), 'rdetail_extras_selected');
481                         unHideMe($('rdetail_reviews_div')); 
482                         break;
483
484                 case "excerpt": 
485                         addCSSClass($('rdetail_excerpt_link'), 'rdetail_extras_selected');
486                         unHideMe($('rdetail_excerpt_div'));
487                         break;
488
489                 case "preview": 
490                         addCSSClass($('rdetail_preview_link'), 'rdetail_extras_selected');
491                         unHideMe($('rdetail_preview_div'));
492                         rdetailDisplayGBPreview();
493                         break;
494
495                 case "anotes": 
496                         addCSSClass($('rdetail_anotes_link'), 'rdetail_extras_selected');
497                         unHideMe($('rdetail_anotes_div'));
498                         break;
499
500                 case "toc": 
501                         addCSSClass($('rdetail_toc_link'), 'rdetail_extras_selected');
502                         unHideMe($('rdetail_toc_div'));
503                         break;
504
505                 case "marc": 
506                         addCSSClass($('rdetail_viewmarc_link'), 'rdetail_extras_selected');
507                         unHideMe($('rdetail_marc_div')); 
508                         if(rdetailMarcFetched) return;
509                         unHideMe($('rdetail_extras_loading'));
510                         rdetailMarcFetched = true;
511                         var req = new Request( FETCH_MARC_HTML, record.doc_id() );
512                         req.callback(rdetailViewMarc); 
513                         req.send();
514                         break;
515
516                 case 'cn':
517                         addCSSClass($('rdetail_viewcn_link'), 'rdetail_extras_selected');
518                         unHideMe($('rdetail_cn_browse_div'));
519                         rdetailShowCNBrowse(defaultCN, getLocation(), null, true);
520                         break;
521
522         }
523 }
524
525 function rdetailVolumeDetails(args) {
526         var row = $(args.rowid);
527         var tbody = row.parentNode;
528         cpdBuild( tbody, row, record, args.cn, args.org, args.depth, args.copy_location );
529         return;
530 }
531
532 function rdetailBuildCNList() {
533
534         var select = $('cn_browse_selector');
535         var index = 0;
536         var arr = [];
537         for( var cn in callnumberCache ) arr.push( cn );
538         arr.sort();
539
540         if( arr.length == 0 ) {
541                 hideMe($('rdetail_cn_browse_select_div'));
542                 return;
543         }
544
545         for( var i in arr ) {
546                 var cn = arr[i];
547                 var opt = new Option(cn);
548                 select.options[index++] = opt;
549         }
550         select.onchange = rdetailGatherCN;
551 }
552
553 function rdetailGatherCN() {
554         var cn = getSelectorVal($('cn_browse_selector'));
555         rdetailShowCNBrowse( cn, getLocation(), getDepth(), true );
556         setSelector( $('cn_browse_selector'), cn );
557 }
558
559
560 function rdetailShowCNBrowse( cn, loc, depth, fromOnclick ) {
561
562         if(!cn) {
563                 unHideMe($('cn_browse_none'));
564                 hideMe($('rdetail_cn_browse_select_div'));
565                 return;
566         }
567
568         unHideMe($('rdetail_cn_browse_select_div'));
569         rdetailBuildCNList();
570         setSelector( $('cn_browse_selector'), cn );
571         hideMe($('rdetail_copy_info_div'));
572         hideMe($('rdetail_reviews_div'));
573         hideMe($('rdetail_toc_div'));
574         hideMe($('rdetail_marc_div'));
575         unHideMe($('rdetail_cn_browse_div'));
576         unHideMe($('cn_browse'));
577         if( !rdetailLocalOnly && ! fromOnclick ) depth = findOrgDepth(globalOrgTree);
578         cnBrowseGo(cn, loc, depth);
579 }
580
581 function rdetailhandleAC(data) {
582
583         if( data.reviews.html ) {
584                 $('rdetail_review_container').innerHTML = data.reviews.html;
585                 unHideMe($('rdetail_reviews_link'));
586         }
587
588         if( data.toc.html ) {
589                 $('rdetail_toc_div').innerHTML = data.toc.html;
590                 unHideMe($('rdetail_toc_link'));
591         }
592
593         if( data.excerpt.html ) {
594                 $('rdetail_excerpt_div').innerHTML = data.excerpt.html;
595                 unHideMe($('rdetail_excerpt_link'));
596         }
597
598         if( data.anotes.html ) {
599                 $('rdetail_anotes_div').innerHTML = data.anotes.html;
600                 unHideMe($('rdetail_anotes_link'));
601         }
602 }
603
604 function rdetailShowReviews(r) {
605         hideMe($('rdetail_extras_loading'));
606         var res = r.getResultObject();
607         var par = $('rdetail_reviews_div');
608         var template = par.removeChild($('rdetail_review_template'));
609         if( res && res.length > 0 ) {
610                 unHideMe($('rdetail_reviews_link'));
611                 for( var i = 0; i != res.length; i++ ) {
612                         var rev = res[i];       
613                         if( rev.text && rev.info ) {
614                                 var node = template.cloneNode(true);
615                                 $n(node, 'review_header').appendChild(text(rev.info));
616                                 $n(node, 'review_text').appendChild(text(rev.text));
617                                 par.appendChild(node);
618                         }
619                 }
620         }
621 }
622
623
624 function rdetailShowTOC(r) {
625         hideMe($('rdetail_extras_loading'));
626         var resp = r.getResultObject();
627         if(resp) {
628                 unHideMe($('rdetail_toc_link'));
629                 $('rdetail_toc_div').innerHTML = resp;
630         }
631 }
632
633 function rdetailBuildInfoRows() {
634         var req;
635         var method = FETCH_COPY_COUNTS_SUMMARY;
636         if (rdetailShowCopyLocation)
637                 method = FETCH_COPY_LOCATION_COUNTS_SUMMARY;
638
639         if( rdetailShowLocal ) 
640                 req = new Request(method, record.doc_id(), getLocation(), getDepth())
641         else
642                 req = new Request(method, record.doc_id());
643         req.callback(_rdetailBuildInfoRows);
644         req.send();
645 }
646
647 function _rdetailRows(node) {
648
649         if( rdetailShowLocal && getLocation() != globalOrgTree.id() ) {
650                 var loc = findOrgUnit(getLocation());
651                 if( node ) {
652                         if( !orgIsMine(node, loc) && !orgIsMine(loc,node) ) return;
653                 } else {
654                         for( var i = 0; i < globalOrgTree.children().length; i++ ) {
655                                 var org = findOrgUnit(globalOrgTree.children()[i]);
656                                 if( orgIsMine(org, loc) ) {
657                                         node = org;
658                                         break;
659                                 }
660                         }
661                 } 
662         }
663
664         if(!node && findOrgType(globalOrgTree.ou_type()).can_have_vols())
665                 node = globalOrgTree;
666
667
668         /* don't show hidden orgs */
669
670         if(node) {
671
672                 if(!isXUL() && !isTrue(node.opac_visible())) return;
673
674                 var row = copyRow.cloneNode(true);
675                 row.id = "cp_info_" + node.id();
676
677                 var libtd = findNodeByName( row, config.names.rdetail.lib_cell );
678                 var cntd  = findNodeByName( row, config.names.rdetail.cn_cell );
679                 var cpctd = findNodeByName( row, config.names.rdetail.cp_count_cell );
680                 var actions = $n(row, 'rdetail_actions_cell');
681
682                 var p = libtd.getElementsByTagName('a')[0];
683                 libtd.insertBefore(text(node.name()), p);
684                 libtd.setAttribute("style", "padding-left: " + ((findOrgDepth(node) - 1)  * 9) + "px;");
685
686                 if(!findOrgType(node.ou_type()).can_have_vols()) {
687
688                         row.removeChild(cntd);
689                         row.removeChild(cpctd);
690                         row.removeChild(actions);
691                         row.setAttribute('novols', '1');
692
693                         libtd.setAttribute("colspan", numStatuses + 3 );
694                         libtd.colSpan = numStatuses + 3;
695                         addCSSClass(row, 'copy_info_region_row');
696                 } 
697
698                 copyRowParent.appendChild(row);
699
700         } else { node = globalOrgTree; }
701
702         for( var c in node.children() ) 
703                 _rdetailRows(node.children()[c]);
704 }
705
706 function rdetailCNPrint(orgid, cn) {
707         var div = cpdBuildPrintWindow( record, orgid);
708         var template = div.removeChild($n(div, 'cnrow'));
709         var rowNode = $("cp_info_" + orgid);
710         cpdStylePopupWindow(div);
711         openWindow(div.innerHTML);
712 }
713
714 var localCNFound = false;
715 var ctr = 0;
716 function _rdetailBuildInfoRows(r) {
717
718         if (rdetailShowCopyLocation)
719                 unHideMe( $n( $('rdetail_copy_info_table'), 'rdetail_copylocation_header' ) );
720
721         removeChildren(copyRowParent);
722
723         _rdetailRows();
724
725         var summary = r.getResultObject();
726         if(!summary) return;
727
728         var found = false;
729         for( var i = 0; i < summary.length; i++ ) {
730
731                 var arr = summary[i];
732                 globalCNCache[arr[1]] = 1;
733                 var thisOrg = findOrgUnit(arr[0]);
734                 var rowNode = $("cp_info_" + thisOrg.id());
735                 if(!rowNode) continue;
736
737                 if(rowNode.getAttribute("used")) {
738
739                         if( rowNode.nextSibling ) {
740                                 sib = rowNode.nextSibling;
741                                 o ='cp_info_'+thisOrg.id()+'_';
742                                 /* push the new row on as the last row for this org unit */
743                                 while( sib && sib.id.match(o) ) {
744                                         sib = sib.nextSibling;
745                                 }
746                                 if(sib)
747                                         rowNode = copyRowParent.insertBefore(copyRow.cloneNode(true), sib);
748                                 else
749                                         rowNode = copyRowParent.appendChild(copyRow.cloneNode(true));
750                         } else {
751                                 rowNode = copyRowParent.appendChild(copyRow.cloneNode(true));
752                         }
753
754                         var n = findNodeByName( rowNode, config.names.rdetail.lib_cell );
755                         n.appendChild(text(thisOrg.name()));
756                         n.setAttribute("style", "padding-left: " + ((findOrgDepth(thisOrg) - 1)  * 9) + "px;");
757                         rowNode.id = "cp_info_" + thisOrg.id() + '_' + (++ctr); 
758
759                 } else {
760                         rowNode.setAttribute("used", "1");
761                 }
762
763                 var cpc_temp = rowNode.removeChild(
764                                 findNodeByName(rowNode, config.names.rdetail.cp_count_cell));
765
766                 var statuses = arr[2];
767                 var cl = '';
768                 if (rdetailShowCopyLocation) {
769                         cl = arr[2];
770                         statuses = arr[3];
771                 }
772
773
774                 rdetailApplyStatuses(rowNode, cpc_temp, statuses);
775
776                 var isLocal = false;
777                 if( orgIsMine( findOrgUnit(getLocation()), thisOrg ) ) { 
778                         found = true; 
779                         isLocal = true; 
780                         if(!localCNFound) {
781                                 localCNFound = true;
782                                 defaultCN = arr[1];
783                         }
784                 }
785
786                 //if(isLocal) unHideMe(rowNode);
787                 unHideMe(rowNode);
788
789                 rdetailSetPath( thisOrg, isLocal );
790                 rdetailBuildBrowseInfo( rowNode, arr[1], isLocal, thisOrg, cl );
791
792                 if( i == summary.length - 1 && !defaultCN) defaultCN = arr[1];
793         }
794
795         if(!found) unHideMe(G.ui.rdetail.cp_info_none);
796 }
797
798 function rdetailBuildBrowseInfo(row, cn, local, orgNode, cl) {
799
800         if(local) {
801                 var cache = callnumberCache[cn];
802                 if( cache ) cache.count++;
803                 else callnumberCache[cn] = { count : 1 };
804         }
805
806         var depth = getDepth();
807         if( !local ) depth = findOrgDepth(globalOrgTree);
808
809         $n(row, 'rdetail_callnumber_cell').appendChild(text(cn));
810
811         if (rdetailShowCopyLocation) {
812                 var cl_cell = $n(row, 'rdetail_copylocation_cell');
813                 cl_cell.appendChild(text(cl));
814                 unHideMe(cl_cell);
815         }
816
817         _debug('setting action clicks for cn ' + cn);
818
819         var dHref = 'javascript:rdetailVolumeDetails('+
820                         '{copy_location : "'+cl+'", rowid : "'+row.id+'", cn :"'+cn+'", depth:"'+depth+'", org:"'+orgNode.id()+'", local: '+local+'});';
821
822         var bHref = 'javascript:rdetailShowCNBrowse("' + cn + '", '+orgNode.id()+', "'+depth+'");'; 
823
824         unHideMe( $n(row, 'details') )
825                 $n(row, 'details').setAttribute('href', dHref);
826         unHideMe( $n(row, 'browse') )
827                 $n(row, 'browse').setAttribute('href', bHref);
828
829         if(isXUL()) {
830                 unHideMe($n(row, 'hold_div'));
831                 $n(row, 'hold').onclick = function() {
832                         var req = new Request(FETCH_VOLUME_BY_INFO, cn, record.doc_id(), orgNode.id());
833                         req.callback(
834                                         function(r) {
835                                         var vol = r.getResultObject();
836                                         holdsDrawEditor({type: 'V', volumeObject : vol});
837                                         }
838                                     );
839                         req.send();
840                 };
841         }
842 }
843
844 // sets the path to org as 'active' and displays the path if it's local 
845 function rdetailSetPath(org, local) {
846         if( findOrgDepth(org) == 0 ) return;
847         var row = $("cp_info_" + org.id());
848         row.setAttribute("hasinfo", "1");
849         unHideMe(row);
850         rdetailSetPath(findOrgUnit(org.parent_ou()), local);
851 }
852
853 //Append all the statuses for a given summary to the 
854 //copy summary table 
855 function rdetailApplyStatuses( row, template, statuses ) {
856         for( var j in _statusPositions ) {
857                 var stat = _statusPositions[j];
858                 var val = statuses[stat.id()];
859                 var nn = template.cloneNode(true);
860                 if(val) nn.appendChild(text(val));
861                 else nn.appendChild(text(0));
862                 row.appendChild(nn);
863         }
864 }
865
866 //Add one td (creating a new column) to the copy summary
867 //table for each opac_visible copy status
868 function rdetailBuildStatusColumns() {
869
870         rdetailGrabCopyStatuses();
871         var parent = statusRow;
872         var template = parent.removeChild(G.ui.rdetail.cp_status);
873
874         var i = 0;
875         for( i = 0; i < cp_statuses.length; i++ ) {
876
877                 var c = cp_statuses[i];
878                 if( c && isTrue(c.opac_visible()) ) {
879                         var name = c.name();
880                         _statusPositions[i] = c;
881                         var node = template.cloneNode(true);
882                         var data = findNodeByName( node, config.names.rdetail.cp_status);
883
884                         data.appendChild(text(name));
885                         parent.appendChild(node);
886                 }
887         }       
888
889         numStatuses = 0;
890         for(x in _statusPositions) numStatuses++; 
891 }
892
893 function rdetailGrabCopyStatuses() {
894         if(cp_statuses) return cp_statuses;
895         var req = new Request(FETCH_COPY_STATUSES);
896         req.send(true);
897         cp_statuses = req.result();
898         cp_statuses = cp_statuses.sort(_rdetailSortStatuses);
899 }
900
901 function _rdetailSortStatuses(a, b) {
902         return parseInt(a.id()) - parseInt(b.id());
903 }
904
905 /**
906  * Check for a Google Book preview 
907  */
908 function rdetailCheckForGBPreview() {
909         if (!googleBookPreview) return;
910         searchForGBPreview( cleanISBN(record.isbn()) );
911 }
912
913 /**
914  *
915  * @param {DOM object} query The form element containing the
916  *                     input parameters "isbns"
917  */
918 function searchForGBPreview( isbn ) {
919
920         // Delete any previous Google Booksearch JSON queries.
921         var GBPJsonScript = document.getElementById("GBPJsonScript");
922         if (GBPJsonScript) {
923                 GBPJsonScript.parentNode.removeChild(GBPJsonScript);
924         }
925
926         // Add a script element with the src as the user's Google Booksearch query. 
927         // JSON output is specified by including the alt=json-in-script argument
928         // and the callback function is also specified as a URI argument.
929         var GBPScriptElement = document.createElement("script");
930
931         GBPScriptElement.setAttribute("id", "GBPJsonScript");
932         GBPScriptElement.setAttribute("src",
933                         "http://books.google.com/books?bibkeys=" + 
934                         isbn + "&jscmd=viewapi&callback=GBPreviewCallback");
935         GBPScriptElement.setAttribute("type", "text/javascript");
936
937         // make the request to Google booksearch
938         document.documentElement.firstChild.appendChild(GBPScriptElement);
939 }
940
941 /**
942  * This function is the call-back function for the JSON scripts which 
943  * executes a Google book search response.
944  *
945  * XXX I18N of text needed
946  *
947  * @param {JSON} booksInfo is the JSON object pulled from the Google books service.
948  */
949 function GBPreviewCallback(GBPBookInfo) {
950         var GBPreviewDiv = document.getElementById("rdetail_preview_div");
951         var GBPBook;
952
953         for ( i in GBPBookInfo ) {
954                 GBPBook = GBPBookInfo[i];
955         }
956
957         if ( !GBPBook ) {
958                 return;
959         }
960
961         if ( GBPBook.preview != "noview" ) {
962                 if ( GBPBook.preview == 'full' ) {
963                         setText( $('rdetail_preview_link'), $('rdetail_preview_full_text').innerHTML );
964                         $('rdetail_preview_link_a').title = $('rdetail_preview_title').innerHTML;      
965                 }
966
967                 // Add a button below the book cover image to load the preview.
968                 GBPBadge = document.createElement( 'img' );
969                 GBPBadge.src = 'http://books.google.com/intl/en/googlebooks/images/gbs_preview_button1.gif';
970                 GBPBadge.title = $('rdetail_preview_badge').innerHTML;
971                 GBPBadge.style.border = 0;
972                 GBPBadgelink = document.createElement( 'a' );
973                 GBPBadgelink.href = 'javascript:rdetailShowExtra("preview");';
974                 GBPBadgelink.appendChild( GBPBadge );
975                 $('rdetail_image_cell').appendChild( GBPBadgelink );
976                 $('rdetail_preview_div').style.height = 600;
977
978                 /* Display the "Preview" tab in the Extras section */
979                 unHideMe( $('rdetail_preview_link' ) );
980         }
981 }
982
983 /**
984  *  This is called when the user clicks on the 'Preview' link.  We assume
985  *  a preview is available from Google if this link was made visible.
986  *
987  * XXX I18N of Google Book Preview language attribute needed
988  */
989 function rdetailDisplayGBPreview() {
990         unHideMe($('rdetail_extras_loading'));
991         GBPreviewPane = $('rdetail_preview_div');
992         if ( GBPreviewPane.getAttribute('loaded') == null ||
993                 GBPreviewPane.getAttribute('loaded') == "false" ) {
994                 google.load("books", "0", {"callback" : rdetailGBPViewerLoadCallback, "language": "en"} );
995                 GBPreviewPane.setAttribute('loaded', 'true');
996         }
997 }
998
999 function rdetailGBPViewerLoadCallback() {
1000         hideMe($('rdetail_extras_loading'));
1001         var GBPViewer = new google.books.DefaultViewer(document.getElementById('rdetail_preview_div'));
1002         GBPViewer.load('ISBN:' + cleanISBN(record.isbn()) );
1003
1004 }