$(document).ready(function() {
    if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
        // search for selectors you want to add hover behavior to
        $("#all_locatie li").hover(
			function() {
			    $(this).addClass('ie_hover');
			},
			function() {
			    $(this).removeClass('ie_hover');
			});
    }

    $("#showhighlights").click(function() {
        if ($(this).hasClass("showinghighlights")) {
            undimEverything();
            $(this).removeClass("showinghighlights");
        } else {
            dimEverything();
            cancelAllExcept("showhighlights");
            $(".hf-highlight").addClass("highlight").css('opacity', 1);
            $(".hf-highlight").addClass("highlight");
            $(this).addClass("showinghighlights");
        }
    });


    setTimeout(checkCheckedGenres, 1000);
    setTimeout(checkLocations, 1000);
    setTimeout(resetBNR, 1000);
    $("#select_allgenres input").click(function() {
        checkCheckedGenres()
    });

    $("#all_genres_hider").click(function() {
        $("#select_allgenres input").attr('checked', false);
        $(this).css("display", "none");
        highlightGenres();
    });

    $("#all_genres li input").click(function() {
        highlightGenres();
    });

    $("#select_alllocaties, #all_locatie").hover(function() {
        displayLocations('show');

    }, function() {
        displayLocations('hide');
    });
    $("#select_allBNRs, #all_BNRs, #selectedBNR").hover(function() {
        displayBNRs('show');
    }, function() {
        displayBNRs('hide');
    });
    $("#selectedBNR").click(function() {
        undimEverything();
        $(this).css("display", "none");
    });
    $("#select_alllocaties input").click(function() {
        checkLocations();
    });
    $("#select_alllocaties input").click(function() {
        checkLocations();
    });


    $("#all_BNRs li").click(function() {
        displayBNRs('hide');
        undimEverything();
        BNRvoorstellingen = $(this).attr("rel");

        resetLocations();
        resetGenres();

        $("#selectedBNR").text($(this).text()).css("display", "block");

        BNRv = BNRvoorstellingen.split(",");
        //alert(BNRv[0]);
        dimEverything();
        cancelAllExcept("bnr_selector_container");
        for (i = 0; i < BNRv.length; i++) {
            showBNRkeuze(BNRv[i]);
        }
    });

    $("#all_locatie input").click(function() {
        checkEachLocation();
        highlightLocations();
    });
    jQuery.extend(jQuery.expr[':'], {
        containsIgnoreCase: "(a.textContent||a.innerText||jQuery(a).text()||'').toLowerCase().indexOf((m[3]||'').toLowerCase())>=0"
    });



    $("#vrijzoeken #filter").click(function() {
        var found = false;
        filtertekst = $("#abcd").val();
        if (filtertekst == "") {
            undimEverything();
        } else {
            dimEverything();
            cancelAllExcept("abcd");

            $("#filter").css("display", "none");
            $("#un-filter").css("display", "block");
            $("#agenda-holder table.overviewtable div.highlight").removeClass("highlight");

            $("#agenda-holder table.overviewtable div.uitvoering div.text h2:containsIgnoreCase(" + filtertekst + "),#agenda-holder table.overviewtable div.uitvoering div.text p.uitvoerende:containsIgnoreCase(" + filtertekst + ")").parents("div.uitvoering").each(function(i) {
                found = true;
                $(this).addClass("highlight");
                kleingevonden = $(this).attr("uitvoeringid");
                $("#calendar .overviewtable div.genre[uitvoeringid=" + kleingevonden + "]").css('opacity', 1);
            });

            if (!found) {
                if (!isEnglish) {
                    alert("Er is geen voorstelling gevonden bij uw zoekopdracht.");
                }
                else {
                    alert("No shows were found for your current query.");
                }
                undimEverything();
                $("#abcd").val("ZOEKEN op uitvoering en uitvoerende");
                $("#filter").css("display", "block");
                $("#un-filter").css("display", "none");
            }
        }
    });

    $("#vrijzoeken #un-filter").click(function() {
        undimEverything();
        $("#abcd").val("ZOEKEN op uitvoering en uitvoerende");
        $("#filter").css("display", "block");
        $("#un-filter").css("display", "none");
    });

    $('#vrijzoeken input').bind('keypress', function(e) {
        if (e.keyCode == 13) {
            $("#vrijzoeken #filter").trigger('click');

        } else {
            $("#filter").css("display", "block");
            $("#un-filter").css("display", "none");
        }
    });
});       // end document ready

function resetBNR() {
    $("#selectedBNR").css("display", "none");
}

function resetLocations() {
    $("#locatie_selector input").attr("checked", true);
}

function resetGenres() {
    $("#all_genres_hider").css("display", "block");
}

function showBNRkeuze(uitvoeringID) {

    $("div.uitvoering[uitvoeringid='" + uitvoeringID + "']").addClass("highlight");
    $("#calendar-holder .overviewtable div.genre[uitvoeringid='" + uitvoeringID + "']").css('opacity', 1);
}

