]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/rdetail.js
Really revert these files. Argh.
[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_excerpt_div'));
353         hideMe($('rdetail_preview_div'));
354         hideMe($('rdetail_marc_div'));
355         hideMe($('cn_browse'));
356         hideMe($('rdetail_cn_browse_div'));
357         hideMe($('rdetail_notes_div'));
358
359         removeCSSClass($('rdetail_copy_info_link'), 'rdetail_extras_selected');
360         removeCSSClass($('rdetail_viewcn_link'), 'rdetail_extras_selected');
361         removeCSSClass($('rdetail_reviews_link'), 'rdetail_extras_selected');
362         removeCSSClass($('rdetail_toc_link'), 'rdetail_extras_selected');
363         removeCSSClass($('rdetail_excerpt_link'), 'rdetail_extras_selected');
364         removeCSSClass($('rdetail_preview_link'), 'rdetail_extras_selected');
365         removeCSSClass($('rdetail_anotes_link'), 'rdetail_extras_selected');
366         removeCSSClass($('rdetail_annotation_link'), 'rdetail_extras_selected');
367         removeCSSClass($('rdetail_viewmarc_link'), 'rdetail_extras_selected');
368
369         switch(type) {
370
371                 case "copyinfo": 
372                         unHideMe($('rdetail_copy_info_div')); 
373                         addCSSClass($('rdetail_copy_info_link'), 'rdetail_extras_selected');
374                         break;
375
376                 case "reviews": 
377                         addCSSClass($('rdetail_reviews_link'), 'rdetail_extras_selected');
378                         unHideMe($('rdetail_reviews_div')); 
379                         break;
380
381                 case "excerpt": 
382                         addCSSClass($('rdetail_excerpt_link'), 'rdetail_extras_selected');
383                         unHideMe($('rdetail_excerpt_div'));
384                         break;
385
386                 case "preview": 
387                         addCSSClass($('rdetail_preview_link'), 'rdetail_extras_selected');
388                         unHideMe($('rdetail_preview_div'));
389                         rdetailDisplayGBPreview();
390                         break;
391
392                 case "anotes": 
393                         addCSSClass($('rdetail_anotes_link'), 'rdetail_extras_selected');
394                         unHideMe($('rdetail_anotes_div'));
395                         break;
396
397                 case "toc": 
398                         addCSSClass($('rdetail_toc_link'), 'rdetail_extras_selected');
399                         unHideMe($('rdetail_toc_div'));
400                         break;
401
402                 case "marc": 
403                         addCSSClass($('rdetail_viewmarc_link'), 'rdetail_extras_selected');
404                         unHideMe($('rdetail_marc_div')); 
405                         if(rdetailMarcFetched) return;
406                         unHideMe($('rdetail_extras_loading'));
407                         rdetailMarcFetched = true;
408                         var req = new Request( FETCH_MARC_HTML, record.doc_id() );
409                         req.callback(rdetailViewMarc); 
410                         req.send();
411                         break;
412
413                 case 'cn':
414                         addCSSClass($('rdetail_viewcn_link'), 'rdetail_extras_selected');
415                         unHideMe($('rdetail_cn_browse_div'));
416                         rdetailShowCNBrowse(defaultCN, getLocation(), null, true);
417                         break;
418
419         }
420 }
421
422 function rdetailVolumeDetails(args) {
423         var row = $(args.rowid);
424         var tbody = row.parentNode;
425         cpdBuild( tbody, row, record, args.cn, args.org, args.depth, args.copy_location );
426         return;
427 }
428
429 function rdetailBuildCNList() {
430
431         var select = $('cn_browse_selector');
432         var index = 0;
433         var arr = [];
434         for( var cn in callnumberCache ) arr.push( cn );
435         arr.sort();
436
437         if( arr.length == 0 ) {
438                 hideMe($('rdetail_cn_browse_select_div'));
439                 return;
440         }
441
442         for( var i in arr ) {
443                 var cn = arr[i];
444                 var opt = new Option(cn);
445                 select.options[index++] = opt;
446         }
447         select.onchange = rdetailGatherCN;
448 }
449
450 function rdetailGatherCN() {
451         var cn = getSelectorVal($('cn_browse_selector'));
452         rdetailShowCNBrowse( cn, getLocation(), getDepth(), true );
453         setSelector( $('cn_browse_selector'), cn );
454 }
455
456
457 function rdetailShowCNBrowse( cn, loc, depth, fromOnclick ) {
458
459         if(!cn) {
460                 unHideMe($('cn_browse_none'));
461                 hideMe($('rdetail_cn_browse_select_div'));
462                 return;
463         }
464
465         unHideMe($('rdetail_cn_browse_select_div'));
466         rdetailBuildCNList();
467         setSelector( $('cn_browse_selector'), cn );
468         hideMe($('rdetail_copy_info_div'));
469         hideMe($('rdetail_reviews_div'));
470         hideMe($('rdetail_toc_div'));
471         hideMe($('rdetail_marc_div'));
472         unHideMe($('rdetail_cn_browse_div'));
473         unHideMe($('cn_browse'));
474         if( !rdetailLocalOnly && ! fromOnclick ) depth = findOrgDepth(globalOrgTree);
475         cnBrowseGo(cn, loc, depth);
476 }
477
478 function rdetailhandleAC(data) {
479
480         if( data.reviews.html ) {
481                 $('rdetail_review_container').innerHTML = data.reviews.html;
482                 unHideMe($('rdetail_reviews_link'));
483         }
484
485         if( data.toc.html ) {
486                 $('rdetail_toc_div').innerHTML = data.toc.html;
487                 unHideMe($('rdetail_toc_link'));
488         }
489
490         if( data.excerpt.html ) {
491                 $('rdetail_excerpt_div').innerHTML = data.excerpt.html;
492                 unHideMe($('rdetail_excerpt_link'));
493         }
494
495         if( data.anotes.html ) {
496                 $('rdetail_anotes_div').innerHTML = data.anotes.html;
497                 unHideMe($('rdetail_anotes_link'));
498         }
499 }
500
501 function rdetailShowReviews(r) {
502         hideMe($('rdetail_extras_loading'));
503         var res = r.getResultObject();
504         var par = $('rdetail_reviews_div');
505         var template = par.removeChild($('rdetail_review_template'));
506         if( res && res.length > 0 ) {
507                 unHideMe($('rdetail_reviews_link'));
508                 for( var i = 0; i != res.length; i++ ) {
509                         var rev = res[i];       
510                         if( rev.text && rev.info ) {
511                                 var node = template.cloneNode(true);
512                                 $n(node, 'review_header').appendChild(text(rev.info));
513                                 $n(node, 'review_text').appendChild(text(rev.text));
514                                 par.appendChild(node);
515                         }
516                 }
517         }
518 }
519
520
521 function rdetailShowTOC(r) {
522         hideMe($('rdetail_extras_loading'));
523         var resp = r.getResultObject();
524         if(resp) {
525                 unHideMe($('rdetail_toc_link'));
526                 $('rdetail_toc_div').innerHTML = resp;
527         }
528 }
529
530 function rdetailBuildInfoRows() {
531         var req;
532         var method = FETCH_COPY_COUNTS_SUMMARY;
533         if (rdetailShowCopyLocation)
534                 method = FETCH_COPY_LOCATION_COUNTS_SUMMARY;
535
536         if( rdetailShowLocal ) 
537                 req = new Request(method, record.doc_id(), getLocation(), getDepth())
538         else
539                 req = new Request(method, record.doc_id());
540         req.callback(_rdetailBuildInfoRows);
541         req.send();
542 }
543
544 function _rdetailRows(node) {
545
546         if( rdetailShowLocal && getLocation() != globalOrgTree.id() ) {
547                 var loc = findOrgUnit(getLocation());
548                 if( node ) {
549                         if( !orgIsMine(node, loc) ) return;
550                 } else {
551                         for( var i = 0; i < globalOrgTree.children().length; i++ ) {
552                                 var org = findOrgUnit(globalOrgTree.children()[i]);
553                                 if( orgIsMine(org, loc) ) {
554                                         node = org;
555                                         break;
556                                 }
557                         }
558                 } 
559         }
560
561         if(!node && findOrgType(globalOrgTree.ou_type()).can_have_vols())
562                 node = globalOrgTree;
563
564
565         /* don't show hidden orgs */
566
567         if(node) {
568
569                 if(!isXUL() && !isTrue(node.opac_visible())) return;
570
571                 var row = copyRow.cloneNode(true);
572                 row.id = "cp_info_" + node.id();
573
574                 var libtd = findNodeByName( row, config.names.rdetail.lib_cell );
575                 var cntd  = findNodeByName( row, config.names.rdetail.cn_cell );
576                 var cpctd = findNodeByName( row, config.names.rdetail.cp_count_cell );
577                 var actions = $n(row, 'rdetail_actions_cell');
578
579                 var p = libtd.getElementsByTagName('a')[0];
580                 libtd.insertBefore(text(node.name()), p);
581                 libtd.setAttribute("style", "padding-left: " + ((findOrgDepth(node) - 1)  * 9) + "px;");
582
583                 if(!findOrgType(node.ou_type()).can_have_vols()) {
584
585                         row.removeChild(cntd);
586                         row.removeChild(cpctd);
587                         row.removeChild(actions);
588                         row.setAttribute('novols', '1');
589
590                         libtd.setAttribute("colspan", numStatuses + 3 );
591                         libtd.colSpan = numStatuses + 3;
592                         addCSSClass(row, 'copy_info_region_row');
593                 } 
594
595                 copyRowParent.appendChild(row);
596
597         } else { node = globalOrgTree; }
598
599         for( var c in node.children() ) 
600                 _rdetailRows(node.children()[c]);
601 }
602
603 function rdetailCNPrint(orgid, cn) {
604         var div = cpdBuildPrintWindow( record, orgid);
605         var template = div.removeChild($n(div, 'cnrow'));
606         var rowNode = $("cp_info_" + orgid);
607         cpdStylePopupWindow(div);
608         openWindow(div.innerHTML);
609 }
610
611 var localCNFound = false;
612 var ctr = 0;
613 function _rdetailBuildInfoRows(r) {
614
615         if (rdetailShowCopyLocation)
616                 unHideMe( $n( $('rdetail_copy_info_table'), 'rdetail_copylocation_header' ) );
617
618         removeChildren(copyRowParent);
619
620         _rdetailRows();
621
622         var summary = r.getResultObject();
623         if(!summary) return;
624
625         var found = false;
626         for( var i = 0; i < summary.length; i++ ) {
627
628                 var arr = summary[i];
629                 globalCNCache[arr[1]] = 1;
630                 var thisOrg = findOrgUnit(arr[0]);
631                 var rowNode = $("cp_info_" + thisOrg.id());
632                 if(!rowNode) continue;
633
634                 if(rowNode.getAttribute("used")) {
635
636                         if( rowNode.nextSibling ) {
637                                 sib = rowNode.nextSibling;
638                                 o ='cp_info_'+thisOrg.id()+'_';
639                                 /* push the new row on as the last row for this org unit */
640                                 while( sib && sib.id.match(o) ) {
641                                         sib = sib.nextSibling;
642                                 }
643                                 if(sib)
644                                         rowNode = copyRowParent.insertBefore(copyRow.cloneNode(true), sib);
645                                 else
646                                         rowNode = copyRowParent.appendChild(copyRow.cloneNode(true));
647                         } else {
648                                 rowNode = copyRowParent.appendChild(copyRow.cloneNode(true));
649                         }
650
651                         var n = findNodeByName( rowNode, config.names.rdetail.lib_cell );
652                         n.appendChild(text(thisOrg.name()));
653                         n.setAttribute("style", "padding-left: " + ((findOrgDepth(thisOrg) - 1)  * 9) + "px;");
654                         rowNode.id = "cp_info_" + thisOrg.id() + '_' + (++ctr); 
655
656                 } else {
657                         rowNode.setAttribute("used", "1");
658                 }
659
660                 var cpc_temp = rowNode.removeChild(
661                                 findNodeByName(rowNode, config.names.rdetail.cp_count_cell));
662
663                 var statuses = arr[2];
664                 var cl = '';
665                 if (rdetailShowCopyLocation) {
666                         cl = arr[2];
667                         statuses = arr[3];
668                 }
669
670
671                 rdetailApplyStatuses(rowNode, cpc_temp, statuses);
672
673                 var isLocal = false;
674                 if( orgIsMine( findOrgUnit(getLocation()), thisOrg ) ) { 
675                         found = true; 
676                         isLocal = true; 
677                         if(!localCNFound) {
678                                 localCNFound = true;
679                                 defaultCN = arr[1];
680                         }
681                 }
682
683                 //if(isLocal) unHideMe(rowNode);
684                 unHideMe(rowNode);
685
686                 rdetailSetPath( thisOrg, isLocal );
687                 rdetailBuildBrowseInfo( rowNode, arr[1], isLocal, thisOrg, cl );
688
689                 if( i == summary.length - 1 && !defaultCN) defaultCN = arr[1];
690         }
691
692         if(!found) unHideMe(G.ui.rdetail.cp_info_none);
693 }
694
695 function rdetailBuildBrowseInfo(row, cn, local, orgNode, cl) {
696
697         if(local) {
698                 var cache = callnumberCache[cn];
699                 if( cache ) cache.count++;
700                 else callnumberCache[cn] = { count : 1 };
701         }
702
703         var depth = getDepth();
704         if( !local ) depth = findOrgDepth(globalOrgTree);
705
706         $n(row, 'rdetail_callnumber_cell').appendChild(text(cn));
707
708         if (rdetailShowCopyLocation) {
709                 var cl_cell = $n(row, 'rdetail_copylocation_cell');
710                 cl_cell.appendChild(text(cl));
711                 unHideMe(cl_cell);
712         }
713
714         _debug('setting action clicks for cn ' + cn);
715
716         var dHref = 'javascript:rdetailVolumeDetails('+
717                         '{copy_location : "'+cl+'", rowid : "'+row.id+'", cn :"'+cn+'", depth:"'+depth+'", org:"'+orgNode.id()+'", local: '+local+'});';
718
719         var bHref = 'javascript:rdetailShowCNBrowse("' + cn + '", '+orgNode.id()+', "'+depth+'");'; 
720
721         unHideMe( $n(row, 'details') )
722                 $n(row, 'details').setAttribute('href', dHref);
723         unHideMe( $n(row, 'browse') )
724                 $n(row, 'browse').setAttribute('href', bHref);
725
726         if(isXUL()) {
727                 unHideMe($n(row, 'hold_div'));
728                 $n(row, 'hold').onclick = function() {
729                         var req = new Request(FETCH_VOLUME_BY_INFO, cn, record.doc_id(), orgNode.id());
730                         req.callback(
731                                         function(r) {
732                                         var vol = r.getResultObject();
733                                         holdsDrawEditor({type: 'V', volumeObject : vol});
734                                         }
735                                     );
736                         req.send();
737                 };
738         }
739 }
740
741 // sets the path to org as 'active' and displays the path if it's local 
742 function rdetailSetPath(org, local) {
743         if( findOrgDepth(org) == 0 ) return;
744         var row = $("cp_info_" + org.id());
745         row.setAttribute("hasinfo", "1");
746         unHideMe(row);
747         rdetailSetPath(findOrgUnit(org.parent_ou()), local);
748 }
749
750 //Append all the statuses for a given summary to the 
751 //copy summary table 
752 function rdetailApplyStatuses( row, template, statuses ) {
753         for( var j in _statusPositions ) {
754                 var stat = _statusPositions[j];
755                 var val = statuses[stat.id()];
756                 var nn = template.cloneNode(true);
757                 if(val) nn.appendChild(text(val));
758                 else nn.appendChild(text(0));
759                 row.appendChild(nn);
760         }
761 }
762
763 //Add one td (creating a new column) to the copy summary
764 //table for each opac_visible copy status
765 function rdetailBuildStatusColumns() {
766
767         rdetailGrabCopyStatuses();
768         var parent = statusRow;
769         var template = parent.removeChild(G.ui.rdetail.cp_status);
770
771         var i = 0;
772         for( i = 0; i < cp_statuses.length; i++ ) {
773
774                 var c = cp_statuses[i];
775                 if( c && isTrue(c.opac_visible()) ) {
776                         var name = c.name();
777                         _statusPositions[i] = c;
778                         var node = template.cloneNode(true);
779                         var data = findNodeByName( node, config.names.rdetail.cp_status);
780
781                         data.appendChild(text(name));
782                         parent.appendChild(node);
783                 }
784         }       
785
786         numStatuses = 0;
787         for(x in _statusPositions) numStatuses++; 
788 }
789
790 function rdetailGrabCopyStatuses() {
791         if(cp_statuses) return cp_statuses;
792         var req = new Request(FETCH_COPY_STATUSES);
793         req.send(true);
794         cp_statuses = req.result();
795         cp_statuses = cp_statuses.sort(_rdetailSortStatuses);
796 }
797
798 function _rdetailSortStatuses(a, b) {
799         return parseInt(a.id()) - parseInt(b.id());
800 }
801
802 /**
803  * XXX Need to adopt a more typical approach to showing loading status
804  */
805 function rdetailCheckForGBPreview() {
806
807         if (!googleBookPreview) return;
808         var GBPp = document.createElement('p');
809         GBPp.appendChild( document.createTextNode('Loading... ' ) );
810         GBPp.id = 'loading';
811         $('rdetail_preview_div').appendChild(GBPp);
812         searchForGBPreview( cleanISBN(record.isbn()) );
813
814 }
815
816 /**
817  *
818  * @param {DOM object} query The form element containing the
819  *                     input parameters "isbns"
820  */
821 function searchForGBPreview( isbn ) {
822
823         // Delete any previous Google Booksearch JSON queries.
824         var GBPJsonScript = document.getElementById("GBPJsonScript");
825         if (GBPJsonScript) {
826                 GBPJsonScript.parentNode.removeChild(GBPJsonScript);
827         }
828
829         // Add a script element with the src as the user's Google Booksearch query. 
830         // JSON output is specified by including the alt=json-in-script argument
831         // and the callback function is also specified as a URI argument.
832         var GBPScriptElement = document.createElement("script");
833
834         GBPScriptElement.setAttribute("id", "GBPJsonScript");
835         GBPScriptElement.setAttribute("src",
836                         "http://books.google.com/books?bibkeys=" + 
837                         isbn + "&jscmd=viewapi&callback=GBPreviewCallback");
838         GBPScriptElement.setAttribute("type", "text/javascript");
839
840         // make the request to Google booksearch
841         document.documentElement.firstChild.appendChild(GBPScriptElement);
842 }
843
844 /**
845  * This function is the call-back function for the JSON scripts which 
846  * executes a Google book search response.
847  *
848  * XXX I18N of text needed
849  *
850  * @param {JSON} booksInfo is the JSON object pulled from the Google books service.
851  */
852 function GBPreviewCallback(GBPBookInfo) {
853         // Clear any old data to prepare to display the Loading... message.
854         var GBPreviewDiv = document.getElementById("rdetail_preview_div");
855         var GBPBook;
856
857         for ( i in GBPBookInfo ) {
858                 GBPBook = GBPBookInfo[i];
859         }
860
861         if ( !GBPBook ) {
862                 return;
863         }
864
865         if ( GBPBook.preview != "noview" ) {
866                 if ( GBPBook.preview == 'full' ) {
867                         setText( $('rdetail_preview_link'), 'Full Text' );
868                         $('rdetail_preview_link_a').title = 'See the full text of this book.';      
869                 }
870
871                 // Add a button below the book cover image to load the preview.
872                 GBPBadge = document.createElement( 'img' );
873                 GBPBadge.src = 'http://books.google.com/intl/en/googlebooks/images/gbs_preview_button1.gif';
874                 GBPBadge.title = 'Show a preview of this book from Google Book Search';
875                 GBPBadge.style.border = 0;
876                 GBPBadgelink = document.createElement( 'a' );
877                 GBPBadgelink.href = 'javascript:rdetailShowExtra("preview");';
878                 GBPBadgelink.appendChild( GBPBadge );
879                 $('rdetail_image_cell').appendChild( GBPBadgelink );
880
881                 unHideMe( $('rdetail_preview_link' ) );
882                 $('rdetail_preview_div').style.height = 600;
883         }
884 }
885
886 /**
887  *  This is called when the user clicks on the 'Preview' link.  We assume
888  *  a preview is available from Google if this link was made visible.
889  *
890  * XXX I18N of Google Book Preview language attribute needed
891  */
892 function rdetailDisplayGBPreview() {
893         GBPreviewPane = $('rdetail_preview_div');
894         if ( GBPreviewPane.getAttribute('loaded') == null ||
895                 GBPreviewPane.getAttribute('loaded') == "false" ) {
896                 google.load("books", "0", {"callback" : rdetailGBPViewerLoadCallback, "language": "en"} );
897                 GBPreviewPane.setAttribute('loaded', 'true');
898         }
899 }
900
901 function rdetailGBPViewerLoadCallback() {
902         var GBPViewer = new google.books.DefaultViewer(document.getElementById('rdetail_preview_div'));
903         GBPViewer.load('ISBN:' + cleanISBN(record.isbn()) );
904
905 }