var book;

$(document).ready(function () {

    //page nav click events
    $("#pageSheet2, .pageNavNext").click(function () { nextPage(true); });
    $("#pageSheet1, .pageNavPrev").click(function () { prevPage(true) });

});

function bookViewerSize() {
    var bookHeight = $(".viewer").height() - 30;
    var bookWidth = $(".viewer").width() - 30;

    $(".bookContainer").height(bookHeight);
    $(".pageContainer").height(bookHeight);
    $("#page1a, #page2a")
        .height(bookHeight - 70);
    $(".pageSheet")
        .height(bookHeight - 40)
        .width((bookWidth / 2) - 40);
    $(".pageNavPrev")
        .css("padding-top", bookHeight/2)
        .height(bookHeight/2);
    $(".pageNavNext")
        .css("padding-top", bookHeight/2)
        .height(bookHeight/2);
}

function bookViewerOpen() {
    viewerOpen();
    bookViewerSize();
    $(".bookViewer").fadeIn(500);
    currentPage = -1;
    nextPage(false);
}

function bookViewerClose() {
    if ($(".bookViewer").is(":visible")) $(".bookViewer").fadeOut(500);
}

function nextPage(transition) {
    if (book[parseInt(currentPage) + 1] != undefined) {

        currentPage += 1;
        $(".bookTitle").html(book[currentPage].title);
        $(".bookSubTitle").html(book[currentPage].subTitle);
        pageAdd(book[currentPage].page1Text, book[currentPage].page2Text);
        pageChange(transition);

        pageNavShow();
    }
}

function prevPage(transition) {
    if (parseInt(currentPage) > 0) {
        currentPage += -1;

        currentDirection = "right";
        $(".bookTitle").html(book[currentPage].title);
        $(".bookSubTitle").html(book[currentPage].subTitle);
        pageAdd(book[currentPage].page1Text, book[currentPage].page2Text);
        pageChange(transition);
        currentDirection = "left";

        pageNavShow();
    }
}

function pageAdd(page1Content, page2Content) {
    $("#page1a").html($("#page1b").html()).show();
    $("#page2a").html($("#page2b").html()).show();
    $("#page1b").html(page1Content);
    $("#page2b").html(page2Content);
}

function pageChange(transition) {
    if (transition) {
        if (currentDirection == "left") {
            $("#page1a").delay(500).hide("slide", { direction: currentDirection }, 500);
            $("#page2a").hide("slide", { direction: currentDirection }, 500);
        }
        else {
            $("#page1a").hide("slide", { direction: currentDirection }, 500);
            $("#page2a").delay(500).hide("slide", { direction: currentDirection }, 500);
        }
    } else {
        $("#page1a").hide();
        $("#page2a").hide();
    }
}

function pageNavShow() {
	$(".pageNavNext").show().html("&gt;");
	$(".pageNavPrev").show().html("&lt;");
	if (book[parseInt(currentPage) + 1] == undefined) $(".pageNavNext").html("");
	if (parseInt(currentPage) <= 0) $(".pageNavPrev").html("");
}

function bookGet() {
    $.ajax({
        type: "POST",
        url: "default.aspx/bookGet",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            //set array for common use
            book = response.d;
            //display thumbnails
            bookViewerOpen();
        },
        failure: function (msg) {
            alert(msg);
        }
    });
}

