function ShowSelectedPriceRange(mindx, maxdx) {
    $("span.priceRangeMin").html($("select.ddlMinPriceRefine")[0].options[mindx].text);
    $("span.priceRangeMax").html($("select.ddlMaxPriceRefine")[0].options[maxdx].text);
}
function SetSelectedPriceRange(mindx, maxdx) {
    $("select.ddlMinPriceRefine")[0].selectedIndex = mindx;
    $("select.ddlMaxPriceRefine")[0].selectedIndex = maxdx;
}
function CbxSetSelections() {
    var cbxs;
    if (this.value == "0") {
        /* set all */
        cbxs = $(":checkbox[name=" + this.name + "]");
        if (this.checked)
            cbxs.attr("checked", "checked");
        else
            cbxs.removeAttr("checked");
    } else {
        /* check if all are selected */
        cbxs = $(":checkbox[name=" + this.name + "][value!=0]:checked");
        if (cbxs.length == 5)
            $(":checkbox[name=" + this.name + "][value=0]").attr("checked", "checked");
        else
            $(":checkbox[name=" + this.name + "][value=0]").removeAttr("checked");
    }
}

$(function() {
    $('#reset-filters').click(function(e) {
        var container = $(this).closest('.infobox');

        $('input:checkbox', container).attr('checked', false);
        $('#ddlCurrencyPicker', container).val('GBP');
        $("#stars_wrapper", container).stars("select", 1);
        $('select.ddlMinPriceRefine', container).attr('selectedIndex', 0).change();
        $('select.ddlMaxPriceRefine', container).attr('selectedIndex', 6).change();
        e.preventDefault();
    });

    $("#stars_wrapper").stars({ inputType: "select", cancelShow: false });

    /* Price slider */
    $("select.ddlMinPriceRefine, select.ddlMaxPriceRefine").selectToUISlider({
        labels: 0
            , tooltip: false
            , sliderOptions: {
                slide: function(event, ui) {
                    SetSelectedPriceRange(ui.values[0], ui.values[1]);
                    ShowSelectedPriceRange(ui.values[0], ui.values[1]);
                }
                , values: [$("select.ddlMinPriceRefine")[0].selectedIndex, $("select.ddlMaxPriceRefine")[0].selectedIndex]
            }
    }).hide();
    $('.ui-slider-scale').hide();
    ShowSelectedPriceRange($("select.ddlMinPriceRefine")[0].selectedIndex, $("select.ddlMaxPriceRefine")[0].selectedIndex);

    //    /* Quality rating slider */
    //    var qltySrcFrmt = "/images/{X}stars_ph.gif";
    //    $("#slider-range-quality").slider({
    //        range: "max",
    //        min: 0,
    //        max: 5,
    //        value: $("input.hdnQuality").val(),
    //        slide: function(event, ui) {
    //            $("input.hdnQuality").val(ui.value);
    //            $("span.quality").html(ui.value);
    //            $($("img.rating")[0]).attr("src", qltySrcFrmt.replace("{X}", ui.value));
    //        }
    //    });
    //    $("span.quality").html($("input.hdnQuality").val());
    //    $($("img.rating")[0]).attr("src", qltySrcFrmt.replace("{X}", $("input.hdnQuality").val()));
    /* Quality rating check boxes */
    var cbxs = $(":checkbox[name=quality]").click(CbxSetSelections);
    if (cbxs.filter("[value=0]:checked").length == 1) {
        /* check them all */
        $(":checkbox[name=quality]").attr("checked", "checked");
    }

    //    /* Guest rating slider */
    //    var gstSrcFrmt = "/images/{X}ticks_ph.gif";
    //    $("#slider-range-guest").slider({
    //        range: "max",
    //        min: 0,
    //        max: 5,
    //        value: $("input.hdnGuest").val(),
    //        slide: function(event, ui) {
    //            $("input.hdnGuest").val(ui.value);
    //            $("span.guest").html(ui.value);
    //            $($("img.rating")[1]).attr("src", gstSrcFrmt.replace("{X}", ui.value));
    //        }
    //    });
    //    $("span.guest").html($("input.hdnGuest").val());
    //    $($("img.rating")[1]).attr("src", gstSrcFrmt.replace("{X}", $("input.hdnGuest").val()));
    /* Guest rating check boxes */
    cbxs = $(":checkbox[name=guest]").click(CbxSetSelections);
    if (cbxs.filter("[value=0]:checked").length == 1) {
        /* check them all */
        cbxs.attr("checked", "checked");
    }

    /* dynamic update: maybe... */
    //    $("input[type=image].updatebut").click(function(e) {
    //        $("#search-results-container").load("/property_searching/display_search_results.aspx?country=63&must=HasEcoRating #search-results-container", loadMap);
    //        e.preventDefault();
    //    });
});
