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