jQuery( document ).ready( function() {
	jQuery( 'form#vehicle-filter select, form#vehicle-filter input' ).on( 'change', function() {
		jQuery( 'form#vehicle-filter' ).submit();
	} );
});

// Dual Range Slider
(function () {
    function initRangeSlider( container ) {
        const track = container.querySelector( '.dual-range-slider-track' );
        const inputs = track.querySelectorAll( 'input[type="range"]' );
        const minInput = inputs[0];
        const maxInput = inputs[1];
        const outputs = container.querySelectorAll( 'output' );
        const minOutput = outputs[0];
        const maxOutput = outputs[1];

        function formatNumber( value ) {
            return parseInt( value ).toLocaleString( 'de-DE' );
        }

        function update() {
            const min = parseInt( minInput.value );
            const max = parseInt( maxInput.value );
            const total = parseInt( minInput.max ) - parseInt( minInput.min );
            const offset = parseInt( minInput.min );
            const leftPct = ( ( min - offset ) / total ) * 100;
            const rightPct = ( ( max - offset ) / total ) * 100;

            track.style.setProperty( '--fill-left', leftPct + '%' );
            track.style.setProperty( '--fill-width', (rightPct - leftPct) + '%' );

            minOutput.value = formatNumber( min );
            maxOutput.value = formatNumber( max );
        }

        minInput.addEventListener( 'input', function () {
            if ( parseInt( this.value ) >= parseInt( maxInput.value ) - parseInt( this.step ) ) {
                this.value = parseInt( maxInput.value ) - parseInt( this.step );
            }
            update();
        });

        maxInput.addEventListener( 'input', function () {
            if ( parseInt( this.value ) <= parseInt( minInput.value ) + parseInt( this.step ) ) {
                this.value = parseInt( minInput.value ) + parseInt( this.step );
            }
            update();
        });

        update();
    }

    document.querySelectorAll('.dual-range-slider').forEach( initRangeSlider );
})();