function checkCheckedGenres() {
    $("#select_alllocaties input").attr("checked", true);
    var n = $("#select_allgenres input:checked").length;
    if (n == 1) {
        $("#all_genres_hider").css("display", "block");
        undimEverything()


    } else if (n == 0) {
        $("#all_genres_hider").css("display", "none");
        $("#calendar-holder td .genre").css('opacity', 1)
        highlightGenres();
    }
}
function dimEverything() {
    $("#agenda-holder .uitvoering").each(function() {
        if (!($(this).find(".dimmer").length > 0)) {
            $("#agenda-holder .uitvoering").append("<div class='dimmer'></div>");
        }
        $(this).removeClass("highlight");
    });

    $(".overviewtable td").addClass("dimTD");
    $("#calendar-holder td .genre").css('opacity', 0.2);
}
function undimEverything() {
    $("#agenda-holder .uitvoering .dimmer").remove();
    $("#calendar-holder td .genre").css('opacity', 1)
    $(".overviewtable td").removeClass("dimTD");
    $(".genre").removeClass("highlight");
    $(".uitvoering").removeClass("highlight");
    cancelAllExcept("none");
}
function cancelAllExcept(which) {

    if (which == "none") {
        $("#showhighlights,#locatie_selector_container,#genre_selector_container,#abcd,#bnr_selector_container").css("border", "solid 1px #666666");
    } else {
        $("#showhighlights,#locatie_selector_container,#genre_selector_container,#abcd,#bnr_selector_container").css("border", "solid 1px #999999");
        $("#" + which).css("border", "solid 1px #000000 ");
        if (which == "showhighlights") {

        } else {
            $("#showhighlights").removeClass("showinghighlights");
        }
        if (which == "bnr_selector_container") {

        } else {
            resetBNR();
        }
    }
}
function highlightGenres() {
    resetBNR();
    if ($("#select_alllocaties input").attr('checked') == false) {
        undimEverything();

        $("#select_alllocaties input").attr('checked', true);
        $("#all_locatie input").attr('checked', true);
    }
    dimEverything();
    cancelAllExcept("genre_selector_container");
    $("#all_genres li input:checked").each(function() {

        genreclass = $(this).parent().attr("class");

        $("#agenda-holder ." + genreclass).each(function() {
            $(this).parent().parent().parent().parent().parent().parent().addClass("highlight");
        });

        $("#calendar-holder td ." + genreclass).each(function() {
            $(this).parent().parent().parent().parent().css('opacity', 1)
        });
    });
}
function highlightLocations() {
    resetBNR();
    dimEverything();
    cancelAllExcept("locatie_selector_container");
    $("#all_locatie li input:checked").each(function() {

        locatienaam = $(this).parent().attr("class");
        $(".location").each(function() {
            if ($(this).hasClass(locatienaam)) {
                $(this).parent().parent().addClass("highlight");
            }
        });
    });
}
function displayLocations(what) {
    if (what == "show") {
        $("#all_locatie").show();
    } else {
        $("#all_locatie").hide();
    }
}
function displayBNRs(what) {
    if (what == "show") {
        $("#all_BNRs").show();
    } else {
        $("#all_BNRs").hide();
    }
}

function checkLocations() {
    dimGenres();
    if ($("#select_alllocaties input").attr("checked") == true) {

        $("#all_locatie input").attr('checked', true);
        undimEverything();
    } else {
        var x = $("#all_locatie input:not(:checked)").length;
        var y = $("#all_locatie input").length;
        if (x != y) {
            $("#all_locatie input").attr('checked', false);
        }
        //$(".all_locatie input").attr('checked', false);
    }
}
function dimGenres() {
    if ($("#select_allgenres input").attr('checked') == false) {
        undimEverything();
        $("#all_genres_hider").css("display", "block");
        $("#select_allgenres input").attr('checked', true);
    }
}
function checkEachLocation() {
    // browser for locations so put genres to 'all'
    dimGenres();


    var x = $("#all_locatie input:not(:checked)").length;
    var y = $("#all_locatie input").length;
    if (x > 0) {
        $("#select_alllocaties input").attr("checked", false);
    } else if (x = y) {
        $("#select_alllocaties input").attr("checked", true);
    }

}
function highlightQuery() {
    //setDayWidth(6,2);
    var filter = $(document).getUrlParam("fltr");
    var filterID = $(document).getUrlParam("id");
    if (filter == "bnr") {
        $("#selectBNR option[id='bnr-" + filterID + "']").attr("selected", true);
        BNRvoorstellingen = $("#selectBNR option[id='bnr-" + filterID + "']").val();

        // alert(BNRvoorstellingen);
        resetLocations();
        resetGenres();

        BNRv = BNRvoorstellingen.split(",");
        //alert(BNRv[0]);
        dimEverything();
        for (i = 0; i < BNRv.length; i++) {

            showBNRkeuze(BNRv[i]);
        }
    }
}


