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