2 Copyright(c)2008 Internet Archive. Software license AGPL version 3.
4 This file is part of GnuBook.
6 GnuBook is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Affero General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GnuBook is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Affero General Public License for more details.
16 You should have received a copy of the GNU Affero General Public License
17 along with GnuBook. If not, see <http://www.gnu.org/licenses/>.
19 The GnuBook source is hosted at http://github.com/openlibrary/bookreader/
21 archive.org cvs $Revision: 1.58 $ $Date: 2008-11-03 22:25:40 $
25 //______________________________________________________________________________
26 // After you instantiate this object, you must supply the following
27 // book-specific functions, before calling init():
31 // You must also add a numLeafs property before calling init().
36 this.mode = 1; //1 or 2
38 this.displayedLeafs = [];
39 //this.leafsToDisplay = [];
41 this.prefetchedImgs = {}; //an object with numeric keys cooresponding to leafNum
44 this.animating = false;
46 this.autoTimer = null;
47 this.flipSpeed = 'fast';
49 this.twoPagePopUp = null;
50 this.leafEdgeTmp = null;
52 this.searchResults = {};
54 this.firstIndex = null;
59 //______________________________________________________________________________
60 GnuBook.prototype.init = function() {
62 var startLeaf = window.location.hash;
63 var title = this.bookTitle.substr(0,50);
64 if (this.bookTitle.length>50) title += '...';
66 $("#GnuBook").empty();
67 $("#GnuBook").append("<div id='GBtoolbar'><span style='float:left;'><button class='GBicon' id='zoom_out' onclick='gb.zoom1up(-1); return false;'/> <button class='GBicon' id='zoom_in' onclick='gb.zoom1up(1); return false;'/> zoom: <span id='GBzoom'>25</span>% <button class='GBicon' id='script' onclick='gb.switchMode(1); return false;'/> <button class='GBicon' id='book_open' onclick='gb.switchMode(2); return false;'/> <a href='"+this.bookUrl+"' target='_blank'>"+title+"</a></span></div>");
68 $("#GBtoolbar").append("<form class='GBpageform' action='javascript:' onsubmit='gb.jumpToPage(this.elements[0].value)'><button class='GBicon' id='page_code' onclick='gb.showEmbedCode(); return false;'/> page:<input id='GBpagenum' type='text' size='3' onfocus='gb.autoStop();'></input> <button class='GBicon' id='book_previous' onclick='gb.prev(); return false;'/> <button class='GBicon' id='book_next' onclick='gb.next(); return false;'/> <button class='GBicon play' id='autoImg' onclick='gb.autoToggle(); return false;'/></form>");
69 $("#GnuBook").append("<div id='GBcontainer'></div>");
70 $("#GBcontainer").append("<div id='GBpageview'></div>");
72 $("#GBcontainer").bind('scroll', this, function(e) {
76 this.setupKeyListeners();
78 $(window).bind('resize', this, function(e) {
79 //console.log('resize!');
80 if (1 == e.data.mode) {
81 //console.log('centering 1page view');
82 e.data.centerPageView();
83 $('#GBpageview').empty()
84 e.data.displayedLeafs = [];
85 e.data.updateSearchHilites(); //deletes hilights but does not call remove()
88 //console.log('drawing 2 page view');
89 e.data.prepareTwoPageView();
94 this.resizePageView();
96 if ('' != startLeaf) {
97 this.jumpToIndex(parseInt(startLeaf.substr(1)));
98 //alert('jump to ' + startLeaf);
101 this.displayedLeafs=[0];
102 this.prepareTwoPageView();
103 //if (this.auto) this.nextPage();
107 GnuBook.prototype.setupKeyListeners = function() {
120 $(window).keypress(function(e) {
149 //______________________________________________________________________________
150 GnuBook.prototype.drawLeafs = function() {
151 if (1 == this.mode) {
152 this.drawLeafsOnePage();
154 this.drawLeafsTwoPage();
159 // drawLeafsOnePage()
160 //______________________________________________________________________________
161 GnuBook.prototype.drawLeafsOnePage = function() {
162 //alert('drawing leafs!');
166 var scrollTop = $('#GBcontainer').attr('scrollTop');
167 var scrollBottom = scrollTop + $('#GBcontainer').height();
168 //console.log('top=' + scrollTop + ' bottom='+scrollBottom);
170 var leafsToDisplay = [];
175 for (i=0; i<this.numLeafs; i++) {
176 var height = parseInt(this.getPageHeight(i)/this.reduce);
178 leafBottom += height;
179 //console.log('leafTop = '+leafTop+ ' pageH = ' + this.pageH[i] + 'leafTop>=scrollTop=' + (leafTop>=scrollTop));
180 var topInView = (leafTop >= scrollTop) && (leafTop <= scrollBottom);
181 var bottomInView = (leafBottom >= scrollTop) && (leafBottom <= scrollBottom);
182 var middleInView = (leafTop <=scrollTop) && (leafBottom>=scrollBottom);
183 if (topInView | bottomInView | middleInView) {
184 //console.log('to display: ' + i);
185 leafsToDisplay.push(i);
187 leafTop += height +10;
191 var firstLeafToDraw = leafsToDisplay[0];
192 window.location.hash = firstLeafToDraw;
193 this.firstIndex = firstLeafToDraw;
195 if ((0 != firstLeafToDraw) && (1 < this.reduce)) {
197 leafsToDisplay.unshift(firstLeafToDraw);
200 var lastLeafToDraw = leafsToDisplay[leafsToDisplay.length-1];
201 if ( ((this.numLeafs-1) != lastLeafToDraw) && (1 < this.reduce) ) {
202 leafsToDisplay.push(lastLeafToDraw+1);
207 for (i=0; i<firstLeafToDraw; i++) {
208 leafTop += parseInt(this.getPageHeight(i)/this.reduce) +10;
211 //var viewWidth = $('#GBpageview').width(); //includes scroll bar width
212 var viewWidth = $('#GBcontainer').attr('scrollWidth');
215 for (i=0; i<leafsToDisplay.length; i++) {
216 var leafNum = leafsToDisplay[i];
217 var height = parseInt(this.getPageHeight(leafNum)/this.reduce);
219 if(-1 == jQuery.inArray(leafsToDisplay[i], this.displayedLeafs)) {
220 var width = parseInt(this.getPageWidth(leafNum)/this.reduce);
221 //console.log("displaying leaf " + leafsToDisplay[i] + ' leafTop=' +leafTop);
222 var div = document.createElement("div");
223 div.className = 'GBpagediv1up';
224 div.id = 'pagediv'+leafNum;
225 div.style.position = "absolute";
226 $(div).css('top', leafTop + 'px');
227 var left = (viewWidth-width)>>1;
228 if (left<0) left = 0;
229 $(div).css('left', left+'px');
230 $(div).css('width', width+'px');
231 $(div).css('height', height+'px');
232 //$(div).text('loading...');
234 $('#GBpageview').append(div);
236 var img = document.createElement("img");
237 img.src = this.getPageURI(leafNum);
238 $(img).css('width', width+'px');
239 $(img).css('height', height+'px');
243 //console.log("not displaying " + leafsToDisplay[i] + ' score=' + jQuery.inArray(leafsToDisplay[i], this.displayedLeafs));
246 leafTop += height +10;
250 for (i=0; i<this.displayedLeafs.length; i++) {
251 if (-1 == jQuery.inArray(this.displayedLeafs[i], leafsToDisplay)) {
252 var leafNum = this.displayedLeafs[i];
253 //console.log('Removing leaf ' + leafNum);
254 //console.log('id='+'#pagediv'+leafNum+ ' top = ' +$('#pagediv'+leafNum).css('top'));
255 $('#pagediv'+leafNum).remove();
257 //console.log('NOT Removing leaf ' + this.displayedLeafs[i]);
261 this.displayedLeafs = leafsToDisplay.slice();
262 this.updateSearchHilites();
264 if (null != this.getPageNum(firstLeafToDraw)) {
265 $("#GBpagenum").val(this.getPageNum(firstLeafToDraw));
267 $("#GBpagenum").val('');
271 // drawLeafsTwoPage()
272 //______________________________________________________________________________
273 GnuBook.prototype.drawLeafsTwoPage = function() {
274 //alert('drawing two leafs!');
276 var scrollTop = $('#GBcontainer').attr('scrollTop');
277 var scrollBottom = scrollTop + $('#GBcontainer').height();
278 var leafNum = this.currentLeafL;
279 var height = this.getPageHeight(leafNum);
280 var width = this.getPageWidth(leafNum);
281 var handSide= this.getPageSide(leafNum);
283 var leafEdgeWidthL = parseInt( (leafNum/this.numLeafs)*this.twoPageEdgeW );
284 var leafEdgeWidthR = this.twoPageEdgeW - leafEdgeWidthL;
285 var divWidth = this.twoPageW*2+20 + this.twoPageEdgeW;
286 var divLeft = ($('#GBcontainer').width() - divWidth) >> 1;
287 //console.log(leafEdgeWidthL);
289 var middle = ($('#GBcontainer').width() >> 1);
290 var left = middle - this.twoPageW;
291 var top = ($('#GBcontainer').height() - this.twoPageH) >> 1;
293 var scaledW = parseInt(this.twoPageH*width/height);
294 left = 10+leafEdgeWidthL;
295 //var right = left+scaledW;
296 var right = $(this.twoPageDiv).width()-11-$(this.leafEdgeL).width()-scaledW;
298 var gutter = middle+parseInt((2*this.currentLeafL - this.numLeafs)*this.twoPageEdgeW/this.numLeafs/2);
301 this.prefetchImg(leafNum);
302 $(this.prefetchedImgs[leafNum]).css({
303 position: 'absolute',
304 /*right: gutter+'px',*/
305 left: gutter-scaledW+'px',
307 backgroundColor: 'rgb(234, 226, 205)',
308 height: this.twoPageH +'px',
309 width: scaledW + 'px',
310 borderRight: '1px solid black',
312 }).appendTo('#GBcontainer');
313 //$('#GBcontainer').append(this.prefetchedImgs[leafNum]);
316 var leafNum = this.currentLeafR;
317 var height = this.getPageHeight(leafNum);
318 var width = this.getPageWidth(leafNum);
319 // var left = ($('#GBcontainer').width() >> 1);
322 var scaledW = this.twoPageH*width/height;
323 this.prefetchImg(leafNum);
324 $(this.prefetchedImgs[leafNum]).css({
325 position: 'absolute',
328 backgroundColor: 'rgb(234, 226, 205)',
329 height: this.twoPageH + 'px',
330 width: scaledW + 'px',
331 borderLeft: '1px solid black',
333 }).appendTo('#GBcontainer');
334 //$('#GBcontainer').append(this.prefetchedImgs[leafNum]);
337 this.displayedLeafs = [this.currentLeafL, this.currentLeafR];
338 this.setClickHandlers();
340 this.updatePageNumBox2UP();
343 // updatePageNumBox2UP
344 //______________________________________________________________________________
345 GnuBook.prototype.updatePageNumBox2UP = function() {
346 if (null != this.getPageNum(this.currentLeafL)) {
347 $("#GBpagenum").val(this.getPageNum(this.currentLeafL));
349 $("#GBpagenum").val('');
351 window.location.hash = this.currentLeafL;
355 //______________________________________________________________________________
356 GnuBook.prototype.loadLeafs = function() {
360 if (null == this.timer) {
361 this.timer=setTimeout(function(){self.drawLeafs()},250);
363 clearTimeout(this.timer);
364 this.timer=setTimeout(function(){self.drawLeafs()},250);
370 //______________________________________________________________________________
371 GnuBook.prototype.zoom1up = function(dir) {
372 if (2 == this.mode) { //can only zoom in 1-page mode
378 if (this.reduce <= 0.5) return;
379 this.reduce*=0.5; //zoom in
381 if (this.reduce >= 8) return;
382 this.reduce*=2; //zoom out
385 this.resizePageView();
387 $('#GBpageview').empty()
388 this.displayedLeafs = [];
391 $('#GBzoom').text(100/this.reduce);
396 //______________________________________________________________________________
397 GnuBook.prototype.resizePageView = function() {
400 //var viewWidth = $('#GBcontainer').width(); //includes scrollBar
401 var viewWidth = $('#GBcontainer').attr('clientWidth');
403 var oldScrollTop = $('#GBcontainer').attr('scrollTop');
404 var oldViewHeight = $('#GBpageview').height();
405 if (0 != oldViewHeight) {
406 var scrollRatio = oldScrollTop / oldViewHeight;
411 for (i=0; i<this.numLeafs; i++) {
412 viewHeight += parseInt(this.getPageHeight(i)/this.reduce) + this.padding;
413 var width = parseInt(this.getPageWidth(i)/this.reduce);
414 if (width>viewWidth) viewWidth=width;
416 $('#GBpageview').height(viewHeight);
417 $('#GBpageview').width(viewWidth);
419 $('#GBcontainer').attr('scrollTop', Math.floor(scrollRatio*viewHeight));
421 this.centerPageView();
427 //______________________________________________________________________________
428 GnuBook.prototype.centerPageView = function() {
430 var scrollWidth = $('#GBcontainer').attr('scrollWidth');
431 var clientWidth = $('#GBcontainer').attr('clientWidth');
432 //console.log('sW='+scrollWidth+' cW='+clientWidth);
433 if (scrollWidth > clientWidth) {
434 $('#GBcontainer').attr('scrollLeft', (scrollWidth-clientWidth)/2);
440 //______________________________________________________________________________
441 GnuBook.prototype.jumpToPage = function(pageNum) {
442 //if (2 == this.mode) return;
445 var foundPage = false;
446 var foundLeaf = null;
447 for (i=0; i<this.numLeafs; i++) {
448 if (this.getPageNum(i) == pageNum) {
458 this.jumpToIndex(foundLeaf);
459 $('#GBcontainer').attr('scrollTop', leafTop);
461 alert('Page not found. This book might not have pageNumbers in scandata.');
466 //______________________________________________________________________________
467 GnuBook.prototype.jumpToIndex = function(index) {
468 if (2 == this.mode) {
470 if (index<this.currentLeafL) {
471 if ('L' == this.getPageSide(index)) {
472 this.flipBackToIndex(index);
474 this.flipBackToIndex(index-1);
476 } else if (index>this.currentLeafR) {
477 if ('R' == this.getPageSide(index)) {
478 this.flipFwdToIndex(index);
480 this.flipFwdToIndex(index+1);
488 for (i=0; i<index; i++) {
489 h = parseInt(this.getPageHeight(i)/this.reduce);
490 leafTop += h + this.padding;
492 //$('#GBcontainer').attr('scrollTop', leafTop);
493 $('#GBcontainer').animate({scrollTop: leafTop}, 'fast');
500 //______________________________________________________________________________
501 GnuBook.prototype.switchMode = function(mode) {
502 if (mode == this.mode) return;
505 this.removeSearchHilites();
509 this.prepareOnePageView();
511 this.prepareTwoPageView();
516 //prepareOnePageView()
517 //______________________________________________________________________________
518 GnuBook.prototype.prepareOnePageView = function() {
520 var startLeaf = this.displayedLeafs[0];
522 $('#GBcontainer').empty();
523 $('#GBcontainer').css({
528 $("#GBcontainer").append("<div id='GBpageview'></div>");
529 this.resizePageView();
530 this.jumpToIndex(startLeaf);
531 this.displayedLeafs = [];
532 this.drawLeafsOnePage();
533 $('#GBzoom').text(100/this.reduce);
536 // prepareTwoPageView()
537 //______________________________________________________________________________
538 GnuBook.prototype.prepareTwoPageView = function() {
539 $('#GBcontainer').empty();
541 var firstLeaf = this.displayedLeafs[0];
542 if ('R' == this.getPageSide(firstLeaf)) {
543 if (0 == firstLeaf) {
550 this.currentLeafL = null;
551 this.currentLeafR = null;
552 this.pruneUnusedImgs();
554 this.currentLeafL = firstLeaf;
555 this.currentLeafR = firstLeaf + 1;
557 this.calculateSpreadSize(); //sets this.twoPageW, twoPageH, and twoPageRatio
559 var middle = ($('#GBcontainer').width() >> 1);
560 var gutter = middle+parseInt((2*this.currentLeafL - this.numLeafs)*this.twoPageEdgeW/this.numLeafs/2);
561 var scaledWL = this.getPageWidth2UP(this.currentLeafL);
562 var scaledWR = this.getPageWidth2UP(this.currentLeafR);
563 var leafEdgeWidthL = parseInt( (firstLeaf/this.numLeafs)*this.twoPageEdgeW );
564 var leafEdgeWidthR = this.twoPageEdgeW - leafEdgeWidthL;
566 //console.log('idealWidth='+idealWidth+' idealHeight='+idealHeight);
567 //var divWidth = this.twoPageW*2+20 + this.twoPageEdgeW;
568 var divWidth = scaledWL + scaledWR + 20 + this.twoPageEdgeW;
569 var divHeight = this.twoPageH+20;
570 //var divLeft = ($('#GBcontainer').width() - divWidth) >> 1;
571 var divLeft = gutter-scaledWL-leafEdgeWidthL-10;
572 var divTop = ($('#GBcontainer').height() - divHeight) >> 1;
573 //console.log('divWidth='+divWidth+' divHeight='+divHeight+ ' divLeft='+divLeft+' divTop='+divTop);
575 this.twoPageDiv = document.createElement('div');
576 $(this.twoPageDiv).attr('id', 'book_div_1').css({
577 border: '1px solid rgb(68, 25, 17)',
578 width: divWidth + 'px',
579 height: divHeight+'px',
580 visibility: 'visible',
581 position: 'absolute',
582 backgroundColor: 'rgb(136, 51, 34)',
583 left: divLeft + 'px',
585 MozBorderRadiusTopleft: '7px',
586 MozBorderRadiusTopright: '7px',
587 MozBorderRadiusBottomright: '7px',
588 MozBorderRadiusBottomleft: '7px'
589 }).appendTo('#GBcontainer');
590 //$('#GBcontainer').append('<div id="book_div_1" style="border: 1px solid rgb(68, 25, 17); width: ' + divWidth + 'px; height: '+divHeight+'px; visibility: visible; position: absolute; background-color: rgb(136, 51, 34); left: ' + divLeft + 'px; top: '+divTop+'px; -moz-border-radius-topleft: 7px; -moz-border-radius-topright: 7px; -moz-border-radius-bottomright: 7px; -moz-border-radius-bottomleft: 7px;"/>');
593 var height = this.getPageHeight(this.currentLeafR);
594 var width = this.getPageWidth(this.currentLeafR);
595 var scaledW = this.twoPageH*width/height;
597 this.leafEdgeR = document.createElement('div');
598 $(this.leafEdgeR).css({
599 borderStyle: 'solid solid solid none',
600 borderColor: 'rgb(51, 51, 34)',
601 borderWidth: '1px 1px 1px 0px',
602 background: 'transparent url(http://www.us.archive.org/GnuBook/images/right-edges.png) repeat scroll 0% 0%',
603 width: leafEdgeWidthR + 'px',
604 height: this.twoPageH-1 + 'px',
606 left: gutter+scaledW+'px',
609 }).appendTo('#GBcontainer');
611 this.leafEdgeL = document.createElement('div');
612 $(this.leafEdgeL).css({
613 borderStyle: 'solid none solid solid',
614 borderColor: 'rgb(51, 51, 34)',
615 borderWidth: '1px 0px 1px 1px',
616 background: 'transparent url(http://www.us.archive.org/GnuBook/images/left-edges.png) repeat scroll 0% 0%',
617 width: leafEdgeWidthL + 'px',
618 height: this.twoPageH-1 + 'px',
619 left: divLeft+10+'px',
622 }).appendTo('#GBcontainer');
627 divHeight = this.twoPageH+20;
628 divLeft = ($('#GBcontainer').width() - divWidth) >> 1;
629 divTop = ($('#GBcontainer').height() - divHeight) >> 1;
631 div = document.createElement('div');
632 $(div).attr('id', 'book_div_2').css({
633 border: '1px solid rgb(68, 25, 17)',
634 width: divWidth+'px',
635 height: divHeight+'px',
636 position: 'absolute',
637 backgroundColor: 'rgb(68, 25, 17)',
640 }).appendTo('#GBcontainer');
641 //$('#GBcontainer').append('<div id="book_div_2" style="border: 1px solid rgb(68, 25, 17); width: '+divWidth+'px; height: '+divHeight+'px; visibility: visible; position: absolute; background-color: rgb(68, 25, 17); left: '+divLeft+'px; top: '+divTop+'px;"/>');
643 divWidth = this.twoPageW*2;
644 divHeight = this.twoPageH;
645 divLeft = ($('#GBcontainer').width() - divWidth) >> 1;
646 divTop = ($('#GBcontainer').height() - divHeight) >> 1;
649 this.prepareTwoPagePopUp();
651 this.displayedLeafs = [];
652 //this.leafsToDisplay=[firstLeaf, firstLeaf+1];
653 //console.log('leafsToDisplay: ' + this.leafsToDisplay[0] + ' ' + this.leafsToDisplay[1]);
654 this.drawLeafsTwoPage();
655 this.updateSearchHilites2UP();
658 $('#GBzoom').text((100*this.twoPageH/this.getPageHeight(firstLeaf)).toString().substr(0,4));
661 // prepareTwoPagePopUp()
662 //______________________________________________________________________________
663 GnuBook.prototype.prepareTwoPagePopUp = function() {
664 this.twoPagePopUp = document.createElement('div');
665 $(this.twoPagePopUp).css({
666 border: '1px solid black',
668 position: 'absolute',
669 fontFamily: 'sans-serif',
672 backgroundColor: 'rgb(255, 255, 238)',
674 }).appendTo('#GBcontainer');
675 $(this.twoPagePopUp).hide();
677 $(this.leafEdgeL).add(this.leafEdgeR).bind('mouseenter', this, function(e) {
678 $(e.data.twoPagePopUp).show();
681 $(this.leafEdgeL).add(this.leafEdgeR).bind('mouseleave', this, function(e) {
682 $(e.data.twoPagePopUp).hide();
685 $(this.leafEdgeL).bind('click', this, function(e) {
687 var jumpIndex = e.data.currentLeafL - ($(e.data.leafEdgeL).offset().left + $(e.data.leafEdgeL).width() - e.pageX) * 10;
688 jumpIndex = Math.max(jumpIndex, 0);
689 e.data.flipBackToIndex(jumpIndex);
693 $(this.leafEdgeR).bind('click', this, function(e) {
695 var jumpIndex = e.data.currentLeafR + (e.pageX - $(e.data.leafEdgeR).offset().left) * 10;
696 jumpIndex = Math.max(jumpIndex, 0);
697 e.data.flipFwdToIndex(jumpIndex);
701 $(this.leafEdgeR).bind('mousemove', this, function(e) {
703 var jumpLeaf = e.data.currentLeafR + (e.pageX - $(e.data.leafEdgeR).offset().left) * 10;
704 jumpLeaf = Math.min(jumpLeaf, e.data.numLeafs-1);
705 $(e.data.twoPagePopUp).text('View Leaf '+jumpLeaf);
707 $(e.data.twoPagePopUp).css({
708 left: e.pageX +5+ 'px',
709 top: e.pageY-$('#GBcontainer').offset().top+ 'px'
713 $(this.leafEdgeL).bind('mousemove', this, function(e) {
714 var jumpLeaf = e.data.currentLeafL - ($(e.data.leafEdgeL).offset().left + $(e.data.leafEdgeL).width() - e.pageX) * 10;
715 jumpLeaf = Math.max(jumpLeaf, 0);
716 $(e.data.twoPagePopUp).text('View Leaf '+jumpLeaf);
718 $(e.data.twoPagePopUp).css({
719 left: e.pageX - $(e.data.twoPagePopUp).width() - 30 + 'px',
720 top: e.pageY-$('#GBcontainer').offset().top+ 'px'
725 // calculateSpreadSize()
726 //______________________________________________________________________________
727 // Calculates 2-page spread dimensions based on this.currentLeafL and
729 // This function sets this.twoPageH, twoPageW, and twoPageRatio
731 GnuBook.prototype.calculateSpreadSize = function() {
732 var firstLeaf = this.currentLeafL;
733 var secondLeaf = this.currentLeafR;
734 //console.log('first page is ' + firstLeaf);
736 var canon5Dratio = 1.5;
738 var firstLeafRatio = this.getPageHeight(firstLeaf) / this.getPageWidth(firstLeaf);
739 var secondLeafRatio = this.getPageHeight(secondLeaf) / this.getPageWidth(secondLeaf);
740 //console.log('firstLeafRatio = ' + firstLeafRatio + ' secondLeafRatio = ' + secondLeafRatio);
743 if (Math.abs(firstLeafRatio - canon5Dratio) < Math.abs(secondLeafRatio - canon5Dratio)) {
744 ratio = firstLeafRatio;
745 //console.log('using firstLeafRatio ' + ratio);
747 ratio = secondLeafRatio;
748 //console.log('using secondLeafRatio ' + ratio);
751 var totalLeafEdgeWidth = parseInt(this.numLeafs * 0.1);
752 var maxLeafEdgeWidth = parseInt($('#GBcontainer').width() * 0.1);
753 totalLeafEdgeWidth = Math.min(totalLeafEdgeWidth, maxLeafEdgeWidth);
755 $('#GBcontainer').css('overflow', 'hidden');
757 var idealWidth = ($('#GBcontainer').width() - 30 - totalLeafEdgeWidth)>>1;
758 var idealHeight = $('#GBcontainer').height() - 30;
759 //console.log('init idealWidth='+idealWidth+' idealHeight='+idealHeight + ' ratio='+ratio);
761 if (idealHeight/ratio <= idealWidth) {
763 idealWidth = parseInt(idealHeight/ratio);
766 idealHeight = parseInt(idealWidth*ratio);
769 this.twoPageH = idealHeight;
770 this.twoPageW = idealWidth;
771 this.twoPageRatio = ratio;
772 this.twoPageEdgeW = totalLeafEdgeWidth;
777 //______________________________________________________________________________
778 GnuBook.prototype.next = function() {
779 if (2 == this.mode) {
781 this.flipFwdToIndex(null);
783 if (this.firstIndex <= (this.numLeafs - 2)) {
784 this.jumpToIndex(this.firstIndex+1);
790 //______________________________________________________________________________
791 GnuBook.prototype.prev = function() {
792 if (2 == this.mode) {
794 this.flipBackToIndex(null);
796 if (this.firstIndex >= 1) {
797 this.jumpToIndex(this.firstIndex-1);
802 GnuBook.prototype.home = function() {
803 if (2 == this.mode) {
811 GnuBook.prototype.end = function() {
812 if (2 == this.mode) {
813 this.jumpToIndex(this.numLeafs-5);
816 this.jumpToIndex(this.numLeafs-1);
821 //______________________________________________________________________________
822 // to flip back one spread, pass index=null
823 GnuBook.prototype.flipBackToIndex = function(index) {
824 if (1 == this.mode) return;
826 var leftIndex = this.currentLeafL;
827 if (leftIndex <= 2) return;
828 if (this.animating) return;
830 if (null != this.leafEdgeTmp) {
831 alert('error: leafEdgeTmp should be null!');
840 if ('L' != this.getPageSide(index)) {
841 alert('img with index ' + index + ' is not a left-hand page');
845 this.animating = true;
850 var gutter= this.prepareFlipBack(prevL, prevR);
852 var leftLeaf = this.currentLeafL;
854 var oldLeafEdgeWidthL = parseInt( (this.currentLeafL/this.numLeafs)*this.twoPageEdgeW );
855 var newLeafEdgeWidthL = parseInt( (index /this.numLeafs)*this.twoPageEdgeW );
856 var leafEdgeTmpW = oldLeafEdgeWidthL - newLeafEdgeWidthL;
858 var scaledWL = this.getPageWidth2UP(prevL);
860 var top = ($('#GBcontainer').height() - this.twoPageH) >> 1;
862 this.leafEdgeTmp = document.createElement('div');
863 $(this.leafEdgeTmp).css({
864 borderStyle: 'solid none solid solid',
865 borderColor: 'rgb(51, 51, 34)',
866 borderWidth: '1px 0px 1px 1px',
867 background: 'transparent url(http://www.us.archive.org/GnuBook/images/left-edges.png) repeat scroll 0% 0%',
868 width: leafEdgeTmpW + 'px',
869 height: this.twoPageH-1 + 'px',
870 left: gutter-scaledWL+10+newLeafEdgeWidthL+'px',
872 position: 'absolute',
874 }).appendTo('#GBcontainer');
876 //$(this.leafEdgeL).css('width', newLeafEdgeWidthL+'px');
877 $(this.leafEdgeL).css({
878 width: newLeafEdgeWidthL+'px',
879 left: gutter-scaledWL-newLeafEdgeWidthL+'px'
882 var left = $(this.prefetchedImgs[leftLeaf]).offset().left;
883 var right = $('#GBcontainer').width()-left-$(this.prefetchedImgs[leftLeaf]).width()+$('#GBcontainer').offset().left-2+'px';
884 $(this.prefetchedImgs[leftLeaf]).css({
889 left = $(this.prefetchedImgs[leftLeaf]).offset().left - $('#book_div_1').offset().left;
890 right = left+$(this.prefetchedImgs[leftLeaf]).width()+'px';
892 $(this.leafEdgeTmp).animate({left: gutter}, this.flipSpeed, 'easeInSine');
893 //$(this.prefetchedImgs[leftLeaf]).animate({width: '0px'}, 'slow', 'easeInSine');
895 var scaledWR = this.getPageWidth2UP(prevR);
899 this.removeSearchHilites();
901 $(this.prefetchedImgs[leftLeaf]).animate({width: '0px'}, self.flipSpeed, 'easeInSine', function() {
902 $(self.leafEdgeTmp).animate({left: gutter+scaledWR+'px'}, self.flipSpeed, 'easeOutSine');
903 $(self.prefetchedImgs[prevR]).animate({width: scaledWR+'px'}, self.flipSpeed, 'easeOutSine', function() {
904 $(self.prefetchedImgs[prevL]).css('zIndex', 2);
906 $(self.leafEdgeR).css({
907 width: self.twoPageEdgeW-newLeafEdgeWidthL+'px',
908 left: gutter+scaledWR+'px'
911 $(self.twoPageDiv).css({
912 width: scaledWL+scaledWR+self.twoPageEdgeW+20+'px',
913 left: gutter-scaledWL-newLeafEdgeWidthL-10+'px'
916 $(self.leafEdgeTmp).remove();
917 self.leafEdgeTmp = null;
919 self.currentLeafL = prevL;
920 self.currentLeafR = prevR;
921 self.displayedLeafs = [prevL, prevR];
922 self.setClickHandlers();
923 self.pruneUnusedImgs();
925 self.animating = false;
927 self.updateSearchHilites2UP();
928 self.updatePageNumBox2UP();
929 //$('#GBzoom').text((self.twoPageH/self.getPageHeight(prevL)).toString().substr(0,4));
936 //______________________________________________________________________________
937 // to flip forward one spread, pass index=null
938 GnuBook.prototype.flipFwdToIndex = function(index) {
939 var rightLeaf = this.currentLeafR;
940 if (rightLeaf >= this.numLeafs-3) return;
942 if (this.animating) return;
944 if (null != this.leafEdgeTmp) {
945 alert('error: leafEdgeTmp should be null!');
953 if (index>=this.numLeafs-3) return;
955 if ('R' != this.getPageSide(index)) {
956 alert('img with index ' + index + ' is not a right-hand page');
960 this.animating = true;
965 var gutter= this.prepareFlipFwd(nextL, nextR);
967 var oldLeafEdgeWidthL = parseInt( (this.currentLeafL/this.numLeafs)*this.twoPageEdgeW );
968 var oldLeafEdgeWidthR = this.twoPageEdgeW-oldLeafEdgeWidthL;
969 var newLeafEdgeWidthL = parseInt( (nextL /this.numLeafs)*this.twoPageEdgeW );
970 var newLeafEdgeWidthR = this.twoPageEdgeW-newLeafEdgeWidthL;
972 var leafEdgeTmpW = oldLeafEdgeWidthR - newLeafEdgeWidthR;
974 var top = ($('#GBcontainer').height() - this.twoPageH) >> 1;
976 var height = this.getPageHeight(rightLeaf);
977 var width = this.getPageWidth(rightLeaf);
978 var scaledW = this.twoPageH*width/height;
980 var middle = ($('#GBcontainer').width() >> 1);
981 var currGutter = middle+parseInt((2*this.currentLeafL - this.numLeafs)*this.twoPageEdgeW/this.numLeafs/2);
983 this.leafEdgeTmp = document.createElement('div');
984 $(this.leafEdgeTmp).css({
985 borderStyle: 'solid none solid solid',
986 borderColor: 'rgb(51, 51, 34)',
987 borderWidth: '1px 0px 1px 1px',
988 background: 'transparent url(http://www.us.archive.org/GnuBook/images/left-edges.png) repeat scroll 0% 0%',
989 width: leafEdgeTmpW + 'px',
990 height: this.twoPageH-1 + 'px',
991 left: currGutter+scaledW+'px',
993 position: 'absolute',
995 }).appendTo('#GBcontainer');
997 var scaledWR = this.getPageWidth2UP(nextR);
998 $(this.leafEdgeR).css({width: newLeafEdgeWidthR+'px', left: gutter+scaledWR+'px' });
1000 var scaledWL = this.getPageWidth2UP(nextL);
1004 var speed = this.flipSpeed;
1006 this.removeSearchHilites();
1008 $(this.leafEdgeTmp).animate({left: gutter}, speed, 'easeInSine');
1009 $(this.prefetchedImgs[rightLeaf]).animate({width: '0px'}, speed, 'easeInSine', function() {
1010 $(self.leafEdgeTmp).animate({left: gutter-scaledWL-leafEdgeTmpW+'px'}, speed, 'easeOutSine');
1011 $(self.prefetchedImgs[nextL]).animate({width: scaledWL+'px'}, speed, 'easeOutSine', function() {
1012 $(self.prefetchedImgs[nextR]).css('zIndex', 2);
1014 $(self.leafEdgeL).css({
1015 width: newLeafEdgeWidthL+'px',
1016 left: gutter-scaledWL-newLeafEdgeWidthL+'px'
1019 $(self.twoPageDiv).css({
1020 width: scaledWL+scaledWR+self.twoPageEdgeW+20+'px',
1021 left: gutter-scaledWL-newLeafEdgeWidthL-10+'px'
1024 $(self.leafEdgeTmp).remove();
1025 self.leafEdgeTmp = null;
1027 self.currentLeafL = nextL;
1028 self.currentLeafR = nextR;
1029 self.displayedLeafs = [nextL, nextR];
1030 self.setClickHandlers();
1031 self.pruneUnusedImgs();
1033 self.animating = false;
1035 self.updateSearchHilites2UP();
1036 self.updatePageNumBox2UP();
1037 //$('#GBzoom').text((self.twoPageH/self.getPageHeight(nextL)).toString().substr(0,4));
1044 //______________________________________________________________________________
1045 GnuBook.prototype.setClickHandlers = function() {
1047 $(this.prefetchedImgs[this.currentLeafL]).click(function() {
1050 self.flipBackToIndex(null);
1052 $(this.prefetchedImgs[this.currentLeafR]).click(function() {
1055 self.flipFwdToIndex(null);
1060 //______________________________________________________________________________
1061 GnuBook.prototype.prefetchImg = function(leafNum) {
1062 if (undefined == this.prefetchedImgs[leafNum]) {
1063 var img = document.createElement("img");
1064 img.src = this.getPageURI(leafNum);
1065 this.prefetchedImgs[leafNum] = img;
1070 // prepareFlipBack()
1071 //______________________________________________________________________________
1072 GnuBook.prototype.prepareFlipBack = function(prevL, prevR) {
1074 this.prefetchImg(prevL);
1075 this.prefetchImg(prevR);
1077 var height = this.getPageHeight(prevL);
1078 var width = this.getPageWidth(prevL);
1079 var middle = ($('#GBcontainer').width() >> 1);
1080 var top = ($('#GBcontainer').height() - this.twoPageH) >> 1;
1081 var scaledW = this.twoPageH*width/height;
1083 var gutter = middle+parseInt((2*prevL - this.numLeafs)*this.twoPageEdgeW/this.numLeafs/2);
1085 $(this.prefetchedImgs[prevL]).css({
1086 position: 'absolute',
1087 /*right: middle+'px',*/
1088 left: gutter-scaledW+'px',
1090 backgroundColor: 'rgb(234, 226, 205)',
1091 height: this.twoPageH,
1092 width: scaledW+'px',
1093 borderRight: '1px solid black',
1097 $('#GBcontainer').append(this.prefetchedImgs[prevL]);
1099 $(this.prefetchedImgs[prevR]).css({
1100 position: 'absolute',
1103 backgroundColor: 'rgb(234, 226, 205)',
1104 height: this.twoPageH,
1106 borderLeft: '1px solid black',
1110 $('#GBcontainer').append(this.prefetchedImgs[prevR]);
1118 //______________________________________________________________________________
1119 GnuBook.prototype.prepareFlipFwd = function(nextL, nextR) {
1121 this.prefetchImg(nextL);
1122 this.prefetchImg(nextR);
1124 var height = this.getPageHeight(nextR);
1125 var width = this.getPageWidth(nextR);
1126 var middle = ($('#GBcontainer').width() >> 1);
1127 var top = ($('#GBcontainer').height() - this.twoPageH) >> 1;
1128 var scaledW = this.twoPageH*width/height;
1130 var gutter = middle+parseInt((2*nextL - this.numLeafs)*this.twoPageEdgeW/this.numLeafs/2);
1132 $(this.prefetchedImgs[nextR]).css({
1133 position: 'absolute',
1136 backgroundColor: 'rgb(234, 226, 205)',
1137 height: this.twoPageH,
1138 width: scaledW+'px',
1139 borderLeft: '1px solid black',
1143 $('#GBcontainer').append(this.prefetchedImgs[nextR]);
1145 height = this.getPageHeight(nextL);
1146 width = this.getPageWidth(nextL);
1147 scaledW = this.twoPageH*width/height;
1149 $(this.prefetchedImgs[nextL]).css({
1150 position: 'absolute',
1151 right: $('#GBcontainer').width()-gutter+'px',
1153 backgroundColor: 'rgb(234, 226, 205)',
1154 height: this.twoPageH,
1156 borderRight: '1px solid black',
1160 $('#GBcontainer').append(this.prefetchedImgs[nextL]);
1167 //______________________________________________________________________________
1168 GnuBook.prototype.getNextLeafs = function(o) {
1169 //TODO: we might have two left or two right leafs in a row (damaged book)
1170 //For now, assume that leafs are contiguous.
1172 //return [this.currentLeafL+2, this.currentLeafL+3];
1173 o.L = this.currentLeafL+2;
1174 o.R = this.currentLeafL+3;
1178 //______________________________________________________________________________
1179 GnuBook.prototype.getPrevLeafs = function(o) {
1180 //TODO: we might have two left or two right leafs in a row (damaged book)
1181 //For now, assume that leafs are contiguous.
1183 //return [this.currentLeafL-2, this.currentLeafL-1];
1184 o.L = this.currentLeafL-2;
1185 o.R = this.currentLeafL-1;
1188 // pruneUnusedImgs()
1189 //______________________________________________________________________________
1190 GnuBook.prototype.pruneUnusedImgs = function() {
1191 //console.log('current: ' + this.currentLeafL + ' ' + this.currentLeafR);
1192 for (var key in this.prefetchedImgs) {
1193 //console.log('key is ' + key);
1194 if ((key != this.currentLeafL) && (key != this.currentLeafR)) {
1195 //console.log('removing key '+ key);
1196 $(this.prefetchedImgs[key]).remove();
1198 if ((key < this.currentLeafL-4) || (key > this.currentLeafR+4)) {
1199 //console.log('deleting key '+ key);
1200 delete this.prefetchedImgs[key];
1206 //______________________________________________________________________________
1207 GnuBook.prototype.prefetch = function() {
1208 var lim = this.currentLeafL-4;
1210 lim = Math.max(lim, 0);
1211 for (i = lim; i < this.currentLeafL; i++) {
1212 this.prefetchImg(i);
1215 if (this.numLeafs > (this.currentLeafR+1)) {
1216 lim = Math.min(this.currentLeafR+4, this.numLeafs-1);
1217 for (i=this.currentLeafR+1; i<=lim; i++) {
1218 this.prefetchImg(i);
1223 // getPageWidth2UP()
1224 //______________________________________________________________________________
1225 GnuBook.prototype.getPageWidth2UP = function(index) {
1226 var height = this.getPageHeight(index);
1227 var width = this.getPageWidth(index);
1228 return Math.floor(this.twoPageH*width/height);
1232 //______________________________________________________________________________
1233 GnuBook.prototype.search = function(term) {
1234 $('#GnuBookSearchScript').remove();
1235 var script = document.createElement("script");
1236 script.setAttribute('id', 'GnuBookSearchScript');
1237 script.setAttribute("type", "text/javascript");
1238 script.setAttribute("src", 'http://'+this.server+'/GnuBook/flipbook_search_gb.php?url='+escape(this.bookPath+'/'+this.bookId+'_djvu.xml')+'&term='+term+'&format=XML&callback=gb.GBSearchCallback');
1239 document.getElementsByTagName('head')[0].appendChild(script);
1242 // GBSearchCallback()
1243 //______________________________________________________________________________
1244 GnuBook.prototype.GBSearchCallback = function(txt) {
1246 if (jQuery.browser.msie) {
1247 var dom=new ActiveXObject("Microsoft.XMLDOM");
1251 var parser = new DOMParser();
1252 var dom = parser.parseFromString(txt, "text/xml");
1255 $('#GnuBookSearchResults').empty();
1256 $('#GnuBookSearchResults').append('<ul>');
1258 for (var key in this.searchResults) {
1259 if (null != this.searchResults[key].div) {
1260 $(this.searchResults[key].div).remove();
1262 delete this.searchResults[key];
1265 var pages = dom.getElementsByTagName('PAGE');
1266 for (var i = 0; i < pages.length; i++){
1267 //console.log(pages[i].getAttribute('file').substr(1) +'-'+ parseInt(pages[i].getAttribute('file').substr(1), 10));
1270 var re = new RegExp (/_(\d{4})/);
1271 var reMatch = re.exec(pages[i].getAttribute('file'));
1272 var leafNum = parseInt(reMatch[1], 10);
1273 //var leafNum = parseInt(pages[i].getAttribute('file').substr(1), 10);
1275 var children = pages[i].childNodes;
1277 for (var j=0; j<children.length; j++) {
1278 //console.log(j + ' - ' + children[j].nodeName);
1279 //console.log(children[j].firstChild.nodeValue);
1280 if ('CONTEXT' == children[j].nodeName) {
1281 context += children[j].firstChild.nodeValue;
1282 } else if ('WORD' == children[j].nodeName) {
1283 context += '<b>'+children[j].firstChild.nodeValue+'</b>';
1285 var index = this.leafNumToIndex(leafNum);
1286 if (null != index) {
1287 //coordinates are [left, bottom, right, top, [baseline]]
1288 //we'll skip baseline for now...
1289 var coords = children[j].getAttribute('coords').split(',',4);
1290 if (4 == coords.length) {
1291 this.searchResults[index] = {'l':coords[0], 'b':coords[1], 'r':coords[2], 't':coords[3], 'div':null};
1296 //TODO: remove hardcoded instance name
1297 $('#GnuBookSearchResults').append('<li><b><a href="javascript:gb.jumpToIndex('+index+');">Leaf ' + leafNum + '</a></b> - ' + context+'</li>');
1299 $('#GnuBookSearchResults').append('</ul>');
1301 this.updateSearchHilites();
1304 // updateSearchHilites()
1305 //______________________________________________________________________________
1306 GnuBook.prototype.updateSearchHilites = function() {
1307 if (2 == this.mode) {
1308 this.updateSearchHilites2UP();
1310 this.updateSearchHilites1UP();
1314 // showSearchHilites1UP()
1315 //______________________________________________________________________________
1316 GnuBook.prototype.updateSearchHilites1UP = function() {
1318 for (var key in this.searchResults) {
1320 if (-1 != jQuery.inArray(parseInt(key), this.displayedLeafs)) {
1321 var result = this.searchResults[key];
1322 if(null == result.div) {
1323 result.div = document.createElement('div');
1324 $(result.div).attr('className', 'GnuBookSearchHilite').appendTo('#pagediv'+key);
1325 //console.log('appending ' + key);
1328 width: (result.r-result.l)/this.reduce + 'px',
1329 height: (result.b-result.t)/this.reduce + 'px',
1330 left: (result.l)/this.reduce + 'px',
1331 top: (result.t)/this.reduce +'px'
1335 //console.log(key + ' not displayed');
1336 this.searchResults[key].div=null;
1341 // showSearchHilites2UP()
1342 //______________________________________________________________________________
1343 GnuBook.prototype.updateSearchHilites2UP = function() {
1345 var middle = ($('#GBcontainer').width() >> 1);
1347 for (var key in this.searchResults) {
1348 key = parseInt(key, 10);
1349 if (-1 != jQuery.inArray(key, this.displayedLeafs)) {
1350 var result = this.searchResults[key];
1351 if(null == result.div) {
1352 result.div = document.createElement('div');
1353 $(result.div).attr('className', 'GnuBookSearchHilite').css('zIndex', 3).appendTo('#GBcontainer');
1354 //console.log('appending ' + key);
1357 var height = this.getPageHeight(key);
1358 var width = this.getPageWidth(key)
1359 var reduce = this.twoPageH/height;
1360 var scaledW = parseInt(width*reduce);
1362 var gutter = middle+parseInt((2*this.currentLeafL - this.numLeafs)*this.twoPageEdgeW/this.numLeafs/2);
1364 if ('L' == this.getPageSide(key)) {
1365 var pageL = gutter-scaledW;
1369 var pageT = ($('#GBcontainer').height() - this.twoPageH) >> 1;
1372 width: (result.r-result.l)*reduce + 'px',
1373 height: (result.b-result.t)*reduce + 'px',
1374 left: pageL+(result.l)*reduce + 'px',
1375 top: pageT+(result.t)*reduce +'px'
1379 //console.log(key + ' not displayed');
1380 if (null != this.searchResults[key].div) {
1381 //console.log('removing ' + key);
1382 $(this.searchResults[key].div).remove();
1384 this.searchResults[key].div=null;
1389 // removeSearchHilites()
1390 //______________________________________________________________________________
1391 GnuBook.prototype.removeSearchHilites = function() {
1392 for (var key in this.searchResults) {
1393 if (null != this.searchResults[key].div) {
1394 $(this.searchResults[key].div).remove();
1395 this.searchResults[key].div=null;
1401 //______________________________________________________________________________
1402 GnuBook.prototype.showEmbedCode = function() {
1404 var overlay = document.createElement("div");
1406 position: 'absolute',
1408 left: ($('#GBcontainer').width()-400)/2 + 'px',
1411 border: "3px double #999999",
1413 backgroundColor: "#fff"
1414 }).appendTo('#GnuBook');
1416 htmlStr = '<p style="text-align:center;"><b>Embed Bookreader in your blog!</b></p>';
1417 htmlStr += '<p><b>Note:</b> The bookreader is still in beta testing. URLs may change in the future, breaking embedded books. This feature is just for testing!</b></p>';
1418 htmlStr += '<p>The bookreader uses iframes for embedding. It will not work on web hosts that block iframes. The embed feature has been tested on blogspot.com blogs as well as self-hosted Wordpress blogs. This feature will NOT work on wordpress.com blogs.</p>';
1419 htmlStr += '<p>Embed Code: <input type="text" size="40" value="<iframe src=\'http://www.us.archive.org/GnuBook/GnuBookEmbed.php?id='+this.bookId+'\' width=\'430px\' height=\'430px\'></iframe>"></p>';
1420 htmlStr += '<p style="text-align:center;"><a href="" onclick="$(this.parentNode.parentNode).remove(); return false;">Close popup</a></p>';
1422 overlay.innerHTML = htmlStr;
1426 //______________________________________________________________________________
1427 GnuBook.prototype.autoToggle = function() {
1428 if (2 != this.mode) {
1433 if (null == this.autoTimer) {
1434 this.flipSpeed = 2000;
1435 this.flipFwdToIndex();
1437 $('#autoImg').removeClass('play').addClass('pause');
1438 this.autoTimer=setInterval(function(){
1439 if (self.animating) {return;}
1441 if (self.currentLeafR >= self.numLeafs-5) {
1442 self.flipBackToIndex(1);
1444 self.flipFwdToIndex();
1453 //______________________________________________________________________________
1454 GnuBook.prototype.autoStop = function() {
1455 if (null != this.autoTimer) {
1456 clearInterval(this.autoTimer);
1457 this.flipSpeed = 'fast';
1458 $('#autoImg').removeClass('pause').addClass('play');
1459 this.autoTimer = null;