﻿$(document).ready(function() {
    //alert(navigator.appVersion);
    $.fn.setLocations = function(px) {

        $(this).each(function(i) {
            var iLength = $("div[class^='block']").length;

            var sContent = $(this).html();
            var iTotalPos = 0
            var iPosition = 0;
            var sHorizontal = "";
            var sVertical = "";
            var sHTML = "";
            var sTheme = "blue";
            var bLarge = false;

            if (iLength > 1 || (sContent.indexOf("<FORM") == -1) && (sContent.indexOf("<form") == -1)) {
                if ($(this).attr("theme")) { sTheme = $(this).attr("theme"); }
                if ($(this).attr("class").indexOf("_large") > -1) { bLarge = true; }

                if (((sContent.indexOf("<FORM") > -1) || (sContent.indexOf("<form") > -1))) {
                    if ($(this).find("div.wide_block_middle").html() != null) {
                        sContent = $(this).find("div.wide_block_middle").html();
                    }

                }

                $("div[class^='block']").each(function(j) {
                    if ($(this).attr("class").indexOf("_large") > -1) {
                        if (j <= i) {
                            iPosition += 2;
                        }
                        iTotalPos += 2;
                    }
                    else {
                        if (j <= i) { iPosition += 1; }
                        iTotalPos += 1;
                    }

                });

                $(this).children().remove();

                if (!bLarge) {
                    var iMod = iPosition % 2;

                    if (iMod == 1) { sHorizontal = "left"; } else if (iMod == 0) { sHorizontal = "right"; }


                    if (iPosition <= 2) {
                        sVertical = "top";
                    }
                    else if (iPosition == iTotalPos || (iPosition == (iTotalPos - 1) && iMod == 1)) {
                        sVertical = "bottom";
                    }
                    else {
                        sVertical = "middle";
                    }

                    switch (sVertical) {
                        case "top":
                            var sNoline = "";

                            if ((iTotalPos <= 2) || (sHorizontal == "right" && iTotalPos <= 3)) { sNoline = "_noline"; }

                            sHTML += "<div class='" + sHorizontal + "_" + sVertical + "_top" + sNoline + "'>";
                            sHTML += "<div class='" + sHorizontal + "_" + sVertical + "_middle" + sNoline + "'>" + sContent;
                            sHTML += "</div></div>";
                            sHTML += "<div class='" + sHorizontal + "_" + sVertical + "_bottom" + sNoline + "'>&nbsp;</div>";
                            break;
                        case "middle":
                            sHTML += "<div class='" + sHorizontal + "_" + sVertical + "_top'>&nbsp;</div>";
                            sHTML += "<div class='" + sHorizontal + "_" + sVertical + "_middle'>" + sContent + "</div>";
                            sHTML += "<div class='" + sHorizontal + "_" + sVertical + "_bottom'>&nbsp;</div>";
                            break;
                        case "bottom":
                            sHTML += "<div class='" + sHorizontal + "_" + sVertical + "_top'>&nbsp;</div>";
                            sHTML += "<div class='" + sHorizontal + "_" + sVertical + "_middle'>";
                            sHTML += "<div class='" + sHorizontal + "_" + sVertical + "_bottom'>" + sContent + "</div></div>";
                            break;

                    }
                }
                else {
                    if (iPosition == 2) { sVertical = "top"; }
                    else if (iPosition == iTotalPos) { sVertical = "bottom"; }
                    else { sVertical = "middle"; }
                    $(this).removeAttr("class");
                    $(this).attr("class", "block_large_" + sVertical);

                    sHTML += "<div class='" + sTheme + "'><div class='wide_block_top'>&nbsp;</div>";
                    sHTML += "<div class='wide_block_middle'>" + sContent + "</div><div class='wide_block_bottom'>&nbsp;</div></div>";
                    bLarge = false;
                }
                $(this).html(sHTML);
            }


        });
    };

    $.fn.equalHeights = function(px) {

        var currentTallest = 0;
        var teller = 0;
        var arBlocks = $(this);
        var bImage = false;

        for (var i = 0; i < arBlocks.length; i++) {
            if ($(arBlocks[i]).attr("class").indexOf("wide_") == -1) {
                if ((teller % 2) == 0) {
                    currentTallest = 0;
                    bImage = true;
                }

                var bBottom = ($(arBlocks[i]).html().indexOf("_bottom_bottom") > -1);
                var bImage = ($(arBlocks[i]).html().indexOf("imageloadermask") > -1);
                currentTallest = getImageHeightFromBlock(arBlocks[i], currentTallest);

                if (bBottom) {
                    if (navigator.appVersion.indexOf("Chrome") > -1) {
                        currentTallest = getBlockHeight($(arBlocks[i]).children("div[class$='_bottom_bottom']"), currentTallest);
                    }
                    else if ($(arBlocks[i]).children("div[class$='_bottom_bottom']").height() > currentTallest) {
                        currentTallest = $(arBlocks[i]).children("div[class$='_bottom_bottom']").height();
                    }
                }
                else {
                    if (navigator.appVersion.indexOf("Chrome") > -1) {
                        currentTallest = getBlockHeight($(arBlocks[i]), currentTallest);
                    }
                    if ($(arBlocks[i]).height() > currentTallest) {
                        currentTallest = $(arBlocks[i]).height();
                    }
                }

                if (bImage && $(arBlocks[i]).find(".backgroundimage").length > 0) {
                    currentTallest = $(arBlocks[i]).find(".backgroundimage").attr("height");
                    $(arBlocks[i]).find("div.imageloadermask:first").css({ "height": currentTallest });
                    currentTallest -= 13;
                }

                if ((teller % 2) == 1) {
                    if (bBottom) {
                        if ($(arBlocks[i]).find(".filler").length > 0) {
                            $(arBlocks[i]).find(".filler").css({ 'min-height': currentTallest + 13 });
                            $(arBlocks[i]).find(".filler").css({ 'height': currentTallest + 13 });
                        }

                        if ($(arBlocks[i - 1]).find(".filler").length > 0) {
                            $(arBlocks[i - 1]).find(".filler").css({ 'min-height': currentTallest + 13 });
                            $(arBlocks[i - 1]).find(".filler").css({ 'height': currentTallest + 13 });
                        }

                        $(arBlocks[i]).children("div[class$='_bottom_bottom']").css({ 'min-height': currentTallest });
                        $(arBlocks[i - 1]).children("div[class$='_bottom_bottom']").css({ 'min-height': currentTallest });
                        $(arBlocks[i]).children("div[class$='_bottom_bottom']").css({ 'height': currentTallest });
                        $(arBlocks[i - 1]).children("div[class$='_bottom_bottom']").css({ 'height': currentTallest });
                    }
                    else {
                        if ($(arBlocks[i]).find(".filler").length > 0) {
                            $(arBlocks[i]).find(".filler").css({ 'min-height': currentTallest + 13 });
                            $(arBlocks[i]).find(".filler").css({ 'height': currentTallest + 13 });
                        }

                        $(arBlocks[i]).css({ 'min-height': currentTallest });
                        $(arBlocks[i]).css({ 'height': currentTallest });

                        if ($(arBlocks[i - 1]).find(".filler").length > 0) {
                            $(arBlocks[i - 1]).find(".filler").css({ 'min-height': currentTallest + 13 });
                            $(arBlocks[i - 1]).find(".filler").css({ 'height': currentTallest + 13 });
                        }

                        $(arBlocks[i - 1]).css({ 'min-height': currentTallest });
                        $(arBlocks[i - 1]).css({ 'height': currentTallest });
                        //                        $(arBlocks[i]).css({ 'min-height': currentTallest });
                        //                        $(arBlocks[i]).css({ 'height': currentTallest });
                        //                        $(arBlocks[i - 1]).css({ 'min-height': currentTallest });
                        //                        $(arBlocks[i - 1]).css({ 'height': currentTallest });
                    }
                }
                else if ((teller == (arBlocks.length - 1)) && (teller % 2) == 0) {
                    $(arBlocks[i]).children("div[class$='_bottom_bottom']").css({ 'min-height': currentTallest });
                    $(arBlocks[i]).children("div[class$='_bottom_bottom']").css({ 'height': currentTallest });

                }

                teller++;
            }
        }
        return this;
    };

    $.fn.equalHomeHeights = function(px) {
        var currentTallest = 0;
        $(this).each(function(i) {
            if ($(this).attr("class") != "home_block_holder") {
                if ($(this).children("div").height() > currentTallest) { currentTallest = $(this).children("div").height(); }
            }
        });

        $("div.home_block").children("div").css({ "height": currentTallest });
        $("div.home_block middleblock").children("div").css({ "height": currentTallest });
        $("div.home_block").children("div").css({ "min-height": currentTallest });
        $("div.home_block middleblock").children("div").css({ "min-height": currentTallest });
    };

    $("div[class^='block']").setLocations();
    $("div[class$='_middle'],div[class$='_middle_noline']").equalHeights();
    $("div[class^='home_block']").equalHomeHeights();
    $('a.lightbox').lightBox();
});

function doSearch() {
    var sSearch = $("#txtSearch").val();

    if (sSearch.length > 0) {
        var sURL = "/zoeken";

        sURL += "&q=" + sSearch + "&start=0";

        window.location = sURL;
        return false;
    }
}

function checkSearch() {
    if (event.keyCode == 13) {
        doSearch();
    }
}

function getBlockHeight(oBlock, iHeight) {
    var oImages = $(oBlock).find("img");
    var iBlockHeight = 0;
    
    for (var i = 0; i < oImages.length; i++) {
        iBlockHeight = Math.max(oImages[i].height, iBlockHeight);
    }
    iBlockHeight = Math.max($(oBlock).height(), iBlockHeight);
    
    return Math.max(iHeight, iBlockHeight);
}

function getImageHeightFromBlock(oBlock, iCurrentTallest) {
    var oImages = $(oBlock).find("img");
    
    if(oImages!=null) {
        for (var i = 0;i < oImages.length;i++) {
            if (oImages.height() > iCurrentTallest) { iCurrentTallest = oImages.height(); }
        }
    }

    return iCurrentTallest;
}