﻿var loaded = false, activeTab, currentMusic, playing = false, start = true;
window.onresize = resize;
var checker;

function checkSilverlight()
{
    var version;
    version = getSilverlightVersion();
    if (version >= 2)
    {
        document.getElementById("tabs").style.display = "";
        document.getElementById("BookBackground").style.display = "";
        document.getElementById("BookDiv").style.display = "";
        document.getElementById("footer").style.display = "";
        document.getElementById("SilverlightInstall").style.display = "none";
    }
    else
    {
        document.getElementById("tabs").style.display = "none";
        document.getElementById("BookBackground").style.display = "none";
        document.getElementById("BookDiv").style.display = "none";
        document.getElementById("footer").style.display = "none";
        document.getElementById("SilverlightInstall").style.display = "";
        checker = setInterval(checkInstall, 5000);
    }
}

function checkInstall()
{
    window.navigator.plugins.refresh(false);
    var version = getSilverlightVersion();
    if (version >= 2)
    {
        clearInterval(checker);
        location.reload();
    }
}

function bookLoaded()
{
    window.onresize = resize;
    document.body.onmouseup = mouseUp;
    window.document.body.onmouseup = mouseUp;
    document.body.onmousemove = mouseMoved;
    window.document.body.onmousemove = mouseMoved;
    loaded = true;
    //enableFPS();
    adjustables();
    resize();
    activeTab = document.getElementById('appartementen');
    initTabs();
}

function resize(e)
{

    if (loaded) document.getElementById("Book").Content.BookBridge.correctionX = (Math.min(document.body.clientWidth, document.documentElement.clientWidth) - 878) / 2;
    if (loaded) document.getElementById("Book").Content.BookBridge.correctionY = 40;
    if (loaded) window.status = "resized: (" + document.getElementById("Book").Content.BookBridge.correctionY + ", " + document.getElementById("Book").Content.BookBridge.correctionX + ")";
}

function enableFPS()
{
    if (loaded) document.getElementById("Book").settings.EnableFramerateCounter = true;
}

function adjustables()
{
    if (loaded) document.getElementById("Book").Content.BookBridge.AdjustMyZ('footer');
    if (loaded) document.getElementById("Book").Content.BookBridge.AdjustMyZ('pageList');
    if (loaded) document.getElementById("Book").Content.BookBridge.AdjustMyZ('tabs');

}

function populatePageList(pages)
{
    var sel = document.getElementById('pageList');
    while (sel.firstChild) sel.removeChild(sel.firstChild);

    for (var i = 0; i < pages.length; i++)
    {
        var option = document.createElement('option');
        option.value = pages[i];
        option.appendChild(document.createTextNode(pages[i]));
        document.getElementById('pageList').appendChild(option);
    }
}

function silverlightOutput(text)
{
    if (loaded) document.getElementById("Book").Content.BookBridge.ToOutput(text);
}

function mouseMoved(e)
{
    if (!e) e = window.event;

    if (loaded) document.getElementById("Book").Content.BookBridge.mouseMove(e.clientX, e.clientY);
}

function mouseUp(e)
{
    if (!e) e = window.event;

    if (loaded) document.getElementById("Book").Content.BookBridge.mouseUp(e.clientX, e.clientY);
}

function pageListChanged(e)
{
    if (e.options[e.selectedIndex].value !== "noselect" && loaded) document.getElementById("Book").Content.BookBridge.Goto(e.options[e.selectedIndex].value);
}

function goTo(page)
{
    if (loaded) document.getElementById("Book").Content.BookBridge.Goto(page);
}

function popup(who, what, width, height)
{
    $.showAkModal(who, what, width, height);
}

function popupAjax(who, what, width, height)
{
    $.showAjaxModal(who, what, width, height);
}

function pagingText(page, subpage)
{
    document.getElementById("pageText").innerHTML = page;
    if (typeof (subpage) == 'undefined' || subpage == '')
        document.getElementById("subpageText").innerHTML = "";
    else
        document.getElementById("subpageText").innerHTML = " / " + subpage;
}

function initTabs()
{
    document.getElementById('home').style.zIndex = "9";
    document.getElementById('bedrijven').style.zIndex = "8";
    document.getElementById('overigen').style.zIndex = "7";
    document.getElementById('appartementen').style.zIndex = "6";
    document.getElementById('tot5ton').style.zIndex = "5";
    document.getElementById('vanaf5ton').style.zIndex = "4";
    document.getElementById('nieuwbouw').style.zIndex = "3";
    document.getElementById('tabs').style.zIndex = "3";
    document.getElementById('footer').style.zIndex = "6"
    activeTab.firstChild.style.display = 'block';
}

