﻿//funzione caricamento immagini ricorsiva
/*
function LoadImageBook(index, max, thisbook) {
    if (index < max && currentbook == thisbook) {
        var img = new Image();
        $(img).load(function () {
            if (currentbook == thisbook) {
                //alert(images[index]);
                var list = $("<li></li>").attr("class", "loading");
                $("ul.book-pictures").append(list);
                $(this).css("display", "none");
                $(this).attr("width", sizes[index][0]);
                $(this).attr("height", sizes[index][1]);
                $(list).append(this);
                $(list).removeClass("loading");
                $(this).fadeIn("fast", function () {
                    LoadImageBook(index + 1, max, thisbook);
                });
            }
        }).error(function () {
            $(list).remove();
            LoadImageBook(index + 1, max, thisbook);
        }).attr("src", images[index]);
    }
}
*/

var currentbook = 0;
var imgs = [];
var imageItem, list, img;
var so = null;
var stop = false;
var stoprecursion = false;
var nvolte = 0;
var isrecurring = false;

function model_onload() {
    //bindo i link del cambio book
    $("a.book-selector-link").unbind("click").click(function () {
        var idb = Number($(this).attr("id").substring(2));
        if (idb != currentbook) {
            stoprecursion = true;
            nvolte = 0
            setTimeout(function () { checkTimeout(idb); }, 200);
        }
    });
    $("a.vbook-selector-link").unbind("click").click(function () {
        var idb = Number($(this).attr("id").substring(2));
        var titb = $(this).html();
        stoprecursion = true;
        nvolte = 0
        setTimeout(function () { checkTimeoutVid(idb, titb); }, 200);
        
    });

    book_onload();
}

function checkTimeout(idb) {
    if (nvolte == 10 || isrecurring == false) {
        book_onload(idb);
    } else {
        nvolte++;
        setTimeout(function () { checkTimeout(idb); }, 200);
    }
}
function checkTimeoutVid(idb,titb) {
    if (nvolte == 10 || isrecurring == false) {
        vbook_onload(idb, titb);
    } else {
        nvolte++;
        setTimeout(function () { checkTimeoutVid(idb); }, 200);
    }
}


function book_onload(ulvar) {
    stoprecursion = false;
    var ulbook = null;
    switch (typeof ulvar) {
        case null:
        case "undefined":
            ulbook = $("#picbox ul:first");
            break;
        case "object":
            ulbook = ulvar;
            break;
        case "number":
        case "string":
            ulbook = $("#picbox ul[id='b" + ulvar + "']");
            break;
        default:
            ulbook = $("#picbox ul:first");
            break;
    }
    var page = 0;
    var npages = $(ulbook).children("li").length;
    var idbook = Number($(ulbook).attr("id").substring(1));

    
    currentbook = idbook;
    $("#book-viewer").smoothDivScroll("destroy");
    $("#book-viewer").html('<div class="scrollingHotSpotLeft"></div><div class="scrollingHotSpotRight"></div><div class="scrollWrapper"><div class="scrollableArea"><ul class="book-pictures"></ul></div></div>');
    $(".book-name-current").html($(ulbook).attr("title"));
    $(".book-print-current").attr("href", "/pdf/" + idbook).show();
    $("#vbook-viewer").hide();
    $("#book-viewer").show();
    LoadBookNew(ulbook);
    
}

function LoadBookNew(bookul) {
    imgs = [];
    $("ul.book-pictures").css('width', $(bookul).css('width'));
    $("#book-viewer").smoothDivScroll({
        visibleHotSpots: "always"
    });

    var idbook = Number($(bookul).attr("id").substring(1));
    $("#printbook").attr("href", "/pdf/" + idbook);
    $("#printbook").show();

    $("#book-viewer div:eq(0)").show();
    $("#book-viewer div:eq(1)").show();
    $("ul.book-pictures").empty();
    $(bookul).find("li a").each(function (i, item) {
        imageItem = { url: $(item).attr('href'), w: $(item).attr('title').split('|')[0], h: $(item).attr('title').split('|')[1] };
        imgs.push(imageItem);
    });
    isrecurring = true;
    LoadImage(0);
}

