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