$(document).ready(function(){
    $(".cards-filter-item .cards-all input").click(function(){
        var this_checked = $(this).attr("checked");
        $(".cards-filter-item .card-item input").attr("checked", this_checked);
    });
    $(".cards-filter-item .card-item input").click(function(){
        var this_checked = $(this).attr("checked");
        if (!this_checked) $(".cards-filter-item .cards-all input").attr("checked", false);
        else {
            var is_check_all = true;
            $(".cards-filter-item .card-item input").each(function(){
                if (!$(this).attr("checked")) is_check_all = false;
            });
            if (is_check_all) $(".cards-filter-item .cards-all input").attr("checked", true);
        }
    });

    $(".cards-filter-item .city input").click(function(){
        var this_checkboxes = $(this).parent().parent().children(".cards-filter-checkbox-block");
        if (this_checkboxes.is(":hidden")) {
            $(".cards-filter-checkbox-block").slideUp("slow");
            this_checkboxes.slideDown("slow");
        } else {
            if (!this_checkboxes.is(":visible")) {
                $(".cards-filter-checkbox-block").slideUp("slow");
            }
        }
    });

    $(".cards-filter-item .okrug-all input").click(function(){
        var this_checked = $(this).attr("checked");
        $(this).parent().parent().children(".okrug-item").children("input").attr("checked", this_checked);
    });
    $(".cards-filter-item .okrug-item input").click(function(){
        var this_checked = $(this).attr("checked");
        if (!this_checked) $(this).parent().parent().children(".okrug-all").children("input").attr("checked", false);
        else {
            var is_check_all = true;
            $(".cards-filter-item .okrug-item input").each(function(){
                if (!$(this).attr("checked")) is_check_all = false;
            });
            if (is_check_all) $(this).parent().parent().children(".okrug-all").children("input").attr("checked", true);
        }
    });

    $(".cards-filter-item .highway-all input").click(function(){
        var this_checked = $(this).attr("checked");
        $(this).parent().parent().children(".highway-item").children("input").attr("checked", this_checked);
    });
    $(".cards-filter-item .highway-item input").click(function(){
        var this_checked = $(this).attr("checked");
        if (!this_checked) $(this).parent().parent().children(".highway-all").children("input").attr("checked", false);
        else {
            var is_check_all = true;
            $(".cards-filter-item .highway-item input").each(function(){
                if (!$(this).attr("checked")) is_check_all = false;
            });
            if (is_check_all) $(this).parent().parent().children(".highway-all").children("input").attr("checked", true);
        }
    });

    $("#sections_geo_filter input.sections_geo_checkbox").click(function(){
        var this_checked = $(this).attr("checked");
        $(this).parent().children(".subsection").children("input").attr("checked", this_checked);
    });
    $("#sections_geo_filter .subsection input").click(function(){
        var this_checked = $(this).attr("checked");
        if (!this_checked) $(this).parent().parent().children("input.sections_geo_checkbox").attr("checked", false);
        else {
            var is_check_all = true;
            $("#sections_geo_filter .subsection input").each(function(){
                if (!$(this).attr("checked")) is_check_all = false;
            });
            if (is_check_all) $(this).parent().parent().children("input.sections_geo_checkbox").attr("checked", true);
        }
    });

    $("#add_param_btn").click(function(){
        var add_param = $("#add_param");
//        if (add_param.is(":hidden")) add_param.removeClass("no-display");
//        else add_param.addClass("no-display");
        if (add_param.is(":hidden")) add_param.slideDown("slow");
        else add_param.slideUp("slow");
        return false;
    });

    $("#fuel_type_checker a").click(function(){
        var th_btn_div = $(this).parent().parent();
        if (!(th_btn_div.hasClass("grbut"))) {
            $("#fuel_type_checker .grbut").removeClass("grbut");
            th_btn_div.addClass("grbut");
            $("#fuel_type").val($(this).attr("id"));
        }
        return false;
    });

    $("#cards_geo_checker a").click(function(){
        var th_btn_div = $(this).parent().parent();
        if (!(th_btn_div.hasClass("grbut"))) {
            var th_card_geo = $(this).attr("geo");
            $("#cards_geo_checker .grbut").removeClass("grbut");
            th_btn_div.addClass("grbut");
            $(".allinf .cards-geo-block").hide();
            $("#"+th_card_geo+"_banners").show();
        }
        return false;
    });

    $("#city_checker a").click(function(){
        var th_btn_div = $(this).parent().parent();
        if (!(th_btn_div.hasClass("grbut"))) {
            $("#city_checker .grbut").removeClass("grbut");
            th_btn_div.addClass("grbut");
            $("#city_id").val($(this).attr("id"));
        }
        return false;
    });

    $("#geo_search_type_checker a").click(function(){
        var th_btn_div = $(this).parent().parent();
        if (!(th_btn_div.hasClass("grbut"))) {
            $("#geo_search_type_checker .grbut").removeClass("grbut");
            th_btn_div.addClass("grbut");
            $(".geo-filter-item").parent().addClass("no-display");
            $("#"+$(this).attr("id")+"_geo_filter").removeClass("no-display");
            $("#geo_search_type").val($(this).attr("id"));
        }
        return false;
    });

    $("a.cities_section_href").click(function(){
        var section_block = $(this).parent().children(".geo-filter-section");
        if (section_block.is(":hidden")) section_block.slideDown(200);
        else section_block.slideUp(200);
        return false;
    });


    $("#station_type_checker a").click(function(){
        var th_btn_div = $(this).parent().parent();
        if (!(th_btn_div.hasClass("grbut"))) {
            $("#station_type_checker .grbut").removeClass("grbut");
            th_btn_div.addClass("grbut");
            $("#station_type").val($(this).attr("id"));
        }
        return false;
    });

    $(".ch_submit a").click(function(){
        document.getElementById("filter_form").submit();
        return false;
    });

    $("#city_checker_catalog a").click(function(){
        var th_btn_div = $(this).parent().parent();
        if (!(th_btn_div.hasClass("grbut"))) {
            $("#city_checker_catalog .grbut").removeClass("grbut");
            th_btn_div.addClass("grbut");
            var th_id = $(this).attr("id");
            if (th_id == "all") {
                th_id = "";
                $(".cardlist .geoitems").show();
            } else {
                $(".cardlist .geoitems").hide();
                $(".cardlist .geo_" + th_id).show();
            }
            $("#city_id").val(th_id);
        }
        return false;
    });

    $(".cardlist .submit_form").click(function(){
        document.getElementById("filter_form").submit();
        return false;
    });

    $(".sechrezalt .closelink").click(function(){
        var th_id = $(this).attr("id");
        if ($(this).parent().hasClass("graytd")) {
            var cur_colspan = Math.abs($("#recomend_td").attr("colspan"));
            var new_colspan = cur_colspan - 1;
            if (new_colspan == 0) $("#recomend_td").hide();
            else $("#recomend_td").attr("colspan", new_colspan.toString());
        }
        $("." + th_id).hide();
        return false;
    });

    $(".cardhref").click(function(){
        var th_id = $(this).attr("id");
        var city_id = $("#city_id").val();
        var th_href = "/cards/" + th_id + "/";
        if (city_id != "") th_href += "city/" + city_id + "/";
//        location.href = th_href;
        location.replace(th_href);
        return false;
    });

    $(".card-container .card-txt").click(function(){
        location.replace($(this).parent().children("a").attr("href"));
    });

    $(".couponhref").click(function(){
        var th_id = $(this).attr("id");
        var city_id = $("#city_id").val();
        var th_href = "/coupons/" + th_id + "/";
        if (city_id != "") th_href += "city/" + city_id + "/";
//        location.href = th_href;
        location.replace(th_href);
        return false;
    });
});