// main.js - Main JavaScript
// File location: /bidwheels.net/assets/js/main.js

$(document).ready(function() {
    // Mobile menu toggle
    $('.mobile-menu-toggle').click(function() {
        $('.navbar').toggleClass('active');
        $('.navbar').slideToggle();
    });
    
    // Smooth scroll for anchor links
    $('a[href*="#"]:not([href="#"])').click(function() {
        if (location.pathname.replace(/^\//, '') === this.pathname.replace(/^\//, '') && location.hostname === this.hostname) {
            var target = $(this.hash);
            target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
            if (target.length) {
                $('html, body').animate({
                    scrollTop: target.offset().top - 100
                }, 1000);
                return false;
            }
        }
    });
    
    // Auto-hide alerts after 5 seconds
    setTimeout(function() {
        $('.alert').fadeOut('slow');
    }, 5000);
    
    // Bid form validation
    $('.bid-form').submit(function(e) {
        var bidAmount = parseFloat($('#bid_amount').val());
        var currentBid = parseFloat($('#current_bid').val());
        var startingPrice = parseFloat($('#starting_price').val());
        var minBid = currentBid > 0 ? currentBid + 1 : startingPrice;
        
        if (bidAmount < minBid) {
            e.preventDefault();
            alert('Bid must be at least $' + minBid.toFixed(2));
            return false;
        }
        
        return true;
    });
    
    // Payment method selection
    $('.payment-method').click(function() {
        $('.payment-method').removeClass('selected');
        $(this).addClass('selected');
        var methodId = $(this).data('id');
        $('#selected_payment_method').val(methodId);
        
        // Load payment form fields via AJAX
        $.ajax({
            url: '/ajax/get_payment_fields.php',
            type: 'POST',
            data: { method_id: methodId },
            success: function(response) {
                $('.payment-fields').html(response);
            }
        });
    });
    
    // Countdown timer for auctions
    function updateCountdown() {
        $('.countdown').each(function() {
            var endTime = $(this).data('end');
            var now = new Date().getTime();
            var distance = new Date(endTime).getTime() - now;
            
            if (distance < 0) {
                $(this).html('Auction Ended');
                $(this).addClass('ended');
                return;
            }
            
            var days = Math.floor(distance / (1000 * 60 * 60 * 24));
            var hours = Math.floor((distance % (86400000)) / (3600000));
            var minutes = Math.floor((distance % (3600000)) / (60000));
            var seconds = Math.floor((distance % (60000)) / 1000);
            
            var display = '';
            if (days > 0) display += days + 'd ';
            display += hours + 'h ' + minutes + 'm ' + seconds + 's';
            
            $(this).html(display);
        });
    }
    
    updateCountdown();
    setInterval(updateCountdown, 1000);
    
    // Image gallery
    $('.gallery-thumb').click(function() {
        var imgSrc = $(this).data('full');
        $('.main-image img').attr('src', imgSrc);
        $('.gallery-thumb').removeClass('active');
        $(this).addClass('active');
    });
    
    // Search form
    $('.search-form').submit(function(e) {
        var query = $('.search-input').val().trim();
        if (query === '') {
            e.preventDefault();
            alert('Please enter a search term');
            return false;
        }
        return true;
    });
});