﻿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 getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}

function SetDropDowns() {
    var city = getQuerystring("city", "");
    $('#ddlCity').val(city);

    var city = getQuerystring("area", "");
    $('#ddlArea').val(city);
}

$(function() {

    $("#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);
});
