var FPR = FPR || {};
FPR.app = {};
FPR.app.init = (function($){
"use strict";
var Book,
offline = false,
sidebarWidth = 0,
windowWidth;
function init(bookURL){
var search = window.location.search.match(/book=(.*)/),
bookURL = bookURL || (search ? search[1] : "moby-dick");
//-- Setup the browser prefixes
FP.core.crossBrowserColumnCss();
//-- Set up our sidebar
windowWidth = $(window).width();
if(windowWidth > 550){
$("#main").width(windowWidth-sidebarWidth);
}else{
$("#main").width(windowWidth);
}
loadSettings();
//-- Create a new book object,
// this will create an iframe in the el with the ID provided
Book = new FP.Book("area");
//-- Add listeners to handle book events
//-- Full list of event are at start of book.js
Book.listen("book:metadataReady", meta);
Book.listen("book:tocReady", toc);
Book.listen("book:bookReady", bookReady);
Book.listen("book:chapterReady", chapterChange);
Book.listen("book:online", goOnline);
Book.listen("book:offline", goOffline);
//Book.registerHook("beforeChapterDisplay", FP.Hooks.transculsions.insert);
//-- Start loading / parsing of the book.
// This must be done AFTER adding listeners or hooks
Book.start(bookURL);
//-- Wait for Dom ready to handle jquery
$(function() {
controls();
});
}
function meta(){
var title = Book.getTitle(),
author = Book.getCreator(),
$title = $("#book-title"),
$author = $("#chapter-title"),
$dash = $("#title-seperator");
document.title = title+" – "+author;
$title.html(title);
$author.html(author);
$dash.show();
}
function toc(){
var contents = Book.getTOC(),
$toc = $("#toc"),
$links,
$items;
$toc.empty();
//-- Recursively generate TOC levels
$items = generateTocItems(contents, 1);
$toc.append($items);
$links = $(".toc_link");
$links.on("click", function(e){
var $this = $(this),
url = $this.data("url");
$(".openChapter").removeClass("openChapter");
$this.parents('li').addClass("openChapter");
//-- Provide the Book with the url to show
// The Url must be found in the books manifest
if(!Book.useHash){
Book.show(url);
e.preventDefault();
}
});
}
function loadSettings() {
var userFont = "";
if (!localStorage.getItem("fontSize")) {
userFont = "medium";
localStorage.setItem("fontSize", userFont);
} else {
userFont = localStorage.getItem("fontSize");
}
var $settings = $("#settingsPanel");
$settings.append("
");
var $settingsItem = $("
");
var $fontSizes = $("Extra Small " +
"Small " +
"Medium " +
"Large " +
"Extra Large");
$settingsItem.find("h3").text('Font Size').after($fontSizes);
$settings.find("ul").append($settingsItem);
var $fontSizeButtons = $('input[name="fontSize"]');
$fontSizeButtons.each(function() {
if ($(this).attr("value") == userFont) {
$(this).attr("checked", "checked");
}
$(this).on("click", function() {
localStorage.setItem("fontSize", $(this).attr("value"));
});
});
}
function generateTocItems(contents, level){
var $container = $("
");
var type = (level == 1) ? "chapter" : "section";
contents.forEach(function(item){
var $subitems,
$wrapper = $("