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