﻿//=================== UI Multiselect Code ====================//
$(document).ready(function () {

    if ($('div#modify-results').length) {
        // setup tabs
        $('.tabs').tabs({
            classNamePanelCurrent: "current-panel"
        });

        // setup multi-selects
        $('.bedrooms-bathrooms select, .country-select, .state-select, .city-select, .neighborhood-select, #type-of-dollar').multiselect({
            multiple: false,
            header: false,
            selectedList: 1,
            height: "auto"
        });

        $('.bedrooms-bathrooms div.ui-multiselect-menu').css("width", "54px");
        $('#modify-results-price div.ui-multiselect-menu').css({ "max-height": "150px", "overflow-y": "auto" });

        /*$('#modify-results #property-type-select, #modify-results #preferred-lifestyle').multiselect({
        autoOpen: true,
        header: false,
        noneSelectedText: "Any",
        height: "auto"
        });*/

        //$('#modify-results .ui-multiselect-menu').not('.ui-multiselect-single').append('<button type="button" class="select-button"></button>');
        //$('#modify-results .ui-multiselect-menu').not('.ui-multiselect-single').append('<button type="button" class="bt bt-gray select-button"><span>Select</span></button>');
        //$('#modify-results .ui-multiselect-menu .select-button').live("click", function () {
        //    $('#modify-results .ui-multiselect-menu').css('display', 'none');
        //});

        var chosenBedroomVal = $("#bedrooms-amount option[value=" + SearchResultsJS.SelectedBedrooms + "]").text();
        $("#bedrooms-amount").val(SearchResultsJS.SelectedBedrooms).next(".ui-multiselect").find("span:eq(1)").text(chosenBedroomVal);
        var chosenBathroomVal = $("#bathrooms-amount option[value=" + SearchResultsJS.SelectedBathrooms + "]").text();
        $("#bathrooms-amount").val(SearchResultsJS.SelectedBathrooms).next(".ui-multiselect").find("span:eq(1)").text(chosenBathroomVal);
        //$("div#bedrooms-amount-inputPair").find("input[value=" + SearchResultsJS.SelectedBedrooms + "]").click();
        //$("div#bathrooms-amount-inputPair").find("input[value=" + SearchResultsJS.SelectedBathrooms + "]").click();

        //Setting the filter-changed flag for Bedrooms, Bathrooms, and Currency
        $('.bedrooms-bathrooms select, select#type-of-dollar').multiselect({
            close: function () {
                SearchResultsJS.HasFilterChanged = true;
                SearchResultsJS.UpdateSearchData();
            }
        });

        //Setting the filter-changed flag for Property-types and Lifestyles
        $('#checkbox-group-property-type label input, #checkbox-group-lifestyle label input').click(function () {
            SearchResultsJS.HasFilterChanged = true;
            SearchResultsJS.UpdateSearchData();
        });

        //Setting the filter-changed flag for the Keywords
        var charLength = 0;
        var oldLength = $("input#keywords").val().length;
        var lengthChanged = false;
        $("input#keywords").keyup(function (e) {
            var keyword = $(this).val();
            keyword = keyword.replace(/^\s+/, "");

            //Check to see if the user changed the keyword and sets the lengthChanged flag
            charLength = keyword.length;
            if (charLength != oldLength) {
                lengthChanged = true;
                oldLength = charLength;
                SearchResultsJS.HasFilterChanged = true;
                SearchResultsJS.UpdateSearchData();
            }
        });

        //Setting the filter-changed flag for the Specific Price Range
        var charLength = 0;
        var oldLength = $("input#minimum-price").val().length;
        var lengthChanged = false;
        $("input#minimum-price, input#maximum").keyup(function (e) {
            var price = $.trim($(this).val());
            //price = price.replace(",", "");
            var comma = new RegExp(',');
            while (comma.test(price)) {
                price = price.replace(comma, '');
            }

            //Check to see if the user changed the price and sets the lengthChanged flag
            charLength = price.length;
            if (!isNaN(price) && charLength != oldLength) {
                lengthChanged = true;
                oldLength = charLength;
                SearchResultsJS.HasFilterChanged = true;
                SearchResultsJS.UpdateSearchData();
            }
        });

        //Setting the flag for the zip-code distance and distance-unit dropdowns
        $("select.amount-select").multiselect({
            close: function () {
                var index = $(this).attr("id").substring($(this).attr("id").length - 1);
                var locationName = $(this).parent().parent().find("div.selection span").text();
                var locationID = $('input.autoCompletes').filter(function () { return $(this).attr("location") == locationName; }).attr("id");
                var locationPos = locationID.substring(locationID.length - 1);
                $("input#autoComplete" + locationPos).attr("distance", $("select#search-state-within-amount" + index).val());
                $("input#autoComplete" + locationPos).attr("distance-unit", $("select#search-state-within-unit" + index).val());
                $("input#autoComplete" + locationPos).attr("zipwithinonly", "0");
                $(this).parent().find("input#search-state-within" + index).attr("checked", true);
                SearchResultsJS.HasFilterChanged = true;
                SearchResultsJS.UpdateSearchData();
            }
        });


        //----- Handling the money slider and currency
        var currencySign = "$";
        var convertMil = function (amount) {
            milSymbol = "M+";
            if (amount != SearchResultsJS.PriceRangeMax) {
                milSymbol = "M";
            }
            if (amount.toString().match(/[0-9]{7,}$/)) {
                return (amount / 1000000.0) + milSymbol;
            }
            if (amount >= 100000) {
                return (amount / 1000.0) + "K+";
            }            
            if ((amount >= 1000) && (amount < 100000)) {
                formatted = amount.toString();
                var expression = new RegExp('([0-9]+)([0-9]{3})');
                while (expression.test(formatted))
                    formatted = formatted.replace(expression, '$1,$2');
                amount = formatted;
            }
            return amount;
        };

        // set up currency
        $("select#type-of-dollar").multiselect({
            multiple: false,
            click: function (event, ui) {
                currencySign = "$";
                if (ui.value != "USD") {
                    currencySign = "";
                }
                leftVal = convertMil($(".slider-range").slider("values", 0));
                rightVal = convertMil($(".slider-range").slider("values", 1));
                $("#slider-amount-advanced-search").val(currencySign + leftVal + " - " + currencySign + rightVal);
            }
        });

        // set up slider
        $("#slider-price-range .slider-range").slider({
            range: true,
            min: SearchResultsJS.PriceRangeMin,
            max: SearchResultsJS.PriceRangeMax,
            step: SearchResultsJS.PriceRangeStep,
            values: [SearchResultsJS.PriceRangeStart, SearchResultsJS.PriceRangeEnd],
            slide: function (event, ui) {
                leftVal = convertMil(ui.values[0]);
                rightVal = convertMil(ui.values[1]);
                $("#slider-amount-advanced-search").val(currencySign + leftVal + " - " + currencySign + rightVal);
                SearchResultsJS.HasFilterChanged = true;
                SearchResultsJS.UpdateSearchData();
            }
        });
        $("#slider-amount-advanced-search").attr("disabled", true).css("text-align", "center");
        minVal = $(".slider-range").slider("values", 0);
        maxVal = $(".slider-range").slider("values", 1);
        $("#slider-amount-advanced-search").val(currencySign + convertMil(minVal) + " - " + currencySign + convertMil(maxVal));

        // set up specific price range
        $('.enter-specific-price-range').revealNew({
            classShown: 'enter-specific-price-range-shown',
            show: "slideDown", showOptions: { duration: 300, easing: "easeOutQuart" },
            hide: "slideUp", hideOptions: { duration: 300, easing: "easeOutQuart" }
        }).click(function () {
            $('#slider-price-range').toggle();
        });
    }

});         //end document-ready