function setActiveTab(who)
{
    if (who == 'home') initTabs();
    else
    {
        var temp = activeTab.style.zIndex;
        activeTab.firstChild.style.display = 'block';
        activeTab.style.zIndex = document.getElementById(who).style.zIndex;
        document.getElementById(who).style.zIndex = temp;
        activeTab = document.getElementById(who);
        activeTab.firstChild.style.display = 'none';
    }
}

function playMusic()
{
    if (!loaded && playing) return;
    playing = true;
    if (typeof (currentMusic) == 'undefined')
        currentMusic = 0;

    document.getElementById("Book").Content.BookBridge.Play("muziek" + (currentMusic + 1));
    document.getElementById("knoppauze").style.display = '';
    document.getElementById("knopplay").style.display = 'none';
}

function pauseMusic()
{
    if (!loaded && !playing) return;
    playing = false;
    document.getElementById("Book").Content.BookBridge.Pause("muziek" + (currentMusic + 1));
    document.getElementById("knoppauze").style.display = 'none';
    document.getElementById("knopplay").style.display = '';
}

function previousMusic()
{
    if (!loaded && !playing) return;
    document.getElementById("Book").Content.BookBridge.Stop("muziek" + (currentMusic + 1));
    currentMusic = (currentMusic + 2) % 3;
    document.getElementById("Book").Content.BookBridge.Play("muziek" + (currentMusic + 1));
}

function nextMusic()
{
    if (!loaded && !playing) return;
    document.getElementById("Book").Content.BookBridge.Stop("muziek" + (currentMusic + 1));
    currentMusic = (++currentMusic) % 3;
    document.getElementById("Book").Content.BookBridge.Play("muziek" + (currentMusic + 1));
}

function logThis(text)
{
    //if (loaded) document.getElementById("log").innerHTML += text + "<br />";
}

function hideTabs()
{
    // tabs should only be visible when we're not at the cover or the back
    $('#tabs').animate({ height: 0, top: 40 }, 2000);
}

function showTabs()
{
    // tabs should only be visible when we're not at the cover or the back
    $('#tabs').animate({ height: 37, top: 3 }, 2000);
}

function hideAnimationForCover()
{
    // when the cover is visible, the left piece of the book should not be visible
    // and the tabs should also not be visible
    $('#BookBackground').animate({ width: 454, height: 571, left: 279 }, 2000);
    $('#Book').animate({ left: -439 }, 2000);
    $('#BookDiv').animate({ width: 438, height: 625, left: 286 }, 2000);
}

function showAnimationForCover()
{
    // when the cover is visible, the left piece of the book should not be visible
    // and the tabs should also not be visible
    $('#BookBackground').animate({ top: 25 }, 1500,
        function()
        {
            $('#tabs').animate({ height: 29, top: 11 }, 1000,
                function()
                {
                    $('#tabs').animate({ height: 29 }, 250,
                        function()
                        {
                            document.getElementById('Book').Content.BookBridge.Forward();
                            $('#tabs').animate({ height: 29 }, 5000, function() { playMusic(); });
                        }
                    );
                }
            );
        }
    );
}

function animationForBack()
{

}

var ff = (navigator.appName == "Netscape") ? true : false;
var ie = (navigator.appName == "Microsoft Internet Explorer") ? true : false;

function init()
{
    if (ff) document.captureEvents(Event.MOUSEDOWN || Event.CLICK);
    document.onclick = mouseclick;
    document.onmousedown = mouseclick;
    document.oncontextmenu = mouseclick;
    if (ff) document.releaseEvents(Event.MOUSEDOWN || Event.CLICK);
}

function mouseclick(e)
{
    if (!loaded) return true;
    if (ff && (e.which == 3))
    {
        // is the mouse positioned over the left or the right page
        if (!e) e = window.event;
        if (e.clientX < document.body.clientWidth / 2) document.getElementById("Book").Content.BookBridge.Refresh(true);
        else document.getElementById("Book").Content.BookBridge.Refresh(false);
        return true;
    }
    else if (ie && (window.event.button == 2))
    {
        // is the mouse positioned over the left or the right page
        if (!e) e = window.event;
        if (e.clientX < document.body.clientWidth / 2) document.getElementById("Book").Content.BookBridge.Refresh(true);
        else document.getElementById("Book").Content.BookBridge.Refresh(false);
        return true;
    }
    else return true;
}
