﻿$(function () {

    $("#filter a").click(function () {
        var hash = $(this).attr("href").split("#")[1];
        filterListings(hash);
        $(this).parent().addClass("active").siblings().removeClass("active");
    });

    if (window.location.hash) {
        $("#filter a[href='" + window.location.hash + "']").trigger("click");
    }

    //intercept all nav links to add the current filter to the URL (proper 'back' button links can be re-built from this as a breadcrumb)
    $(".types a").click(function () {
        var link = $(this).attr("href");
        if (window.location.hash) {
            link = $(this).attr("href") + "&filter=" + window.location.hash.toString().replace(/#/i, "");
        }
        $(this).attr("href", link);
        return true;
    });

});

function filterListings(hash) {
    var season = (hash === 'all') ? '' : hash.substring(6);
    var $listings = $(".types");
    $listings.removeClass("hide");
    $listings.show();
    if (season !== '') {
        $listings.filter(":not([class*='-" + season + "-'])").hide();
    }

    // hide or show the listing container as necessary
    $(".listings").each(function () {
        if ($(this).find(".types").length === $(this).find(".types.hide").length) {
            $(this).addClass("hide");
        } else {
            $(this).removeClass("hide");
        }
    });

}
