$(document).ready(function() {
    $('.open-modal').click(function(e) {
        var me = $(this);
        e.preventDefault();

        if ( me.data('requestRunning') ) {
            return;
        }

        me.data('requestRunning', true);

        var day = $(this).data('day');
        var menu = $(this).data('menu');
        var name = $(this).data('name');
        var ipa = $(this).data('ipa');
        var japa = $(this).data('japa');
        $.ajax({
            url: 'modal.php',
            type: 'POST',
            data: { ipa: ipa, japa: japa, day: day, menu: menu, name: name },
            success: function(response) {
                $('#my-modal').html(response);
                $('#my-modal').show();
                document.getElementById("modal-overlay").style.display = "block";
            },
            complete: function() {
                me.data('requestRunning', false);
            }
        });
    });

    $('.close').click(function() {
        $('#my-modal').hide();
        document.getElementById("modal-overlay").style.display = "none";
    });
    $('.close-modal').click(function() {
        $('#my-modal').hide();
        document.getElementById("modal-overlay").style.display = "none";
    });
    $(document).keydown(function(event) { 
        if (event.keyCode == 27) { 
            $('#my-modal').hide();
            document.getElementById("modal-overlay").style.display = "none";
        }
    });
});