﻿$(function() {
    if ($("#picbox").length > 0) {
        onLoadModel();
    }
});


function checkWindow() {
    var ww = $(window).width();
    var wh = $(window).height();
    return [ww, wh];
}

function getContentHeight(wind) {
    return wind[1] - 280;
}

function getPictureHeight(wind) {
    return wind[1] - 300;
}

function getComputedWidth(h1, h2, w) {
    var ratio = h2 / h1;
    return Math.floor(w * ratio);
}

function onLoadModel() {
    $(".mrhide").hide();
    var windim = checkWindow();
    $(".content").height(getContentHeight(windim));
    
    
    var bookSelector = function() {
        if ($(".booktitles a").length > 1) {
            $(".actualbook").html($(".booktitles a").eq(0).html()).show();
            $(".booktitles a").each(function(i, item) {
                $(item).click(function() {
                    $(".actualbook").html($(item).html());
                    onLoadBook($("ul.thumbs").eq(i).find("li"), windim);
                    $(".actualbook").click();
                })
            });
            $(".actualbook").toggle(function() {
                $(".booktitles").show("slow");
                $(".actualbook").addClass("unfolded");
            }, function() {
                $(".booktitles").hide("slow");
                $(".actualbook").removeClass("unfolded");
            });
        } else {
            $(".booktitles").hide();
            $(".actualbook").html($(".booktitles a").eq(0).html()).hide();
            $(".actualbook").addClass("no-folded");
        }
    }
    
    bookSelector();
    var li_selected = $("ul.thumbs:eq(0)>li");
    onLoadBook(li_selected, windim);
}


