]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/rdetail.js
Stop trampling on added content "Excerpt" tab and create a new "Preview" tab (which...
[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 var googleBookPreview = true;
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_preview_div'));
353         hideMe($('rdetail_marc_div'));
354         hideMe($('cn_browse'));
355         hideMe($('rdetail_cn_browse_div'));
356         hideMe($('rdetail_notes_div'));
357
358         removeCSSClass($('rdetail_copy_info_link'), 'rdetail_extras_selected');
359         removeCSSClass($('rdetail_viewcn_link'), 'rdetail_extras_selected');
360         removeCSSClass($('rdetail_reviews_link'), 'rdetail_extras_selected');
361         removeCSSClass($('rdetail_toc_link'), 'rdetail_extras_selected');
362         removeCSSClass($('rdetail_excerpt_link'), 'rdetail_extras_selected');
363         removeCSSClass($('rdetail_preview_link'), 'rdetail_extras_selected');
364         removeCSSClass($('rdetail_anotes_link'), 'rdetail_extras_selected');
365         removeCSSClass($('rdetail_annotation_link'), 'rdetail_extras_selected');
366         removeCSSClass($('rdetail_viewmarc_link'), 'rdetail_extras_selected');
367
368         switch(type) {
369
370                 case "copyinfo": 
371                         unHideMe($('rdetail_copy_info_div')); 
372                         addCSSClass($('rdetail_copy_info_link'), 'rdetail_extras_selected');
373                         break;
374
375                 case "reviews": 
376                         addCSSClass($('rdetail_reviews_link'), 'rdetail_extras_selected');
377                         unHideMe($('rdetail_reviews_div')); 
378                         break;
379
380                 case "excerpt": 
381                         addCSSClass($('rdetail_excerpt_link'), 'rdetail_extras_selected');
382                         unHideMe($('rdetail_excerpt_div'));
383                         break;
384
385                 case "preview": 
386                         addCSSClass($('rdetail_preview_link'), 'rdetail_extras_selected');
387                         unHideMe($('rdetail_preview_div'));
388                         rdetailDisplayPreview();
389                         break;
390
391                 case "anotes": 
392                         addCSSClass($('rdetail_anotes_link'), 'rdetail_extras_selected');
393                         unHideMe($('rdetail_anotes_div'));
394                         break;
395
396
397                 case "toc": 
398                         addCSSClass($('rdetail_toc_link'), 'rdetail_extras_selected');
399                         unHideMe($('rdetail_toc_div'));
400                         break;
401
402                 case "marc": 
403                         addCSSClass($('rdetail_viewmarc_link'), 'rdetail_extras_selected');
404                         unHideMe($('rdetail_marc_div')); 
405                         if(rdetailMarcFetched) return;
406                         unHideMe($('rdetail_extras_loading'));
407                         rdetailMarcFetched = true;
408                         var req = new Request( FETCH_MARC_HTML, record.doc_id() );
409                         req.callback(rdetailViewMarc); 
410                         req.send();
411                         break;
412
413                 case 'cn':
414                         addCSSClass($('rdetail_viewcn_link'), 'rdetail_extras_selected');
415                         unHideMe($('rdetail_cn_browse_div'));
416                         rdetailShowCNBrowse(defaultCN, getLocation(), null, true);
417                         break;
418
419         }
420 }
421
422 function rdetailVolumeDetails(args) {
423         var row = $(args.rowid);
424         var tbody = row.parentNode;
425         cpdBuild( tbody, row, record, args.cn, args.org, args.depth, args.copy_location );
426         return;
427 }
428
429
430 function rdetailBuildCNList() {
431
432         var select = $('cn_browse_selector');
433         var index = 0;
434         var arr = [];
435         for( var cn in callnumberCache ) arr.push( cn );
436         arr.sort();
437
438         if( arr.length == 0 ) {
439                 hideMe($('rdetail_cn_browse_select_div'));
440                 return;
441         }
442
443         for( var i in arr ) {
444                 var cn = arr[i];
445                 var opt = new Option(cn);
446                 select.options[index++] = opt;
447         }
448         select.onchange = rdetailGatherCN;
449 }
450
451 function rdetailGatherCN() {
452         var cn = getSelectorVal($('cn_browse_selector'));
453         rdetailShowCNBrowse( cn, getLocation(), getDepth(), true );
454         setSelector( $('cn_browse_selector'), cn );
455 }
456
457
458 function rdetailShowCNBrowse( cn, loc, depth, fromOnclick ) {
459
460         if(!cn) {
461                 unHideMe($('cn_browse_none'));
462                 hideMe($('rdetail_cn_browse_select_div'));
463                 return;
464         }
465                 
466         unHideMe($('rdetail_cn_browse_select_div'));
467         rdetailBuildCNList();
468         setSelector( $('cn_browse_selector'), cn );
469         hideMe($('rdetail_copy_info_div'));
470         hideMe($('rdetail_reviews_div'));
471         hideMe($('rdetail_toc_div'));
472         hideMe($('rdetail_marc_div'));
473         unHideMe($('rdetail_cn_browse_div'));
474         unHideMe($('cn_browse'));
475         if( !rdetailLocalOnly && ! fromOnclick ) depth = findOrgDepth(globalOrgTree);
476         cnBrowseGo(cn, loc, depth);
477 }
478
479 function rdetailhandleAC(data) {
480
481         if( data.reviews.html ) {
482                 $('rdetail_review_container').innerHTML = data.reviews.html;
483                 unHideMe($('rdetail_reviews_link'));
484         }
485
486         if( data.toc.html ) {
487                 $('rdetail_toc_div').innerHTML = data.toc.html;
488                 unHideMe($('rdetail_toc_link'));
489         }
490
491         if( data.excerpt.html ) {
492                 $('rdetail_excerpt_div').innerHTML = data.excerpt.html;
493                 unHideMe($('rdetail_excerpt_link'));
494         }
495
496         if( data.anotes.html ) {
497                 $('rdetail_anotes_div').innerHTML = data.anotes.html;
498                 unHideMe($('rdetail_anotes_link'));
499         }
500 }
501
502 function rdetailShowReviews(r) {
503         hideMe($('rdetail_extras_loading'));
504         var res = r.getResultObject();
505         var par = $('rdetail_reviews_div');
506         var template = par.removeChild($('rdetail_review_template'));
507         if( res && res.length > 0 ) {
508                 unHideMe($('rdetail_reviews_link'));
509                 for( var i = 0; i != res.length; i++ ) {
510                         var rev = res[i];       
511                         if( rev.text && rev.info ) {
512                                 var node = template.cloneNode(true);
513                                 $n(node, 'review_header').appendChild(text(rev.info));
514                                 $n(node, 'review_text').appendChild(text(rev.text));
515                                 par.appendChild(node);
516                         }
517                 }
518         }
519 }
520
521
522 function rdetailShowTOC(r) {
523         hideMe($('rdetail_extras_loading'));
524         var resp = r.getResultObject();
525         if(resp) {
526                 unHideMe($('rdetail_toc_link'));
527                 $('rdetail_toc_div').innerHTML = resp;
528         }
529 }
530
531
532 function rdetailBuildInfoRows() {
533         var req;
534     var method = FETCH_COPY_COUNTS_SUMMARY;
535     if (rdetailShowCopyLocation)
536         method = FETCH_COPY_LOCATION_COUNTS_SUMMARY;
537         
538         if( rdetailShowLocal ) 
539                 req = new Request(method, record.doc_id(), getLocation(), getDepth())
540         else
541                 req = new Request(method, record.doc_id());
542         req.callback(_rdetailBuildInfoRows);
543         req.send();
544 }
545
546 function _rdetailRows(node) {
547
548         if( rdetailShowLocal && getLocation() != globalOrgTree.id() ) {
549
550                 var loc = findOrgUnit(getLocation());
551
552                 if( node ) {
553                         if( !orgIsMine(loc, node) ) return;
554
555                 } else {
556
557                         for( var i = 0; i < globalOrgTree.children().length; i++ ) {
558                                 var org = findOrgUnit(globalOrgTree.children()[i]);
559                                 if( orgIsMine(org, loc) ) {
560                                         node = org;
561                                         break;
562                                 }
563                         }
564                 } 
565         }
566
567     if(!node && findOrgType(globalOrgTree.ou_type()).can_have_vols())
568         node = globalOrgTree;
569
570
571     /* don't show hidden orgs */
572
573         if(node) {
574
575         if(!isXUL() && !isTrue(node.opac_visible())) return;
576
577                 var row = copyRow.cloneNode(true);
578                 row.id = "cp_info_" + node.id();
579
580                 var libtd = findNodeByName( row, config.names.rdetail.lib_cell );
581                 var cntd  = findNodeByName( row, config.names.rdetail.cn_cell );
582                 var cpctd = findNodeByName( row, config.names.rdetail.cp_count_cell );
583                 var actions = $n(row, 'rdetail_actions_cell');
584         
585                 var p = libtd.getElementsByTagName('a')[0];
586                 libtd.insertBefore(text(node.name()), p);
587                 libtd.setAttribute("style", "padding-left: " + ((findOrgDepth(node) - 1)  * 9) + "px;");
588         
589                 if(!findOrgType(node.ou_type()).can_have_vols()) {
590
591                         row.removeChild(cntd);
592                         row.removeChild(cpctd);
593                         row.removeChild(actions);
594                         row.setAttribute('novols', '1');
595
596                         libtd.setAttribute("colspan", numStatuses + 3 );
597                         libtd.colSpan = numStatuses + 3;
598                         addCSSClass(row, 'copy_info_region_row');
599                 } 
600         
601                 copyRowParent.appendChild(row);
602
603         } else { node = globalOrgTree; }
604
605         for( var c in node.children() ) 
606                 _rdetailRows(node.children()[c]);
607 }
608
609 function rdetailCNPrint(orgid, cn) {
610         var div = cpdBuildPrintWindow( record, orgid);
611         var template = div.removeChild($n(div, 'cnrow'));
612         var rowNode = $("cp_info_" + orgid);
613         cpdStylePopupWindow(div);
614         openWindow(div.innerHTML);
615 }
616
617 var localCNFound = false;
618 var ctr = 0;
619 function _rdetailBuildInfoRows(r) {
620
621     if (rdetailShowCopyLocation)
622             unHideMe( $n( $('rdetail_copy_info_table'), 'rdetail_copylocation_header' ) );
623
624         removeChildren(copyRowParent);
625
626         _rdetailRows();
627
628         var summary = r.getResultObject();
629         if(!summary) return;
630
631         var found = false;
632         for( var i = 0; i < summary.length; i++ ) {
633
634                 var arr = summary[i];
635                 globalCNCache[arr[1]] = 1;
636                 var thisOrg = findOrgUnit(arr[0]);
637                 var rowNode = $("cp_info_" + thisOrg.id());
638                 if(!rowNode) continue;
639
640                 if(rowNode.getAttribute("used")) {
641
642                         if( rowNode.nextSibling ) {
643                 sib = rowNode.nextSibling;
644                 o ='cp_info_'+thisOrg.id()+'_';
645                 /* push the new row on as the last row for this org unit */
646                 while( sib && sib.id.match(o) ) {
647                     sib = sib.nextSibling;
648                 }
649                 if(sib)
650                                     rowNode = copyRowParent.insertBefore(copyRow.cloneNode(true), sib);
651                 else
652                     rowNode = copyRowParent.appendChild(copyRow.cloneNode(true));
653             } else {
654                                 rowNode = copyRowParent.appendChild(copyRow.cloneNode(true));
655             }
656
657                         var n = findNodeByName( rowNode, config.names.rdetail.lib_cell );
658                         n.appendChild(text(thisOrg.name()));
659                         n.setAttribute("style", "padding-left: " + ((findOrgDepth(thisOrg) - 1)  * 9) + "px;");
660                         rowNode.id = "cp_info_" + thisOrg.id() + '_' + (++ctr); 
661
662                 } else {
663                         rowNode.setAttribute("used", "1");
664                 }
665
666                 var cpc_temp = rowNode.removeChild(
667                         findNodeByName(rowNode, config.names.rdetail.cp_count_cell));
668
669                 var statuses = arr[2];
670                 var cl = '';
671         if (rdetailShowCopyLocation) {
672             cl = arr[2];
673             statuses = arr[3];
674         }
675
676
677                 rdetailApplyStatuses(rowNode, cpc_temp, statuses);
678
679                 var isLocal = false;
680                 if( orgIsMine( findOrgUnit(getLocation()), thisOrg ) ) { 
681                         found = true; 
682                         isLocal = true; 
683                         if(!localCNFound) {
684                                 localCNFound = true;
685                                 defaultCN = arr[1];
686                         }
687                 }
688
689                 //if(isLocal) unHideMe(rowNode);
690                 unHideMe(rowNode);
691
692                 rdetailSetPath( thisOrg, isLocal );
693                 rdetailBuildBrowseInfo( rowNode, arr[1], isLocal, thisOrg, cl );
694
695                 if( i == summary.length - 1 && !defaultCN) defaultCN = arr[1];
696         }
697
698         if(!found) unHideMe(G.ui.rdetail.cp_info_none);
699 }
700
701
702 function rdetailBuildBrowseInfo(row, cn, local, orgNode, cl) {
703
704         if(local) {
705                 var cache = callnumberCache[cn];
706                 if( cache ) cache.count++;
707                 else callnumberCache[cn] = { count : 1 };
708         }
709
710         var depth = getDepth();
711         if( !local ) depth = findOrgDepth(globalOrgTree);
712
713         $n(row, 'rdetail_callnumber_cell').appendChild(text(cn));
714
715         if (rdetailShowCopyLocation) {
716                 var cl_cell = $n(row, 'rdetail_copylocation_cell');
717                 cl_cell.appendChild(text(cl));
718
719                 unHideMe(cl_cell);
720         }
721
722         _debug('setting action clicks for cn ' + cn);
723
724         var dHref = 'javascript:rdetailVolumeDetails('+
725                 '{copy_location : "'+cl+'", rowid : "'+row.id+'", cn :"'+cn+'", depth:"'+depth+'", org:"'+orgNode.id()+'", local: '+local+'});';
726
727         var bHref = 'javascript:rdetailShowCNBrowse("' + cn + '", '+orgNode.id()+', "'+depth+'");'; 
728
729         unHideMe( $n(row, 'details') )
730         $n(row, 'details').setAttribute('href', dHref);
731         unHideMe( $n(row, 'browse') )
732         $n(row, 'browse').setAttribute('href', bHref);
733
734         if(isXUL()) {
735                 unHideMe($n(row, 'hold_div'));
736                 $n(row, 'hold').onclick = function() {
737                         var req = new Request(FETCH_VOLUME_BY_INFO, cn, record.doc_id(), orgNode.id());
738                         req.callback(
739                                 function(r) {
740                                         var vol = r.getResultObject();
741                                         holdsDrawEditor({type: 'V', volumeObject : vol});
742                                 }
743                         );
744                         req.send();
745                 };
746         }
747 }
748
749
750 // sets the path to org as 'active' and displays the path if it's local 
751 function rdetailSetPath(org, local) {
752         if( findOrgDepth(org) == 0 ) return;
753         var row = $("cp_info_" + org.id());
754         row.setAttribute("hasinfo", "1");
755         unHideMe(row);
756         rdetailSetPath(findOrgUnit(org.parent_ou()), local);
757 }
758
759
760
761
762 //Append all the statuses for a give summary to the 
763 //copy summary table 
764 function rdetailApplyStatuses( row, template, statuses ) {
765         for( var j in _statusPositions ) {
766                 var stat = _statusPositions[j];
767                 var val = statuses[stat.id()];
768                 var nn = template.cloneNode(true);
769                 if(val) nn.appendChild(text(val));
770                 else nn.appendChild(text(0));
771                 row.appendChild(nn);    
772         }
773 }
774
775
776 var _statusPositions = {};
777
778 //Add one td (creating a new column) to the copy summary
779 //table for each opac_visible copy status
780
781 function rdetailBuildStatusColumns() {
782
783         rdetailGrabCopyStatuses();
784         var parent = statusRow;
785         var template = parent.removeChild(G.ui.rdetail.cp_status);
786
787         var i = 0;
788         for( i = 0; i < cp_statuses.length; i++ ) {
789
790                 var c = cp_statuses[i];
791                 if( c && isTrue(c.opac_visible()) ) {
792                         var name = c.name();
793                         _statusPositions[i] = c;
794                         var node = template.cloneNode(true);
795                         var data = findNodeByName( node, config.names.rdetail.cp_status);
796         
797                         data.appendChild(text(name));
798                         parent.appendChild(node);
799                 }
800         }       
801
802         numStatuses = 0;
803         for(x in _statusPositions) numStatuses++; 
804 }
805
806 function rdetailGrabCopyStatuses() {
807         if(cp_statuses) return cp_statuses;
808    var req = new Request(FETCH_COPY_STATUSES);
809    req.send(true);
810         cp_statuses = req.result();
811         cp_statuses = cp_statuses.sort(_rdetailSortStatuses);
812 }
813
814 function _rdetailSortStatuses(a, b) {
815         return parseInt(a.id()) - parseInt(b.id());
816 }
817
818 function rdetailCheckForPreview() {
819   
820   if (!googleBookPreview) return;
821   var p = document.createElement('p');
822   p.appendChild( document.createTextNode('Loading... ' ) );
823   p.id = 'loading';
824   $('rdetail_preview_div').appendChild(p);
825   searchForPreview( cleanISBN(record.isbn()) );
826
827 }
828  
829 /**
830  *
831  * @param {DOM object} query The form element containing the
832  *                     input parameters "isbns"
833  */
834 function searchForPreview( isbn ) {
835
836   // Delete any previous Google Booksearch JSON queries.
837   var jsonScript = document.getElementById("jsonScript");
838   if (jsonScript) {
839     jsonScript.parentNode.removeChild(jsonScript);
840   }
841   // Add a script element with the src as the user's Google Booksearch query. 
842   // JSON output is specified by including the alt=json-in-script argument
843   // and the callback function is also specified as a URI argument.
844   var scriptElement = document.createElement("script");
845
846   scriptElement.setAttribute("id", "jsonScript");
847   scriptElement.setAttribute("src",
848       "http://books.google.com/books?bibkeys=" + 
849       isbn + "&jscmd=viewapi&callback=previewCallback");
850   scriptElement.setAttribute("type", "text/javascript");
851   // make the request to Google booksearch
852   document.documentElement.firstChild.appendChild(scriptElement);
853 }
854
855 /**
856  * This function is the call-back function for the JSON scripts which 
857  * executes a Google book search response.
858  *
859  * @param {JSON} booksInfo is the JSON object pulled from the Google books service.
860  */
861 function previewCallback(bookInfo) {
862   // Clear any old data to prepare to display the Loading... message.
863   var div = document.getElementById("rdetail_preview_div");
864   var book;
865   
866   for ( i in bookInfo ) {
867     book = bookInfo[i];
868   }
869
870   if ( !book ) {
871     return;
872   }
873
874   if ( book.preview != "noview" ) {
875     if ( book.preview == 'full' ) {
876       setText( $('rdetail_preview_link'), 'Full Text' );
877       $('rdetail_preview_link_a').title = 'See the full text of this book.';      
878     }
879
880     // Add a button below the book cover image to load the preview.
881     badge = document.createElement( 'img' );
882     badge.src = 'http://books.google.com/intl/en/googlebooks/images/gbs_preview_button1.gif';
883     badge.title = 'Show a preview of this book from Google Book Search';
884     badge.style.border = 0;
885     badgelink = document.createElement( 'a' );
886     badgelink.href = 'javascript:rdetailShowExtra("preview");';
887     badgelink.appendChild( badge );
888     $('rdetail_image_cell').appendChild( badgelink );
889
890     unHideMe( $('rdetail_preview_link' ) );
891     $('rdetail_preview_div').style.height = 600;
892   }
893 }
894
895 /**
896  *  This is called when the user clicks on the 'Excerpt' link.  We assume
897  *  a preview is available from Google if this link was made visible.
898  */
899 function rdetailDisplayPreview() {
900   previewPane = $('rdetail_preview_div');
901   if ( $('rdetail_preview_div').getAttribute('loaded') == null ||  $('rdetail_preview_div').getAttribute('loaded') == "false" ) {
902     google.load("books", "0", {"callback" : rdetailViewerLoadCallback, "language": "hy"} );
903     $('rdetail_preview_div').setAttribute('loaded', 'true');
904   }
905 }
906
907 function rdetailViewerLoadCallback() {
908
909   var viewer = new google.books.DefaultViewer(document.getElementById('rdetail_preview_div'));
910   viewer.load('ISBN:' + cleanISBN(record.isbn()) );
911
912 }