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