function LoadImage(index)
{
    if (stoprecursion) {
        stoprecursion = false;
        isrecurring = false;
        return;
    }
    if (index < imgs.length) {
        list = $("<li></li>").attr("class", "loading");
        $("ul.book-pictures").append(list);
        img = new Image();
        $(img).load(function () {
            $(list).append(this);
            $(this).css("display", "none");
            $(this).attr("height", imgs[index].h);
            $(this).attr("width", imgs[index].w);
            $(list).removeClass("loading");
            $(this).fadeIn("fast", function () {
                LoadImage(index + 1);
            });
        }).error(function () {
            $(list).remove();
            LoadImage(index + 1);
        }).attr("src", imgs[index].url);
    } else {
        isrecurring = false;
        return;
    }
}

function vbook_onload(idbook, booktitle) {
    
    currentbook = 0;
    $("#vbook-player").empty();
    //$(".book-name-current").html(booktitle);
    $(".book-print-current").hide();

    so = null;
    so = new SWFObject('/a/mediaplayer/player.swf', 'mpl', '720', '400', '9');
    so.addParam('allowfullscreen', 'true');
    so.addParam('allowscriptaccess', 'always');
    so.addParam('wmode', 'opaque');
    so.addVariable('playlistfile', '/VideoBooks/' + idbook + '/playlist');
    so.addVariable('playlistsize', '200');
    so.addVariable('playlist', 'right');
    so.addVariable('autostart', 'true');
    so.write('vbook-player');

    $("#book-viewer").hide();
    $("#printbook").hide();
    $("#vbook-viewer").show();
    //page_onLoad(page, ulbook);
    
}

/*
function goToPage(pn, ul) {
    var page = pn;
    var lis = $(ul).children("li");
    if (pn >= $(lis).length) {
        page = 0;
    } else if (pn < 0) {
        page = ($(lis).length - 1);
    } else {
        page = pn;
    }
    page_onload(page, ul);
}

function page_onload(p, u) {
    $("#book-page").empty();
    var pli = $(u).find("li").eq(p);
    var bpw = Number($("span[title='page-width']").html());
    var bph = Number($("span[title='page-height']").html());
    var pm = Number($("span[title='picture-margin']").html());
    var totw = 0;
    nomargin = false;
    $(pli).find("a").each(function (i, item) {
        var imgpath = $(item).attr("href");
        var rw = Number($(item).find("span[title='w']").html());
        var rh = Number($(item).find("span[title='h']").html());
        var i = $("<img/>");
        if (pm > 0 && totw == 0) {
            totw += pm;
        }
        if (rw > 0) {
            $(i).attr("width", rw);
            totw += rw;
        }
        if (rh > 0) {
            $(i).attr("height", rh);
        }
        if (rw == 0 || rh == 0) {
            nomargin = true;
        }
        $(i).attr("src", imgpath);
        //calcolare margini
        if (bph > 0 && !nomargin) {
            if (Math.floor((bph - rh) / 2) > 0) {
                $(i).css("marginTop", Math.floor((bph - rh) / 2));
            }
        }
        if (pm > 0) {
            $(i).css("marginRight", pm);
        }
        $("#book-page").append(i);

        var onclickpicture = null;
        if ($(item).next("a").length > 0) {
            onclickpicture = function () { goToPage(p - 1, u); }
        } else {
            onclickpicture = function () { goToPage(p + 1, u); }
        }
        $(i).unbind("click").click(function () { onclickpicture(); });
        $(".book-page-nav-prev").unbind("click").click(function () { goToPage(p - 1, u); });
        $(".book-page-nav-next").unbind("click").click(function () { goToPage(p + 1, u); });
    });
    if (bpw > 0 && !nomargin) {
        if (Math.floor((bpw - totw) / 2) > 0) {
            $("#book-page > img").eq(0).css("marginLeft", Math.floor((bpw - totw) / 2));
        }
    }
    if (pm > 0) {
        $("#book-page > img:last").css("marginRight", 0);
    }
    $(".book-page-current").html(p + 1);
    $(".book-page-total").html($(u).find("li").length);
}
*/
