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