function onLoadBook(lis, windim) {
    
    $("#picbox").show();
    var imgpath = "";
    var imgpath2 = "";
    var page = 0;

    var ulid = $(lis).parent().attr("id")
    var idbook = Number(ulid.substring(1));
    $("#printbook").find("a").attr("href","/pdf/" + idbook)

    var goToPage = function(pn) {
        if (pn >= $(lis).length) {
            page = 0;
        } else if (pn < 0) {
            page = ($(lis).length - 1);
        } else {
            page = pn;
        }
        setPage();
    };

    var updateButtons = function() {
        $("a.nav").unbind("click");
        $("a#npic").click(function() {
            goToPage(page + 1);
        });
        $("a#lpic").click(function() {
            goToPage(page - 1);
        });
    }

    var setPage = function() {
        clear();
        litoload = $(lis).eq(page);
        loadImages();
        updateButtons();
        $("#thumbstripe div img").removeClass("selected")
        $("#thumbstripe div img[title=p" + page + "]").addClass("selected");
        if (isIndexOpen) scrollThumbs();
    }

    var clear = function() {
        $('#picbox div').empty().addClass("loading");
    }

    var loadThumbs = function() {
        $("#thumbstripe div").empty();

        var stripewidth = 0;
        $(lis).each(function(i, item) {
            if ($(item).find("a").eq(0).length > 0) {
                $(item).find("a").eq(0).children("img").clone().appendTo("#thumbstripe div").attr("title", "p" + i).click(function() { goToPage(i) });
            }
            if ($(item).find("a").eq(1).length > 0) {
                $(item).find("a").eq(1).children("img").clone().appendTo("#thumbstripe div").attr("title", "p" + i).click(function() { goToPage(i) });
            }
            stripewidth += 270;
        });

        $("#thumbstripe div").width(stripewidth);

        $("#thumbnav a:eq(0)").click(function() {
            $("#thumbstripe").scrollTo('-=400px', 500);
        });
        $("#thumbnav a:eq(1)").click(function() {
            hideThumbs();
        });
        $("#thumbnav a:eq(2)").click(function() {
            $("#thumbstripe").scrollTo('+=400px', 500);
        });

        $("#showthumbs a").click(function() {
            showThumbs();
        });
    }

    var showThumbs = function() {
        $("#thumbstripe").slideDown("slow");
        $("#thumbnav").slideDown("slow");
        isIndexOpen = true;
        scrollThumbs();
        $("#footer").hide();
    }

    var scrollThumbs = function() {
        var goscroll = (page * 220) - 300
        if (goscroll > 0) {
            if (goscroll > $("#thumbstripe div").width()) {
                $("#thumbstripe").scrollTo($("#thumbstripe div").width(), 800);
            } else {
                $("#thumbstripe").scrollTo(goscroll, 800);
            }
        } else {
            $("#thumbstripe").scrollTo(0, 800);
        }
    }

    var hideThumbs = function() {
        $("#thumbstripe").slideUp("slow");
        $("#thumbnav").slideUp("slow");
        isIndexOpen = false;
        $("#footer").show();
    }

    var loadImages = function() {
        var maxheight = 0;
        if ($(litoload).find("a").eq(0).length > 0) {
            imgpath = $(litoload).find("a").eq(0).attr("href");
            var img1 = new Image();
            $(img1)
                    .load(function() {
                        var img1h = img1.height;

                        var img1w = img1.width;
                        $(img1).hide();
                        if (img1h > getPictureHeight(windim)) {
                            var img1hc = getPictureHeight(windim)
                            maxheight = img1hc;
                            $(img1).height(img1hc);
                            img1w = getComputedWidth(img1h, img1hc, img1w);
                        } else {
                            maxheight = img1h;
                        }
                        $(img1).width(img1w);

                        if ($(litoload).find("a").eq(1).length > 0) {
                            imgpath2 = $(litoload).find("a").eq(1).attr("href");
                            var img2 = new Image();
                            $(img2)
                            .load(function() {
                                var img2h = img2.height;
                                var img2w = img2.width;
                                $(img2).hide();


                                if (img2h > getPictureHeight(windim)) {
                                    var img2hc = getPictureHeight(windim)
                                    $(img2).height(img2hc);
                                    img2w = getComputedWidth(img2h, img2hc, img2w);
                                    if (img2hc > maxheight) {
                                        maxheight = img2hc;
                                    }
                                } else {
                                    if (img2h > maxheight) {
                                        maxheight = img2h;
                                    }
                                }

                                if (img1h < getPictureHeight(windim)) {
                                    var mt = Math.floor((getPictureHeight(windim) - img1h) / 2);
                                    $(img1).css("margin-top", mt + "px");
                                }
                                if (img2h < getPictureHeight(windim)) {
                                    var mt = Math.floor((getPictureHeight(windim) - img2h) / 2);
                                    $(img2).css("margin-top", mt + "px");
                                }
                                $(img2).width(img2w);
                                var totw = img1w + img2w;

                                if (totw > (windim[0] - 40)) {
                                    //le immagini sono troppo larghe
                                }
                                $("#picbox").width(totw + 10);

                                $("#lpic").css("top", (Math.floor(maxheight / 2) - 10) + "px");
                                $("#npic").css("top", (Math.floor(maxheight / 2) - 10) + "px");

                                showPicture(1, img1);
                                showPicture(2, img2);
                            })
                            .error(function() {
                                alert("can't load");
                            })
                            .attr('src', imgpath2);
                        } else {
                            $('#picbox div:eq(1)').removeClass('loading').addClass('empty').height(maxheight);
                            var totw = img1w;
                            $("#picbox").width(totw + 10);

                            $("#lpic").css("top", (Math.floor(maxheight / 2) - 10) + "px");
                            $("#npic").css("top", (Math.floor(maxheight / 2) - 10) + "px");

                            showPicture(1, img1);
                        }

                    })
                    .error(function() {
                        alert("can't load");
                    })
                    .attr('src', imgpath);
        } else {
            $('#picbox div:eq(0)').removeClass('loading').addClass('empty');
        }

    };

    var showPicture = function(n, im) {
        var onclickpicture = null;
        if (n == 1) {
            onclickpicture = function() { goToPage(page - 1); }
        } else {
            onclickpicture = function() { goToPage(page + 1); }
        }

        $('#picbox div:eq(' + (n - 1) + ')')
                .removeClass('loading')
                .append(im)
                .unbind("click")
                .click(function() { onclickpicture(); });
        $(im).fadeIn();
    }

    hideThumbs();
    var isIndexOpen = false;
    
    loadThumbs();
    setPage();
}
