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