var page = (function() { var $container = $( '#container' ), $bookblock = $( '#bb-bookblock' ), $items = $bookblock.children(), itemscount = $items.length, current = 0, bb = $( '#bb-bookblock' ).bookblock( { speed : 800, perspective : 2000, shadowsides : 0.8, shadowflip : 0.4, onendflip : function(old, page, islimit) { current = page; // update toc current updatetoc(); // updatenavigation updatenavigation( islimit ); // initialize jscrollpane on the content div for the new item setjsp( 'init' ); // destroy jscrollpane on the content div for the old item setjsp( 'destroy', old ); } } ), $navnext = $( '#bb-nav-next' ), $navprev = $( '#bb-nav-prev' ).hide(), $menuitems = $container.find( 'ul.menu-toc > li' ), $tblcontents = $( '#tblcontents' ), transendeventnames = { 'webkittransition': 'webkittransitionend', 'moztransition': 'transitionend', 'otransition': 'otransitionend', 'mstransition': 'mstransitionend', 'transition': 'transitionend' }, transendeventname = transendeventnames[modernizr.prefixed('transition')], supporttransitions = modernizr.csstransitions; function init() { // initialize jscrollpane on the content div of the first item setjsp( 'init' ); initevents(); } function initevents() { // add navigation events $navnext.on( 'click', function() { bb.next(); return false; } ); $navprev.on( 'click', function() { bb.prev(); return false; } ); // add swipe events $items.on( { 'swipeleft' : function( event ) { if( $container.data( 'opened' ) ) { return false; } bb.next(); return false; }, 'swiperight' : function( event ) { if( $container.data( 'opened' ) ) { return false; } bb.prev(); return false; } } ); // show table of contents $tblcontents.on( 'click', toggletoc ); // click a menu item $menuitems.on( 'click', function() { var $el = $( this ), idx = $el.index(), jump = function() { bb.jump( idx + 1 ); }; current !== idx ? closetoc( jump ) : closetoc(); return false; } ); // reinit jscrollpane on window resize $( window ).on( 'debouncedresize', function() { // reinitialise jscrollpane on the content div setjsp( 'reinit' ); } ); } function setjsp( action, idx ) { var idx = idx === undefined ? current : idx, $content = $items.eq( idx ).children( 'div.content' ), apijsp = $content.data( 'jsp' ); if( action === 'init' && apijsp === undefined ) { $content.jscrollpane({verticalgutter : 0, hidefocus : true }); } else if( action === 'reinit' && apijsp !== undefined ) { apijsp.reinitialise(); } else if( action === 'destroy' && apijsp !== undefined ) { apijsp.destroy(); } } function updatetoc() { $menuitems.removeclass( 'menu-toc-current' ).eq( current ).addclass( 'menu-toc-current' ); } function updatenavigation( islastpage ) { if( current === 0 ) { $navnext.show(); $navprev.hide(); } else if( islastpage ) { $navnext.hide(); $navprev.show(); } else { $navnext.show(); $navprev.show(); } } function toggletoc() { var opened = $container.data( 'opened' ); opened ? closetoc() : opentoc(); } function opentoc() { $navnext.hide(); $navprev.hide(); $container.addclass( 'slideright' ).data( 'opened', true ); } function closetoc( callback ) { updatenavigation( current === itemscount - 1 ); $container.removeclass( 'slideright' ).data( 'opened', false ); if( callback ) { if( supporttransitions ) { $container.on( transendeventname, function() { $( this ).off( transendeventname ); callback.call(); } ); } else { callback.call(); } } } return { init : init }; })();