'
+ + queryString + '' + uiStringPage + ' ' + pageNumber + '
')
+ .data({'self': this, 'pageIndex': pageIndex })
+ .appendTo('#BRnavline').bt({
contentSelector: '$(this).find(".query")',
- trigger: 'click',
+ trigger: 'hover',
closeWhenOthersOpen: true,
cssStyles: {
- padding: '10px 10px 15px',
+ padding: '12px 14px',
backgroundColor: '#fff',
- border: '3px solid #e2dcc5',
- borderBottom: 'none',
+ border: '4px solid #e2dcc5',
fontFamily: '"Lucida Grande","Arial",sans-serif',
- fontSize: '12px',
- lineHeight: '18px',
+ fontSize: '13px',
+ //lineHeight: '18px',
color: '#615132'
},
shrinkToFit: false,
@@ -3232,7 +3459,7 @@ BookReader.prototype.initNavbar = function() {
padding: 0,
spikeGirth: 0,
spikeLength: 0,
- overlap: '10px',
+ overlap: '22px',
overlay: false,
killTitle: false,
textzIndex: 9999,
@@ -3242,34 +3469,64 @@ BookReader.prototype.initNavbar = function() {
positions: ['top'],
fill: 'white',
windowMargin: 10,
- strokeWidth: 3,
- strokeStyle: '#e2dcc5',
+ strokeWidth: 0,
cornerRadius: 0,
centerPointX: 0,
centerPointY: 0,
shadow: false
+ })
+ .hover( function() {
+ // remove from other markers then turn on just for this
+ // XXX should be done when nav slider moves
+ $('.search,.chapter').removeClass('front');
+ $(this).addClass('front');
+ }, function() {
+ $(this).removeClass('front');
+ }
+ )
+ .bind('click', function() {
+ $(this).data('self').jumpToIndex($(this).data('pageIndex'));
});
- $('.searchChap').bt({
- contentSelector: '$(this).find(".query")',
- trigger: 'click',
+
+ $(marker).animate({top:'-25px'}, 'slow');
+
+}
+
+BookReader.prototype.removeSearchResults = function() {
+ this.removeSearchHilites(); //be sure to set all box.divs to null
+ $('#BRnavpos .search').remove();
+}
+
+BookReader.prototype.addChapter = function(chapterTitle, pageNumber, pageIndex) {
+ var uiStringPage = 'Page'; // i18n
+
+ var percentThrough = BookReader.util.cssPercentage(pageIndex, this.numLeafs - 1);
+
+ $('
'
+ + chapterTitle + '| ' + uiStringPage + ' ' + pageNumber + '
')
+ .appendTo('#BRnavline')
+ .data({'self': this, 'pageIndex': pageIndex })
+ .bt({
+ contentSelector: '$(this).find(".title")',
+ trigger: 'hover',
closeWhenOthersOpen: true,
cssStyles: {
- width: '250px',
- padding: '10px 10px 15px',
- backgroundColor: '#fff',
- border: '3px solid #e2dcc5',
- borderBottom: 'none',
- fontFamily: '"Lucida Grande","Arial",sans-serif',
+ padding: '12px 14px',
+ backgroundColor: '#000',
+ border: '4px solid #e2dcc5',
+ //borderBottom: 'none',
+ fontFamily: '"Arial", sans-serif',
fontSize: '12px',
- lineHeight: '18px',
- color: '#615132'
+ fontWeight: '700',
+ color: '#fff',
+ whiteSpace: 'nowrap'
},
- shrinkToFit: false,
- width: '230px',
+ shrinkToFit: true,
+ width: '200px',
padding: 0,
spikeGirth: 0,
spikeLength: 0,
- overlap: '10px',
+ overlap: '21px',
overlay: false,
killTitle: true,
textzIndex: 9999,
@@ -3277,15 +3534,60 @@ BookReader.prototype.initNavbar = function() {
wrapperzIndex: 9997,
offsetParent: null,
positions: ['top'],
- fill: 'white',
+ fill: 'black',
windowMargin: 10,
- strokeWidth: 3,
- strokeStyle: '#e2dcc5',
+ strokeWidth: 0,
cornerRadius: 0,
centerPointX: 0,
centerPointY: 0,
shadow: false
+ })
+ .hover( function() {
+ // remove hover effect from other markers then turn on just for this
+ $('.search,.chapter').removeClass('front');
+ $(this).addClass('front');
+ }, function() {
+ $(this).removeClass('front');
+ }
+ )
+ .bind('click', function() {
+ $(this).data('self').jumpToIndex($(this).data('pageIndex'));
});
+}
+
+/*
+ * Remove all chapters.
+ */
+BookReader.prototype.removeChapters = function() {
+ $('#BRnavpos .chapter').remove();
+}
+
+/*
+ * Update the table of contents based on array of TOC entries.
+ */
+BookReader.prototype.updateTOC = function(tocEntries) {
+ this.removeChapters();
+ for (var i = 0; i < tocEntries.length; i++) {
+ this.addChapterFromEntry(tocEntries[i]);
+ }
+}
+
+/*
+ * Example table of contents entry - this format is defined by Open Library
+ * {
+ * "pagenum": "17",
+ * "level": 1,
+ * "label": "CHAPTER I",
+ * "type": {"key": "/type/toc_item"},
+ * "title": "THE COUNTRY AND THE MISSION"
+ * }
+ */
+BookReader.prototype.addChapterFromEntry = function(tocEntryObject) {
+ var pageIndex = this.getPageIndex(tocEntryObject['pagenum']);
+ // Only add if we know where it is
+ if (pageIndex) {
+ this.addChapter(tocEntryObject['title'], tocEntryObject['pagenum'], pageIndex);
+ }
$('.chapter').each(function(){
$(this).hover(function(){
$(this).addClass('front');
@@ -3307,27 +3609,44 @@ BookReader.prototype.initNavbar = function() {
$(this).removeClass('front');
});
});
- $("#BRslider").draggable({axis:'x',containment:'parent'});
}
BookReader.prototype.initToolbar = function(mode, ui) {
- $("body").append("
"
+ /*
+ + "
"
+ + "
"
+ + "
"
+ + "
"
+ + "
"
+ + "
"
+ + "
"
+ */
+ );
this.updateToolbarZoom(this.reduce); // Pretty format
@@ -3340,8 +3659,9 @@ BookReader.prototype.initToolbar = function(mode, ui) {
// We build in mode 2
jToolbar.append();
-
- this.bindToolbarNavHandlers(jToolbar);
+
+ // Navigation handlers will be bound after all UI is in place -- makes moving icons between
+ // the toolbar and nav bar easier
// Setup tooltips -- later we could load these from a file for i18n
var titles = { '.logo': 'Go to Archive.org',
@@ -3423,24 +3743,67 @@ BookReader.prototype.switchToolbarMode = function(mode) {
}
}
-// bindToolbarNavHandlers
+// updateToolbarZoom(reduce)
+//______________________________________________________________________________
+// Update the displayed zoom factor based on reduction factor
+BookReader.prototype.updateToolbarZoom = function(reduce) {
+ var value;
+ var autofit = null;
+
+ // $$$ TODO preserve zoom/fit for each mode
+ if (this.mode == this.constMode2up) {
+ autofit = this.twoPage.autofit;
+ } else {
+ autofit = this.onePage.autofit;
+ }
+
+ if (autofit) {
+ value = autofit.slice(0,1).toUpperCase() + autofit.slice(1);
+ } else {
+ value = (100 / reduce).toFixed(2);
+ // Strip trailing zeroes and decimal if all zeroes
+ value = value.replace(/0+$/,'');
+ value = value.replace(/\.$/,'');
+ value += '%';
+ }
+ $('#BRzoom').text(value);
+}
+
+// bindNavigationHandlers
//______________________________________________________________________________
-// Binds the toolbar handlers
-BookReader.prototype.bindToolbarNavHandlers = function(jToolbar) {
+// Bind navigation handlers
+BookReader.prototype.bindNavigationHandlers = function() {
var self = this; // closure
+ jIcons = $('.BRicon');
+
+ jIcons.filter('.onepg').bind('click', function(e) {
+ self.switchMode(self.constMode1up);
+ });
+
+ jIcons.filter('.twopg').bind('click', function(e) {
+ self.switchMode(self.constMode2up);
+ });
- jToolbar.find('.book_left').click(function(e) {
+ jIcons.filter('.thumb').bind('click', function(e) {
+ self.switchMode(self.constModeThumb);
+ });
+
+ jIcons.filter('.fit').bind('fit', function(e) {
+ // XXXmang implement autofit zoom
+ });
+
+ jIcons.filter('.book_left').click(function(e) {
self.left();
return false;
});
- jToolbar.find('.book_right').click(function(e) {
+ jIcons.filter('.book_right').click(function(e) {
self.right();
return false;
});
- jToolbar.find('.book_up').bind('click', function(e) {
+ jIcons.filter('.book_up').bind('click', function(e) {
if ($.inArray(self.mode, [self.constMode1up, self.constModeThumb]) >= 0) {
self.scrollUp();
} else {
@@ -3449,7 +3812,7 @@ BookReader.prototype.bindToolbarNavHandlers = function(jToolbar) {
return false;
});
- jToolbar.find('.book_down').bind('click', function(e) {
+ jIcons.filter('.book_down').bind('click', function(e) {
if ($.inArray(self.mode, [self.constMode1up, self.constModeThumb]) >= 0) {
self.scrollDown();
} else {
@@ -3458,86 +3821,93 @@ BookReader.prototype.bindToolbarNavHandlers = function(jToolbar) {
return false;
});
- jToolbar.find('.print').click(function(e) {
+ jIcons.filter('.print').click(function(e) {
self.printPage();
return false;
});
- jToolbar.find('.embed').click(function(e) {
+ jIcons.filter('.embed').click(function(e) {
self.showEmbedCode();
return false;
});
- jToolbar.find('.bookmark').click(function(e) {
+ jIcons.filter('.bookmark').click(function(e) {
self.showBookmarkCode();
return false;
});
- jToolbar.find('.play').click(function(e) {
+ jIcons.filter('.play').click(function(e) {
self.autoToggle();
return false;
});
- jToolbar.find('.pause').click(function(e) {
+ jIcons.filter('.pause').click(function(e) {
self.autoToggle();
return false;
});
- jToolbar.find('.book_top').click(function(e) {
+ jIcons.filter('.book_top').click(function(e) {
self.first();
return false;
});
- jToolbar.find('.book_bottom').click(function(e) {
+ jIcons.filter('.book_bottom').click(function(e) {
self.last();
return false;
});
- jToolbar.find('.book_leftmost').click(function(e) {
+ jIcons.filter('.book_leftmost').click(function(e) {
self.leftmost();
return false;
});
- jToolbar.find('.book_rightmost').click(function(e) {
+ jIcons.filter('.book_rightmost').click(function(e) {
self.rightmost();
return false;
});
-}
-// updateToolbarZoom(reduce)
-//______________________________________________________________________________
-// Update the displayed zoom factor based on reduction factor
-BookReader.prototype.updateToolbarZoom = function(reduce) {
- var value;
- var autofit = null;
-
- // $$$ TODO preserve zoom/fit for each mode
- if (this.mode == this.constMode2up) {
- autofit = this.twoPage.autofit;
- } else {
- autofit = this.onePage.autofit;
- }
+ jIcons.filter('.read').click(function(e) {
+ self.ttsToggle();
+ return false;
+ });
- if (autofit) {
- value = autofit.slice(0,1).toUpperCase() + autofit.slice(1);
- } else {
- value = (100 / reduce).toFixed(2);
- // Strip trailing zeroes and decimal if all zeroes
- value = value.replace(/0+$/,'');
- value = value.replace(/\.$/,'');
- value += '%';
- }
- $('#BRzoom').text(value);
-}
+ jIcons.filter('.zoom_in').bind('click', function() {
+ self.ttsStop();
+ self.zoom(1);
+ return false;
+ });
+
+ jIcons.filter('.zoom_out').bind('click', function() {
+ self.ttsStop();
+ self.zoom(-1);
+ return false;
+ });
+
+ // XXX fix integration
+ $('#booksearch').bind('submit', function() {
+ self.search($('#textSrch').val());
+ });
-// bindNavigationHandlers
-//______________________________________________________________________________
-// Bind navigation handlers
-BookReader.prototype.bindNavigationHandlers = function() {
+ this.initSwipeData();
$('#BookReader').die('mousemove.navigation').live('mousemove.navigation',
{ 'br': this },
this.navigationMousemoveHandler
);
+
+ $('.BRpageimage').die('mousedown.swipe').live('mousedown.swipe',
+ { 'br': this },
+ this.swipeMousedownHandler
+ )
+ .die('mousemove.swipe').live('mousemove.swipe',
+ { 'br': this },
+ this.swipeMousemoveHandler
+ )
+ .die('mouseup.swipe').live('mouseup.swipe',
+ { 'br': this },
+ this.swipeMouseupHandler
+ );
+
+ this.bindMozTouchHandlers();
}
// unbindNavigationHandlers
@@ -3552,14 +3922,111 @@ BookReader.prototype.unbindNavigationHandlers = function() {
// Handle mousemove related to navigation. Bind at #BookReader level to allow autohide.
BookReader.prototype.navigationMousemoveHandler = function(event) {
// $$$ possibly not great to be calling this for every mousemove
- var navkey = $(document).height() - 75;
- if ((event.pageY < 76) || (event.pageY > navkey)) {
- // inside or near navigation elements
- event.data['br'].hideNavigation();
- } else {
- event.data['br'].showNavigation();
+
+ if (event.data['br'].uiAutoHide) {
+ var navkey = $(document).height() - 75;
+ if ((event.pageY < 76) || (event.pageY > navkey)) {
+ // inside or near navigation elements
+ event.data['br'].hideNavigation();
+ } else {
+ event.data['br'].showNavigation();
+ }
+ }
+}
+
+BookReader.prototype.initSwipeData = function(clientX, clientY) {
+ /*
+ * Based on the really quite awesome "Today's Guardian" at http://guardian.gyford.com/
+ */
+ this._swipe = {
+ mightBeSwiping: false,
+ startTime: (new Date).getTime(),
+ startX: clientX,
+ startY: clientY,
+ deltaX: 0,
+ deltaY: 0,
+ deltaT: 0
+ }
+}
+
+BookReader.prototype.swipeMousedownHandler = function(event) {
+ //console.log('swipe mousedown');
+ //console.log(event);
+
+ var self = event.data['br'];
+ self.initSwipeData(event.clientX, event.clientY);
+ self._swipe.mightBeSwiping = true;
+
+ // We should be the last bubble point for the page images
+ // Disable image drag and select, but keep right-click
+ if ($(event.originalTarget).hasClass('BRpageimage') && event.button != 2) {
+ event.preventDefault();
+ }
+}
+
+BookReader.prototype.swipeMousemoveHandler = function(event) {
+ //console.log('swipe move ' + event.clientX + ',' + event.clientY);
+
+ var _swipe = event.data['br']._swipe;
+ if (! _swipe.mightBeSwiping) {
+ return;
+ }
+
+ // Update swipe data
+ _swipe.deltaX = event.clientX - _swipe.startX;
+ _swipe.deltaY = event.clientY - _swipe.startY;
+ _swipe.deltaT = (new Date).getTime() - _swipe.startTime;
+
+ var absX = Math.abs(_swipe.deltaX);
+ var absY = Math.abs(_swipe.deltaY);
+
+ // Minimum distance in the amount of tim to trigger the swipe
+ var minSwipeLength = Math.max($('#BookReader').width() / 5, 100);
+ var maxSwipeTime = 1000;
+
+ // Check for horizontal swipe
+ if (absX > absY && (absX > minSwipeLength) && _swipe.deltaT < maxSwipeTime) {
+ //console.log('swipe! ' + _swipe.deltaX + ',' + _swipe.deltaY + ' ' + _swipe.deltaT + 'ms');
+
+ _swipe.mightBeSwiping = false; // only trigger once
+ if (event.data['br'].mode == event.data['br'].constMode2up) {
+ if (_swipe.deltaX < 0) {
+ event.data['br'].right();
+ } else {
+ event.data['br'].left();
+ }
+ }
}
}
+BookReader.prototype.swipeMouseupHandler = function(event) {
+ //console.log('swipe mouseup');
+ //console.log(event);
+ event.data['br']._swipe.mightBeSwiping = false;
+}
+
+BookReader.prototype.bindMozTouchHandlers = function() {
+ var self = this;
+
+ // Currently only want touch handlers in 2up
+ $('#BookReader').bind('MozTouchDown', function(event) {
+ //console.log('MozTouchDown ' + event.streamId + ' ' + event.clientX + ',' + event.clientY);
+ if (this.mode == this.constMode2up) {
+ event.preventDefault();
+ }
+ })
+ .bind('MozTouchMove', function(event) {
+ //console.log('MozTouchMove - ' + event.streamId + ' ' + event.clientX + ',' + event.clientY)
+ if (this.mode == this.constMode2up) {
+ event.preventDefault();
+ }
+ })
+ .bind('MozTouchUp', function(event) {
+ //console.log('MozTouchUp - ' + event.streamId + ' ' + event.clientX + ',' + event.clientY);
+ if (this.mode = this.constMode2up) {
+ event.preventDefault();
+ }
+ });
+}
// navigationIsVisible
//______________________________________________________________________________
@@ -3583,6 +4050,7 @@ BookReader.prototype.hideNavigation = function() {
// $$$ don't hardcode height
$('#BRtoolbar').animate({top:-60});
$('#BRnav').animate({bottom:-60});
+ //$('#BRzoomer').animate({right:-26});
}
}
@@ -3594,6 +4062,7 @@ BookReader.prototype.showNavigation = function() {
if (!this.navigationIsVisible()) {
$('#BRtoolbar').animate({top:0});
$('#BRnav').animate({bottom:0});
+ //$('#BRzoomer').animate({right:0});
}
}
@@ -3909,6 +4378,8 @@ BookReader.prototype.startLocationPolling = function() {
if (newHash != self.oldUserHash) { // Only process new user hash once
//console.log('url change detected ' + self.oldLocationHash + " -> " + newHash);
+ self.ttsStop();
+
// Queue change if animating
if (self.animating) {
self.autoStop();
@@ -3943,17 +4414,31 @@ BookReader.prototype.canSwitchToMode = function(mode) {
// searchHighlightVisible
//________
// Returns true if a search highlight is currently being displayed
-BookReader.prototype.searchHighlightVisible = function() {
+BookReader.prototype.searchHighlightVisible = function() {
+ var results = this.searchResults;
+ if (null == results) return false;
+
if (this.constMode2up == this.mode) {
- if (this.searchResults[this.twoPage.currentIndexL]
- || this.searchResults[this.twoPage.currentIndexR]) {
- return true;
- }
- } else { // 1up
- if (this.searchResults[this.currentIndex()]) {
- return true;
+ var visiblePages = Array(this.twoPage.currentIndexL, this.twoPage.currentIndexR);
+ } else if (this.constMode1up == this.mode) {
+ var visiblePages = Array();
+ visiblePages[0] = this.currentIndex();
+ } else {
+ return false;
+ }
+
+ var i, j;
+ for (i=0; i
= 0) {
+ return true;
+ }
}
}
+
return false;
}
@@ -3981,7 +4466,7 @@ BookReader.prototype._getPageHeight= function(index) {
// Returns the page URI or transparent image if out of range
BookReader.prototype._getPageURI = function(index, reduce, rotate) {
if (index < 0 || index >= this.numLeafs) { // Synthesize page
- return this.imagesBaseURL + "/transparent.png";
+ return this.imagesBaseURL + "transparent.png";
}
if ('undefined' == typeof(reduce)) {
@@ -4009,6 +4494,23 @@ BookReader.prototype._getPageURI = function(index, reduce, rotate) {
return this.getPageURI(index, reduce, rotate);
}
+/*
+ * Update based on received record from Open Library.
+ */
+BookReader.prototype.gotOpenLibraryRecord = function(self, olObject) {
+ // $$$ could refactor this so that 'this' is available
+ if (olObject) {
+ if (olObject['table_of_contents']) {
+ // XXX check here that TOC is valid
+ self.updateTOC(olObject['table_of_contents']);
+ }
+ }
+
+ // $$$mang cleanup
+ $('#BRreturn a').attr('href', 'http://openlibrary.org' + olObject.key);
+
+}
+
// Library functions
BookReader.util = {
disableSelect: function(jObject) {
@@ -4027,6 +4529,11 @@ BookReader.util = {
return Math.min(Math.max(value, min), max);
},
+ // Given value and maximum, calculate a percentage suitable for CSS
+ cssPercentage: function(value, max) {
+ return (((value + 0.0) / max) * 100) + '%';
+ },
+
notInArray: function(value, array) {
// inArray returns -1 or undefined if value not in array
return ! (jQuery.inArray(value, array) >= 0);
@@ -4049,3 +4556,480 @@ BookReader.util = {
}
// The final property here must NOT have a comma after it - IE7
}
+
+
+// ttsToggle()
+//______________________________________________________________________________
+BookReader.prototype.ttsToggle = function () {
+
+ this.autoStop();
+
+ if (false == this.ttsPlaying) {
+ this.ttsPlaying = true;
+ this.showProgressPopup('Loading audio...');
+ if(soundManager.supported()) {
+ this.ttsStart();
+ } else {
+ soundManager.onready(function(oStatus) {
+ if (oStatus.success) {
+ this.ttsStart();
+ } else {
+ alert('Could not load soundManager2, possibly due to FlashBlock. Audio playback is disabled');
+ }
+ }, this);
+ }
+ } else {
+ this.ttsStop();
+ }
+}
+
+// ttsStart()
+//______________________________________________________________________________
+BookReader.prototype.ttsStart = function () {
+ if (soundManager.debugMode) console.log('starting readAloud');
+ if (this.constModeThumb == this.mode) this.switchMode(this.constMode1up);
+
+ //this.ttsPlaying = true; //set this in ttsToggle()
+ this.ttsIndex = this.currentIndex();
+ this.ttsFormat = 'mp3';
+ if ($.browser.mozilla) {
+ this.ttsFormat = 'ogg';
+ }
+ this.ttsGetText(this.ttsIndex, 'ttsStartCB');
+}
+
+// ttsStop()
+//______________________________________________________________________________
+BookReader.prototype.ttsStop = function () {
+ if (false == this.ttsPlaying) return;
+
+ if (soundManager.debugMode) console.log('stopping readaloud');
+ soundManager.stopAll();
+ soundManager.destroySound('chunk'+this.ttsIndex+'-'+this.ttsPosition);
+ this.ttsRemoveHilites();
+ this.removeProgressPopup();
+
+ this.ttsPlaying = false;
+ this.ttsIndex = null; //leaf index
+ this.ttsPosition = -1; //chunk (paragraph) number
+ this.ttsBuffering = false;
+ this.ttsPoller = null;
+}
+
+// ttsGetText()
+//______________________________________________________________________________
+BookReader.prototype.ttsGetText = function(index, callback) {
+ var url = 'http://'+this.server+'/BookReader/BookReaderGetTextWrapper.php?path='+this.bookPath+'_djvu.xml&page='+index;
+ this.ttsAjax = $.ajax({url:url, dataType:'jsonp', jsonpCallback:callback});
+}
+
+// ttsStartCB(): text-to-speech callback
+//______________________________________________________________________________
+BookReader.prototype.ttsStartCB = function (data) {
+ if (soundManager.debugMode) console.log('ttsStartCB got data: ' + data);
+ this.ttsChunks = data;
+ this.ttsHilites = [];
+
+ //deal with the page being blank
+ if (0 == data.length) {
+ if (soundManager.debugMode) console.log('first page is blank!');
+ if(this.ttsAdvance(true)) {
+ this.ttsGetText(this.ttsIndex, 'ttsStartCB');
+ }
+ return;
+ }
+
+ this.showProgressPopup('Loading audio...');
+
+ ///// whileloading: broken on safari
+ ///// onload fires on safari, but *after* the sound starts playing..
+ this.ttsPosition = -1;
+ var snd = soundManager.createSound({
+ id: 'chunk'+this.ttsIndex+'-0',
+ //url: 'http://home.us.archive.org/~rkumar/arctic.ogg',
+ url: 'http://'+this.server+'/BookReader/BookReaderGetTTS.php?string=' + escape(data[0][0]) + '&format=.'+this.ttsFormat, //the .ogg is to trick SoundManager2 to use the HTML5 audio player
+ whileloading: function(){if (this.bytesLoaded == this.bytesTotal) this.br.removeProgressPopup();}, //onload never fires in FF...
+ onload: function(){this.br.removeProgressPopup();} //whileloading never fires in safari...
+ });
+ snd.br = this;
+ snd.load();
+
+ this.ttsNextChunk();
+}
+
+// showProgressPopup
+//______________________________________________________________________________
+BookReader.prototype.showProgressPopup = function(msg) {
+ if (soundManager.debugMode) console.log('showProgressPopup index='+this.ttsIndex+' pos='+this.ttsPosition);
+ if (this.popup) return;
+
+ this.popup = document.createElement("div");
+ $(this.popup).css({
+ top: ($('#BookReader').height()*0.5-100) + 'px',
+ left: ($('#BookReader').width()-300)*0.5 + 'px'
+ }).attr('className', 'BRprogresspopup');
+
+ var bar = document.createElement("div");
+ $(bar).css({
+ height: '20px'
+ }).attr('className', 'BRprogressbar');
+ $(this.popup).append(bar);
+
+ if (msg) {
+ var msgdiv = document.createElement("div");
+ msgdiv.innerHTML = msg;
+ $(this.popup).append(msgdiv);
+ }
+
+ $(this.popup).appendTo('#BookReader');
+}
+
+// removeProgressPopup
+//______________________________________________________________________________
+BookReader.prototype.removeProgressPopup = function() {
+ $(this.popup).remove();
+ this.popup=null;
+}
+
+// ttsNextPageCB
+//______________________________________________________________________________
+BookReader.prototype.ttsNextPageCB = function (data) {
+ this.ttsNextChunks = data;
+ if (soundManager.debugMode) console.log('preloaded next chunks.. data is ' + data);
+
+ if (true == this.ttsBuffering) {
+ if (soundManager.debugMode) console.log('ttsNextPageCB: ttsBuffering is true');
+ this.ttsBuffering = false;
+ }
+}
+
+// ttsLoadChunk
+//______________________________________________________________________________
+BookReader.prototype.ttsLoadChunk = function (page, pos, string) {
+ var snd = soundManager.createSound({
+ id: 'chunk'+page+'-'+pos,
+ url: 'http://'+this.server+'/BookReader/BookReaderGetTTS.php?string=' + escape(string) + '&format=.'+this.ttsFormat //the .ogg is to trick SoundManager2 to use the HTML5 audio player
+ });
+ snd.br = this;
+ snd.load()
+}
+
+
+// ttsNextChunk()
+//______________________________________________________________________________
+// This function into two parts: ttsNextChunk gets run before page flip animation
+// and ttsNextChunkPhase2 get run after page flip animation.
+// If a page flip is necessary, ttsAdvance() will return false so Phase2 isn't
+// called. Instead, this.animationFinishedCallback is set, so that Phase2
+// continues after animation is finished.
+
+BookReader.prototype.ttsNextChunk = function () {
+ if (soundManager.debugMode) console.log('nextchunk pos=' + this.ttsPosition);
+
+ if (-1 != this.ttsPosition) {
+ soundManager.destroySound('chunk'+this.ttsIndex+'-'+this.ttsPosition);
+ }
+
+ this.ttsRemoveHilites(); //remove old hilights
+
+ var moreToPlay = this.ttsAdvance();
+
+ if (moreToPlay) {
+ this.ttsNextChunkPhase2();
+ }
+
+ //This function is called again when ttsPlay() has finished playback.
+ //If the next chunk of text has not yet finished loading, ttsPlay()
+ //will start polling until the next chunk is ready.
+}
+
+// ttsNextChunkPhase2()
+//______________________________________________________________________________
+// page flip animation has now completed
+BookReader.prototype.ttsNextChunkPhase2 = function () {
+ if (null == this.ttsChunks) {
+ alert('error: ttsChunks is null?'); //TODO
+ return;
+ }
+
+ if (0 == this.ttsChunks.length) {
+ if (soundManager.debugMode) console.log('ttsNextChunk2: ttsChunks.length is zero.. hacking...');
+ this.ttsStartCB(this.ttsChunks);
+ return;
+ }
+
+ if (soundManager.debugMode) console.log('next chunk is ' + this.ttsPosition);
+
+ //prefetch next page of text
+ if (0 == this.ttsPosition) {
+ if (this.ttsIndex<(this.numLeafs-1)) {
+ this.ttsGetText(this.ttsIndex+1, 'ttsNextPageCB');
+ }
+ }
+
+ this.ttsPrefetchAudio();
+
+ this.ttsPlay();
+}
+
+// ttsAdvance()
+//______________________________________________________________________________
+// 1. advance ttsPosition
+// 2. if necessary, advance ttsIndex, and copy ttsNextChunks to ttsChunks
+// 3. if necessary, flip to current page, or scroll so chunk is visible
+// 4. do something smart is ttsNextChunks has not yet finished preloading (TODO)
+// 5. stop playing at end of book
+
+BookReader.prototype.ttsAdvance = function (starting) {
+ this.ttsPosition++;
+
+ if (this.ttsPosition >= this.ttsChunks.length) {
+
+ if (this.ttsIndex == (this.numLeafs-1)) {
+ if (soundManager.debugMode) console.log('tts stop');
+ return false;
+ } else {
+ if ((null != this.ttsNextChunks) || (starting)) {
+ if (soundManager.debugMode) console.log('moving to next page!');
+ this.ttsIndex++;
+ this.ttsPosition = 0;
+ this.ttsChunks = this.ttsNextChunks;
+ this.ttsNextChunks = null;
+
+ //A page flip might be necessary. This code is confusing since
+ //ttsNextChunks might be null if we are starting on a blank page.
+ if (2 == this.mode) {
+ if ((this.ttsIndex != this.twoPage.currentIndexL) && (this.ttsIndex != this.twoPage.currentIndexR)) {
+ if (!starting) {
+ this.animationFinishedCallback = this.ttsNextChunkPhase2;
+ this.next();
+ return false;
+ } else {
+ this.next();
+ return true;
+ }
+ } else {
+ return true;
+ }
+ }
+ } else {
+ if (soundManager.debugMode) console.log('ttsAdvance: ttsNextChunks is null');
+ return false;
+ }
+ }
+ }
+
+ return true;
+}
+
+// ttsPrefetchAudio()
+//______________________________________________________________________________
+BookReader.prototype.ttsPrefetchAudio = function () {
+
+ if(false != this.ttsBuffering) {
+ alert('TTS Error: prefetch() called while content still buffering!');
+ return;
+ }
+
+ //preload next chunk
+ var nextPos = this.ttsPosition+1;
+ if (nextPos < this.ttsChunks.length) {
+ this.ttsLoadChunk(this.ttsIndex, nextPos, this.ttsChunks[nextPos][0]);
+ } else {
+ //for a short page, preload might nt have yet returned..
+ if (soundManager.debugMode) console.log('preloading chunk 0 from next page, index='+(this.ttsIndex+1));
+ if (null != this.ttsNextChunks) {
+ if (0 != this.ttsNextChunks.length) {
+ this.ttsLoadChunk(this.ttsIndex+1, 0, this.ttsNextChunks[0][0]);
+ } else {
+ if (soundManager.debugMode) console.log('prefetchAudio(): ttsNextChunks is zero length!');
+ }
+ } else {
+ if (soundManager.debugMode) console.log('ttsNextChunks is null, not preloading next page');
+ this.ttsBuffering = true;
+ }
+ }
+
+}
+
+// ttsPlay()
+//______________________________________________________________________________
+BookReader.prototype.ttsPlay = function () {
+
+ var chunk = this.ttsChunks[this.ttsPosition];
+ if (soundManager.debugMode) {
+ console.log('ttsPlay position = ' + this.ttsPosition);
+ console.log('chunk = ' + chunk);
+ console.log(this.ttsChunks);
+ }
+
+ //add new hilights
+ if (2 == this.mode) {
+ this.ttsHilite2UP(chunk);
+ } else {
+ this.ttsHilite1UP(chunk);
+ }
+
+ this.ttsScrollToChunk(chunk);
+
+ //play current chunk
+ if (false == this.ttsBuffering) {
+ soundManager.play('chunk'+this.ttsIndex+'-'+this.ttsPosition,{onfinish:function(){br.ttsNextChunk();}});
+ } else {
+ soundManager.play('chunk'+this.ttsIndex+'-'+this.ttsPosition,{onfinish:function(){br.ttsStartPolling();}});
+ }
+}
+
+// scrollToChunk()
+//______________________________________________________________________________
+BookReader.prototype.ttsScrollToChunk = function(chunk) {
+ if (this.constMode1up != this.mode) return;
+
+ var leafTop = 0;
+ var h;
+ var i;
+ for (i=0; i