﻿function SetupSimpleSearch(propertyListURL,advancedSearchURL, handlersPath, clientID, loadingFeedback, mandatoryFieldsFeedback, emptyDistrict, emptyCounty, emptyParish, emptyZone,
                             qsPurpose, qsGroup, qsCountry, qsDistrict, qsCounty, qsParish, qsZone,
                             qsCondition, qsReference,  qsMinTypo, qsMaxTypo,
                             qsMinPrice, qsMaxPrice, qsMinArea, qsMaxArea, isTypologySlider, isAreaSlider, isPriceSlider, valuesSeparator,showOnlyWithProperties) {
    $(document).ready(function() {

        //Fields declaration
        var ddlCountry = "#" + clientID + "_ddlCountry";
        var ddlDistrict = "#" + clientID + "_ddlDistrict";
        var ddlCounty = "#" + clientID + "_ddlCounty";
        var ddlParish = "#" + clientID + "_ddlParish";
        var ddlZone = "#" + clientID + "_ddlZone";
        var ddlPurpose = "#" + clientID + "_ddlPurpose";
        var ddlGroup = "#" + clientID + "_ddlGroup";
        var ddlCondition = "#" + clientID + "_ddlCondition";
        var ddlTypologyMin = "#" + clientID + "_ddlTypologyMin";
        var ddlTypologyMax = "#" + clientID + "_ddlTypologyMax";
        var ddlMinPrice = "#" + clientID + "_ddlMinPrice";
        var ddlMaxPrice = "#" + clientID + "_ddlMaxPrice";
        var ddlMinArea = "#" + clientID + "_ddlMinArea";
        var ddlMaxArea = "#" + clientID + "_ddlMaxArea";
        var lnkAdvancedSearch = "#" + clientID + "_lnkAdvancedSearch";
        var txtReference = "#" + clientID + "_txtReference";

        var btnSearch = "#" + clientID + "_btnSearch";


        //Events
        if (($(ddlCountry).val() != undefined) && ($(ddlCountry).val() > 0)) {
            BindDistricts();
        }

        if (($(ddlDistrict).val() != undefined) && ($(ddlDistrict).val() > 0)) {
            BindCounties();
        }

        if (($(ddlCounty).val() != undefined) && ($(ddlCounty).val() > 0)) {
            BindParishes($(ddlCounty).val());
        }

        //SelectIndexChange - ddlCountry
        $(ddlCountry).change(function() {
            BindDistricts();
        });

        //SelectIndexChange - ddlDistrict
        $(ddlDistrict).change(function() {
            BindCounties();
        });

        //SelectIndexChange - ddlCounty
        $(ddlCounty).change(function() {
            //GET  ID of County
            var idCounty = $(this).val();
            BindParishes(idCounty);
        });

        //SelectIndexChange - ddlParish
        $(ddlParish).change(function() {
            BindZones();
        });

        //SelectIndexChange - ddlParish
        $(ddlGroup).change(function() {

            BindAreas($(this).val());

        });

        //SelectIndexChange - ddlParish
        $(ddlPurpose).change(function() {
            BindPrices($(this).val());
        });


        //Bind the areas
        BindAreas($(ddlGroup).val());
        //Bind the prices
        BindPrices($(ddlPurpose).val());
        //Bind Typologies
        if (isTypologySlider == "true") {
            BindTypologies();
        };
        
        //On Click  - btnSearch
        $(btnSearch).click(function() {

            //field values declaration
            var purpose = $(ddlPurpose).val();
            var district = $(ddlDistrict).val();
            var country = $(ddlCountry).val();
            var county = $(ddlCounty).val();
            var zone = $(ddlZone).val();
            var parish = $(ddlParish).val();
            var typologyMin = $(ddlTypologyMin).val();
            var typologyMax = $(ddlTypologyMax).val();
            var priceMin = $(ddlMinPrice).val();
            var priceMax = $(ddlMaxPrice).val();
            var areaMin = $(ddlMinArea).val();
            var areaMax = $(ddlMaxArea).val();
            var condition = $(ddlCondition).val();
            var group = $(ddlGroup).val();

            var reference = $(txtReference).val();

            //if not portugal, district, county, parish are not mandatory
            if (country != 1 && country != undefined) {
                $(ddlDistrict).removeClass(clientID + "_mandatory");
                $(ddlCounty).removeClass(clientID + "_mandatory");
                $(ddlParish).removeClass(clientID + "_mandatory");
                $(ddlZone).removeClass(clientID + "_mandatory");
            }

            //get all objects mandatory
            var mandatoryFields = $("." + clientID + "_mandatory");
            var qString = "";
            var validSubmit = false;
            if (reference != "") {
                validSubmit = true;
            } else {
                validSubmit = isSubmitValid(mandatoryFields);
            }
            if (validSubmit == true) {
                //build query string with field values
                if (reference != "" && reference != undefined) {
                    qString = BuildQueryStringItem(qString, qsReference, reference);
                } else {
                    if (purpose > 0) qString = BuildQueryStringItem(qString, qsPurpose, purpose)
                    if (group > 0) qString = BuildQueryStringItem(qString, qsGroup, group)
                    if (condition > 0) qString = BuildQueryStringItem(qString, qsCondition, condition)
                    if (district > 0) qString = BuildQueryStringItem(qString, qsDistrict, district)
                    if (country > 0) qString = BuildQueryStringItem(qString, qsCountry, country)
                    if (county > 0) qString = BuildQueryStringItem(qString, qsCounty, county)
                    if (parish > 0) qString = BuildQueryStringItem(qString, qsParish, parish)
                    if (zone > 0) qString = BuildQueryStringItem(qString, qsZone, zone)
                    if (typologyMin > 0) qString = BuildQueryStringItem(qString, qsMinTypo, typologyMin)
                    if (typologyMax > 0) qString = BuildQueryStringItem(qString, qsMaxTypo, typologyMax)
                    if (priceMax > 0) qString = BuildQueryStringItem(qString, qsMaxPrice, priceMax)
                    if (priceMin > 0) qString = BuildQueryStringItem(qString, qsMinPrice, priceMin)
                    if (areaMin > 0) qString = BuildQueryStringItem(qString, qsMinArea, areaMin)
                    if (areaMax > 0) qString = BuildQueryStringItem(qString, qsMaxArea, areaMax)
                }
                var url = propertyListURL + qString;
                SendSearchToStats(qString);
                parent.location = url;

            }
            else {
                alert(mandatoryFieldsFeedback);
            }

            return false;
        });

        $(lnkAdvancedSearch).click(function() {

            //field values declaration
            var purpose = $(ddlPurpose).val();
            var district = $(ddlDistrict).val();
            var country = $(ddlCountry).val();
            var county = $(ddlCounty).val();
            var zone = $(ddlZone).val();
            var parish = $(ddlParish).val();
            var typologyMin = $(ddlTypologyMin).val();
            var typologyMax = $(ddlTypologyMax).val();
            var priceMin = $(ddlMinPrice).val();
            var priceMax = $(ddlMaxPrice).val();
            var areaMin = $(ddlMinArea).val();
            var areaMax = $(ddlMaxArea).val();
            var condition = $(ddlCondition).val();
            var group = $(ddlGroup).val();
            var reference = $(txtReference).val();


            var qString = "";
            //build query string with field values
            if (reference != "") qString = BuildQueryStringItem(qString, qsReference, reference)
            if (purpose > 0) qString = BuildQueryStringItem(qString, qsPurpose, purpose)
            if (group > 0) qString = BuildQueryStringItem(qString, qsGroup, group)
            if (condition > 0) qString = BuildQueryStringItem(qString, qsCondition, condition)
            if (district > 0) qString = BuildQueryStringItem(qString, qsDistrict, district)
            if (country > 0) qString = BuildQueryStringItem(qString, qsCountry, country)
            if (county > 0) qString = BuildQueryStringItem(qString, qsCounty, county)
            if (parish > 0) qString = BuildQueryStringItem(qString, qsParish, parish)
            if (zone > 0) qString = BuildQueryStringItem(qString, qsZone, zone)
            if (typologyMin > 0) qString = BuildQueryStringItem(qString, qsMinTypo, typologyMin)
            if (typologyMax > 0) qString = BuildQueryStringItem(qString, qsMaxTypo, typologyMax)
            if (priceMax > 0) qString = BuildQueryStringItem(qString, qsMaxPrice, priceMax)
            if (priceMin > 0) qString = BuildQueryStringItem(qString, qsMinPrice, priceMin)
            if (areaMin > 0) qString = BuildQueryStringItem(qString, qsMinArea, areaMin)
            if (areaMax > 0) qString = BuildQueryStringItem(qString, qsMaxArea, areaMax)

            var url = advancedSearchURL + qString;
            parent.location = url;

            return false;
        });


        //Get Districts
        function BindDistricts() {

            //RESET ddlDistrict
            $(ddlDistrict).html("");
            $(ddlDistrict).attr("disabled", "disabled");

            //RESET ddlCounties
            $(ddlCounty).html("");
            $(ddlCounty).attr("disabled", "disabled");

            //RESET ddlParish
            $(ddlParish).html("");
            $(ddlParish).attr("disabled", "disabled");

            //RESET ddlZone
            $(ddlZone).html("");
            $(ddlZone).attr("disabled", "disabled");

            //GET ID from ddlCountry
            var idCountry = $(ddlCountry).val();

            //IF SELECTED District
            if (idCountry != 0) {

                //GET Districts via JSON
                $(ddlDistrict).append($("<option></option>").val(0).html(loadingFeedback));

                $.getJSON(handlersPath + "/GetDistricts.ashx?cid=" + idCountry + "&withprop=" + showOnlyWithProperties.toString, function(Districts) {

                    if (Districts != null && Districts != "-1") {
                        $(ddlDistrict).html("");

                        //APPEND Districts on ddlDistricts
                        var hasRecords = false;

                        if (emptyDistrict != "") $(ddlDistrict).append($("<option></option>").val(0).html(emptyDistrict))
                        else $(ddlDistrict).append($("<option></option>").val(0).html(""))

                        $.each(Districts, function() {
                            $(ddlDistrict).append($("<option></option>").val(this['VALUE']).html(this['TEXT']));
                            hasRecords = true;
                        });
                        //ENABLE ddlDistricts
                        if (hasRecords) {
                            $(ddlDistrict).removeAttr("disabled");
                        }

                        ParseQueryString(qsDistrict, ddlDistrict);

                    }
                    else {
                        $(ddlDistrict).html("");
                    }
                });
            }
        }

        //GetCounties
        function BindCounties() {

            //RESET ddlCounty
            $(ddlCounty).html("");
            $(ddlCounty).attr("disabled", "disabled");

            //RESET ddlParish
            $(ddlParish).html("");
            $(ddlParish).attr("disabled", "disabled");

            //RESET ddlZone
            $(ddlZone).html("");
            $(ddlZone).attr("disabled", "disabled");

            //GET IDs of District
            var idDistrict = $(ddlDistrict).val();

            //IF SELECTED District
            if (idDistrict != 0 || idDistrict == undefined) {

                //GET Counties via JSON
                $(ddlCounty).append($("<option></option>").val(0).html(loadingFeedback));

                $.getJSON(handlersPath + "/GetCounties.ashx?cid=" + idDistrict + "&withprop=" + showOnlyWithProperties.toString, function(Counties) {

                    if (Counties != null && Counties != "-1") {
                        $(ddlCounty).html("");
                        //APPEND Counties ddlCounty
                        var hasRecords = false;

                        if (emptyCounty != "") $(ddlCounty).append($("<option></option>").val(0).html(emptyCounty))
                        else $(ddlCounty).append($("<option></option>").val(0).html(""))

                        $.each(Counties, function() {
                            $(ddlCounty).append($("<option></option>").val(this['VALUE']).html(this['TEXT']));
                            hasRecords = true;
                        });

                        //ENABLE ddlCounty
                        if (hasRecords) {
                            $(ddlCounty).removeAttr("disabled");
                        }
                        ParseQueryString(qsCounty, ddlCounty);
                    }
                    else {
                        $(ddlCounty).html("");
                    }
                });
            }
        }

        //GetParishes
        function BindParishes(idCounty) {

            //RESET ddlParish
            $(ddlParish).html("");
            $(ddlParish).attr("disabled", "disabled");

            //RESET ddlZone
            $(ddlZone).html("");
            $(ddlZone).attr("disabled", "disabled");


            //IF SELECTED  County
            if (idCounty != 0 && idCounty != "") {

                //GET Parishes via JSON
                $(ddlParish).append($("<option></option>").val(0).html(loadingFeedback));
                $.getJSON(handlersPath + "/GetParishes.ashx?cid=" + idCounty + "&withprop=" + showOnlyWithProperties.toString, function(Parishes) {
                    if (Parishes != null && Parishes != "-1") {
                        $(ddlParish).html("");
                        //APPEND Parishes on ddlParish
                        var hasRecords = false;

                        if (emptyParish != "") $(ddlParish).append($("<option></option>").val(0).html(emptyParish))
                        else $(ddlParish).append($("<option></option>").val(0).html(""))

                        $.each(Parishes, function() {
                            $(ddlParish).append($("<option></option>").val(this['VALUE']).html(this['TEXT']));
                            hasRecords = true;
                        });

                        //ENABLE ddlParishs
                        if (hasRecords) {
                            $(ddlParish).removeAttr("disabled");
                        }

                        ParseQueryString(qsParish, ddlParish);


                    }
                    else {
                        $(ddlParish).html("");
                    }
                });
            }
        }

        function BindZones() {

            //RESET  ddlZone
            $(ddlZone).html("");
            $(ddlZone).attr("disabled", "disabled");

            //GET  ID select Parish
            var idParish = $(ddlParish).val();


            //IF SELECTED  County
            if (idParish != 0) {

                //GET Parishes via JSON
                $(ddlZone).append($("<option></option>").val(0).html(loadingFeedback));

                $.getJSON(handlersPath + "/GetZones.ashx?cid=" + idParish + "&withprop=" + showOnlyWithProperties.toString, function(Zones) {
                    if (Zones != null && Zones != "-1") {
                        $(ddlZone).html("");
                        //APPEND Zones in ddlZone
                        var hasRecords = false;

                        if (emptyZone != "") $(ddlZone).append($("<option></option>").val(0).html(emptyZone))
                        else $(ddlZone).append($("<option></option>").val(0).html(""))

                        $.each(Zones, function() {
                            $(ddlZone).append($("<option></option>").val(this['VALUE']).html(this['TEXT']));
                            hasRecords = true;
                        });

                        //ENABLE ddlParishs
                        if (hasRecords) {
                            $(ddlZone).removeAttr("disabled");
                        }

                        ParseQueryString(qsZone, ddlZone);
                    }
                    else {
                        $(ddlZone).html("");
                    }
                });
            }
        }

        function BindAreas(idGroup) {
            //RESET  Areas
            $(ddlMinArea).html("");
            $(ddlMinArea).attr("disabled", "disabled");

            $(ddlMaxArea).html("");
            $(ddlMaxArea).attr("disabled", "disabled");


            var selectValues = [];
            switch (parseInt(idGroup)) {
                case 0:
                case 222:
                case 223:
                    selectValues.push({ key: "0", value: 0 });
                    selectValues.push({ key: "100", value: 100 });
                    selectValues.push({ key: "150", value: 150 });
                    selectValues.push({ key: "200", value: 200 });
                    selectValues.push({ key: "250", value: 250 });
                    selectValues.push({ key: "300", value: 300 });
                    selectValues.push({ key: "350", value: 350 });
                    selectValues.push({ key: "400", value: 400 });
                    selectValues.push({ key: "450", value: 450 });
                    selectValues.push({ key: "500", value: 500 });
                    selectValues.push({ key: "550", value: 550 });
                    selectValues.push({ key: "600", value: 600 });
                    selectValues.push({ key: "650", value: 650 });
                    selectValues.push({ key: "700", value: 700 });
                    selectValues.push({ key: "750", value: 750 });
                    selectValues.push({ key: "800", value: 800 });
                    selectValues.push({ key: "850", value: 850 });
                    selectValues.push({ key: "900", value: 900 });
                    selectValues.push({ key: "950", value: 950 });
                    selectValues.push({ key: "1.000", value: 1000 });
                    selectValues.push({ key: "> 1.000", value: 0 });
                    break;
                case 1532:
                case 224:
                    selectValues.push({ key: "0", value: 0 });
                    selectValues.push({ key: "500", value: 500 });
                    selectValues.push({ key: "1.000", value: 1000 });
                    selectValues.push({ key: "1.500", value: 1500 });
                    selectValues.push({ key: "2.000", value: 2000 });
                    selectValues.push({ key: "2.500", value: 2500 });
                    selectValues.push({ key: "5.000", value: 5000 });
                    selectValues.push({ key: "10.000", value: 10000 });
                    selectValues.push({ key: "15.000", value: 15000 });
                    selectValues.push({ key: "20.000", value: 20000 });
                    selectValues.push({ key: "25.000", value: 25000 });
                    selectValues.push({ key: "30.000", value: 30000 });
                    selectValues.push({ key: "35.000", value: 35000 });
                    selectValues.push({ key: "40.000", value: 40000 });
                    selectValues.push({ key: "45.000", value: 45000 });
                    selectValues.push({ key: "50.000", value: 50000 });
                    selectValues.push({ key: "> 50.000", value: 0 });
                    break;
                case 225:
                    selectValues.push({ key: "0", value: 0 });
                    selectValues.push({ key: "100", value: 100 });
                    selectValues.push({ key: "150", value: 150 });
                    selectValues.push({ key: "200", value: 200 });
                    selectValues.push({ key: "250", value: 250 });
                    selectValues.push({ key: "300", value: 300 });
                    selectValues.push({ key: "350", value: 350 });
                    selectValues.push({ key: "400", value: 400 });
                    selectValues.push({ key: "450", value: 450 });
                    selectValues.push({ key: "500", value: 500 });
                    selectValues.push({ key: "750", value: 750 });
                    selectValues.push({ key: "1.000", value: 1000 });
                    selectValues.push({ key: "1.500", value: 1500 });
                    selectValues.push({ key: "2.000", value: 2000 });
                    selectValues.push({ key: "2.500", value: 2500 });
                    selectValues.push({ key: "5.000", value: 5000 });
                    selectValues.push({ key: "6.000", value: 6000 });
                    selectValues.push({ key: "7.000", value: 7000 });
                    selectValues.push({ key: "8.000", value: 8000 });
                    selectValues.push({ key: "9.000", value: 9000 });
                    selectValues.push({ key: "10.000", value: 10000 });
                    selectValues.push({ key: "> 10.000", value: 0 });
                    break;
            }

            $.each(selectValues, function() {
                var key = $(this).attr("key");
                var value = $(this).attr("value");
                $(ddlMinArea).
                      append($("<option></option>").
                      attr("value", value).
                      text(key));
                $(ddlMaxArea).
                      append($("<option></option>").
                      attr("value", value).
                      text(key));
            });

            $(ddlMinArea).removeAttr("disabled");
            $(ddlMaxArea).removeAttr("disabled");

            var lastIndexArea = $(ddlMaxArea + " option").index($(ddlMaxArea + " option:last"));
            $(ddlMaxArea + " option:eq(" + lastIndexArea + ")").attr("selected", "selected");

            ParseQueryString(qsMinArea, ddlMinArea);
            ParseQueryString(qsMaxArea, ddlMaxArea);

            SetupRangeField("minArea", "maxArea", "areaRange", isAreaSlider);
        }

        function BindPrices(idPurpose) {
            //RESET  Prices
            $(ddlMinPrice).html("");
            $(ddlMinPrice).attr("disabled", "disabled");

            $(ddlMaxPrice).html("");
            $(ddlMaxPrice).attr("disabled", "disabled");

            var selectValues = [];
            switch (parseInt(idPurpose)) {
                case 1516:
                case 241:
                    selectValues.push({ key: "0", value: 0 });
                    selectValues.push({ key: "250", value: 250 });
                    selectValues.push({ key: "350", value: 350 });
                    selectValues.push({ key: "450", value: 450 });
                    selectValues.push({ key: "500", value: 500 });
                    selectValues.push({ key: "1.000", value: 1000 });
                    selectValues.push({ key: "1.500", value: 1500 });
                    selectValues.push({ key: "2.000", value: 2000 });
                    selectValues.push({ key: "> 2.000", value: 0 });
                    break;
                case 0:
                default:
                    selectValues.push({ key: "0", value: 0 });
                    selectValues.push({ key: "25.000", value: 25000 });
                    selectValues.push({ key: "50.000", value: 50000 });
                    selectValues.push({ key: "75.000", value: 75000 });
                    selectValues.push({ key: "100.000", value: 100000 });
                    selectValues.push({ key: "150.000", value: 150000 });
                    selectValues.push({ key: "200.000", value: 200000 });
                    selectValues.push({ key: "> 200.000", value: 0 });
                    break;
            }

            $.each(selectValues, function() {
                var key = $(this).attr("key");
                var value = $(this).attr("value");
                $(ddlMinPrice).
                      append($("<option></option>").
                      attr("value", value).
                      text(key));
                $(ddlMaxPrice).
                      append($("<option></option>").
                      attr("value", value).
                      text(key));
            });

            $(ddlMinPrice).removeAttr("disabled");
            $(ddlMaxPrice).removeAttr("disabled");

            var lastIndexPrice = $(ddlMaxPrice + " option").index($(ddlMaxPrice + " option:last"));
            $(ddlMaxPrice + " option:eq(" + lastIndexPrice + ")").attr("selected", "selected");


            ParseQueryString(qsMinPrice, ddlMinPrice);
            ParseQueryString(qsMaxPrice, ddlMaxPrice);


            SetupRangeField("minPrice", "maxPrice", "priceRange", isPriceSlider);

        }

        //Setup the typology slider - if exists
        function BindTypologies() {
            var lastIndexTypology = $(ddlTypologyMax + " option").index($(ddlTypologyMax + " option:last"));
            $(ddlTypologyMax + " option:eq(" + lastIndexTypology + ")").attr("selected", "selected");
            SetupRangeField("minTypology", "maxTypology", "typologyRange", isTypologySlider);

        }

        //get a specified key of query string
        function ParseQueryString(key, control) {

            var id = $.jqURL.get(key);

            if (id != "undefined" && id != null) {


                if (key == qsDistrict || key == qsGroup) {
                    $(control).val(id.toString())
                }
                else {
                    if ($(control).css("display") != "none") { selectItem(id, control); }
                }

                switch (key) {
                    case qsDistrict: BindCounties(); break;
                    case qsCounty: BindParishes(id); break;
                    case qsParish: BindZones(); break;
                }
            }
        }

        //select the item of the control with the specified id  (can be string of ids
        function selectItem(id, control) {
            var idArray = id.split(',');
            var i = 0;
            for (i = 0; i < idArray.length; i++) {
                $("select#" + control + " option[value=" + idArray[i].toString() + "]").attr("selected", "selected");
            }
        }

        //Build the query string with the id of the field and the value. If the qString is empty, the token will be ?.
        function BuildQueryStringItem(qString, keyID, keyValue) {
            var qsToken = "&";
            if (qString == "") qsToken = "?"
            qString = qString + qsToken + keyID + "=" + keyValue;
            return qString;
        }

        //save the search in statistics
        function SendSearchToStats(qString) {
            $.getJSON(handlersPath + "/UpdateSearchStats.ashx" + qString, null);
        }

        //Verify if a list of values(mandatoryfields) are filled. if not, the submit is not valid
        function isSubmitValid(aValues) {
            var isValid = true;
            $(aValues).each(function() {
                if ($(this).val() <= 0 || $(this).val() == 'null') { isValid = false; }
                if (isValid == false) { return false; }
            });
            if (isValid == true) { return true; }
        }

        //Setup the sliders,if the control is a slider. if the control is already assign, that will be removed and added again
        function SetupRangeField(controlMin, controlMax, label, isSlider) {
            if (isSlider == "true" && $('select.' + controlMin + ', select.' + controlMax).css("display") != "none") {
                $('select.' + controlMin + ', select.' + controlMax).hide();
                var initialMinValue = $('select.' + controlMin + " option:first").text();
                var initialMaxValue = $('select.' + controlMin + " option:last").text();
                $("span." + label).text(initialMinValue + valuesSeparator + initialMaxValue);
                $('select.' + controlMin + ', select.' + controlMax).selectToUISlider({
                    labels: 0,
                    tooltip: false,
                    sliderOptions: {
                        change: function(e, ui) {
                            var minValue = $('select.' + controlMin + ' option').eq(ui.values[0]).text();
                            var maxValue = $('select.' + controlMax + ' option').eq(ui.values[1]).text();
                            $("span." + label).text(minValue + valuesSeparator + maxValue);
                        }
                    },
                    cssClass: label

                });
            } else {
                //alreadyExists
                if (isSlider == "true") {
                    var initialMinValue = $('select.' + controlMin + " option:first").text();
                    var initialMaxValue = $('select.' + controlMin + " option:last").text();
                    $("span." + label).text(initialMinValue + valuesSeparator + initialMaxValue);
                    $("div." + label).remove();
                    $('select.' + controlMin + ', select.' + controlMax).selectToUISlider({
                        labels: 0,
                        tooltip: false,
                        sliderOptions: {
                            change: function(e, ui) {
                                var minValue = $('select.' + controlMin + ' option').eq(ui.values[0]).text();
                                var maxValue = $('select.' + controlMax + ' option').eq(ui.values[1]).text();
                                $("span." + label).text(minValue + valuesSeparator + maxValue);
                            }
                        },
                        cssClass: label
                    });


                }

            }

        }
    });
    
    
}




