/* Minification failed. Returning unminified contents.
(85,43-44): run-time warning JS1195: Expected expression: >
(85,113-114): run-time warning JS1004: Expected ';': )
(92,17-18): run-time warning JS1193: Expected ',' or ')': }
(93,13-14): run-time warning JS1195: Expected expression: )
(96,9-10): run-time warning JS1002: Syntax error: }
(98,45-46): run-time warning JS1195: Expected expression: )
(101,9-10): run-time warning JS1002: Syntax error: }
(103,43-44): run-time warning JS1195: Expected expression: )
(105,9-10): run-time warning JS1002: Syntax error: }
(107,44-45): run-time warning JS1195: Expected expression: )
(113,9-10): run-time warning JS1002: Syntax error: }
(118,5-6): run-time warning JS1002: Syntax error: }
(109,17-29): run-time warning JS1018: 'return' statement outside of function: return true;
(111,17-30): run-time warning JS1018: 'return' statement outside of function: return false;
 */
//create or use NADAjs as "namespace"
var NADAjs = NADAjs || {};

NADAjs.AutotraderSearchCarsForSale = (function () {
    var init = function () {
        //$.getJSON("/Cars/Ajax/HP/GetATCMakes", function (data) {
        //    data = $.map(data, function (item, a) {
        //        return "<option value=" + item.Value + ">" + item.Text + "</option>";
        //    });
        //    $('#atcMake').html(data.join(""));
        //});

        $("#atcMake").change(function () {
            var makeName = $('#atcMake').val();
            $('#atcModel').html('');
            $.getJSON("/Cars/Ajax/HP/GetATCModels", { makeName: makeName }, function (data) {
                data = $.map(data, function (item, a) {
                    return "<option value=" + item.Value + ">" + item.Text + "</option>";
                });
                $('#atcModel').html(data.join("")).prop('disabled', false).parent('.custom-select--redesign').removeClass('custom-select--redesign-disabled');
            });
        });

        $('#atcModel').change(function () {
            $('#atcZip').prop('disabled', false);
        });

        //log autotrader clicked
        $('.js-at-click-time').click(function () {
             
            try {
                var atClickedTime = NADAjs.CookieHelper.getCookie('at_clicked_time');
                if (Math.round(new Date().getTime() / 1000) - atClickedTime > 1800)
                {
                    var expireDate = new Date(); expireDate.setMinutes(expireDate.getMinutes() + 30);
                    NADAjs.CookieHelper.setCookie('at_clicked_time', Math.round(new Date().getTime() / 1000), expireDate);
                }

            }
            catch (err) { }
        });

        $('body').bind('zipSuccess', function (data, param) {
            $('#txtATCZipHP').val(param);
        });
    };

    return {
        init: init
    };
})();//AutotraderSearchCarsForSale

NADAjs.CarsComSearchCarsForSale = (function () {
    var init = function () {
        var updateCTA = function () {
            var linkHref = 'https://www.cars.com/for-sale/searchresults.action/?mdId=' + $('#carsComModel option:selected').data('modelid') + '&mkId=' + $('#carsComMake option:selected').data('makeid') + '&rd=99999&searchSource=QUICK_FORM&stkTypId=28881&zc=' + $('#carsComZip').val();
            var inputCounts = $('.cars-com-search').find('select').length;
            $('.cars-com-search').find('select').each(function () {
                inputCounts = $(this).val() ? inputCounts - 1 : inputCounts;
            });
            
            if (inputCounts === 0) {
                $('.cars-com-cta').removeClass('button-disabled').attr('href', linkHref);
            } else {
                $('.cars-com-cta').addClass('button-disabled').attr('href', '#');
            }
        }

        $('#carsComMake').change(function () {
            var makeName = $(this).val();
            $('#carsComModel').html('<option value="" data-makeid="">Select a Model</option>');
            $('#carsComModel').prop('disabled', true).parent('.custom-select--redesign').addClass('custom-select--redesign-disabled');
            $('#carsComZip').prop('disabled', true);
            $.ajax(
                {
                    async: true,
                    type: "POST",
                    dataType: 'json',
                    url: '/Cars/Ajax/GetCarDotComModels/' + makeName,
                    timeout: 30000000,
                    error: function (error) {
                        console.log(error);
                    },
                    success: function (data) {
                        data.sort((a, b) => (a.DisplayName.toLowerCase() > b.DisplayName.toLowerCase()) ? 1 : -1);
                        $.each(data, function (i, val) {
                            $('#carsComModel').append('<option value="' + val.DisplayName + '" data-modelid="' + val.ModelId + '">' + val.DisplayName + '</option>');
                        });

                        $('#carsComModel').prop('disabled', false).parent('.custom-select--redesign').removeClass('custom-select--redesign-disabled');
                    }
                }
            );

            updateCTA();
        });

        $('#carsComModel').change(function () {
            $('#carsComZip').prop('disabled', false);
            updateCTA();
        });

        $('#carsComZip').change(function () {
            updateCTA();
        });

        $('.cars-com-cta').click(function () {
            if (!$(this).hasClass('button-disabled')) {
                return true;
            } else {
                return false;
            }
        });
    };

    return {
        init: init
    };
})();//CarsComSearchCarsForSale;