
$(document).ready(function () {
    initCurrentPage();
});

var isHomePage = false;
var sitemapIsOpened = true;
var sharePopOverIsOpened = false;

// initializes the current page
function initCurrentPage() {
    if (!isHomePage) {
        // reposition the labels to the correct position
        $(".left-aligned-label").parent().each(function () {
            var pos = $(this).position();
            $(this).attr("style", "left: " + pos.left + "px; top: " + (pos.top - 24) + "px;");
        });


        $(".right-aligned-label").parent().each(function () {
            var pos = $(this).position();
            $(this).attr("style", "left: " + (pos.left - $(this).width()) + "px; top: " + (pos.top - 24) + "px;");
        });
    }
    // check the calendar items
    if ($(".month").length > 0) {
        var months = ["januari", "februari", "maart", "april", "mei", "juni", "juli", "augustus", "september", "oktober", "november", "december"];
        $(".month").each(function () {
            var index = parseInt($(this).html()) - 1;
            $(this).html(months[index]);
        });
    }

    // randomize the name of the imagemap (is a FIX for Safari)
    var rndName = (((Math.random())*0x10000)|0).toString(16);
    $("#imageMap").attr("name", rndName);
    $(".imageMap IMG").attr("usemap", "#" + rndName);
    
    // position the popovers when present
    if ($('.button-popover').length > 0) {
        var xpos = [0, "0px", "0px"];
        var i = 1;
        var cumulativeWidth = $(".unit").position().left + $(".unit .lefthalf").width();
        cumulativeWidth += $(".unit .half").width();
        $(".button-popover").each(function () {
            var buttonWidth = $("#buttonLinkedToPopOverWithID-popover" + i).width();
            $(this).attr("style", "left: " + (-100 + cumulativeWidth + buttonWidth / 2) + "px; top:280px;");
            cumulativeWidth += buttonWidth;
            hideElement($(this));
            i = i + 1;
        });
    }

    // close the sitemap on default for every page reload
    sitemapIsOpened = false;
    // sitemap
    var sitemapButton = $(".sitemap-button").click(handleSitemapButtonClicked);
    updateSitemap();

    // share popover
    sharePopOverIsOpened = false; // this state is not saved between pageloads
    var shareButton = $("#footerContactButton").click(handleShareButtonClicked);

    // check for the existence of a unit by checking for an element
    // with the id "unit"
    var pageHasUnit = $("#unit").length > 0;
    if (pageHasUnit) { // Has a unit
        // this is a content page
    } else { // has no unit
        // probably a room or homepage
        // loop through the labels and hide them
        if (isHomePage) {
            $(".content-label").each(function () { $(this).hide(); });
        }

        $(".left-aligned-medewerker-label").each(function () { $(this).parent().hide(); });

        // loop through the overlays and hide them
        var overlayItemID = 0;

        // loop through the polygons

        if (isHomePage === true) {
            // hide the content to let the image map work
            $(".content .overlay").each(function () {
                $(this).hide();
            });
            $("AREA").each(function () {
                // add mouse interactions
                $(this).hover(
					function () {
					    // get the overlay and show it
					    $(getOverlayForPolygonWithID($(this).attr("id"))).show();
					    $(getLabelForPolygonWithID($(this).attr("id"))).show();
					},
					function () {
					    // get the overlay and hide it
					    $(getOverlayForPolygonWithID($(this).attr("id"))).hide();
					    $(getLabelForPolygonWithID($(this).attr("id"))).hide();
					}
				);
            });
        } else {
            $("AREA").each(function () {
                // add mouse interactions
                $(this).hover(
					function () {
					    // get the overlay and show it

					    $(getLabelForPolygonWithID($(this).attr("id"))).show();
					},
					function () {
					    // get the overlay and hide it
					    $(getLabelForPolygonWithID($(this).attr("id"))).hide();
					}
				);
            });
            $(".button-popover").each(function () {
                $(this).hide(); // hide it first
                // bind the it to the right button next
                var _id = $(this).attr("id");
                $("#buttonLinkedToPopOverWithID-" + _id).click(function (e) {
                    e.preventDefault();
                    // get the popover
                    var _a = $(this).attr("id").split("-"); // get the ID attribute and make an array by splitting with the -
                    var _b = _a[_a.length - 1]; // get the last element from the ID array
                    $(".button-popover").each(function () {
                        if ($(this).attr("id") == _b) {
                            $(this).toggle();
                        } else {
                            $(this).hide();
                        }
                    });
                });
            });
        }

    }

    // execute the function queue 
    if (initQ != null) {
        for (var i = 0; i < initQ.length; i++) {
            initQ[i].call();
        }
    }

    // has a unit
    // has no unit
    // add the roll over items for the room if no unit is present
    // loop through the units if present and add items
}

function getOverlayForPolygonWithID(id) {
    var a = id.split("-");
    var overlaySuffix = a[a.length - 1];
    return $(".still-overlay-" + overlaySuffix);
}
function getLabelForPolygonWithID(id) {
    var a = id.split("-");
    var overlaySuffix = a[a.length - 1];
    return $(".still-label-" + overlaySuffix);
}
function showElement(target) { $(target).css("display", "block"); }
function hideElement(target) { $(target).css("display", "none"); }

function handleSitemapButtonClicked(e) {
    e.preventDefault();
    sitemapIsOpened = sitemapIsOpened ? false : true;
    updateSitemap();
}

function updateSitemap() {
    // update the button label
    $(".sitemap-button a").text(sitemapIsOpened ? "SLUIT MENU" : "OPEN MENU");
    $(".sitemap-background").css("display", sitemapIsOpened == true ? "block" : "none");
}

function handleShareButtonClicked(e) {
    e.preventDefault();
    sharePopOverIsOpened = sharePopOverIsOpened ? false : true;
    $(".share-popover").css("display", sharePopOverIsOpened ? "block" : "none");
}
