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