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