(function( $ ) {
	'use strict';
    
    $(document).ready(function() {

        // After delay, open the promo (full or closed depending on state)
        setTimeout(function() {

            $('.olympus-promo-card').each(function(){
                const promo_id = $(this).data('promoid');
                const promo_card_state = localStorage.getItem('promo_state_' + promo_id);
    
                if(promo_card_state == 'closed'){
                    $(this).find('.olympus-promo-card-closed').first().css("display", "flex").hide().fadeIn(200);
                }else{
                    $(this).find('.olympus-promo-card-open').first().css("display", "flex").hide().fadeIn(200);
                }

            });

        }, 2000);

        // Listen for open
        $('.olympus-promo-card').each(function(){
            const promo_wrapper = $(this);

            promo_wrapper.find('.olympus-promo-card-closed').first().on('click', function(){
                $(this).fadeOut(200, function(){
                    promo_wrapper.find('.olympus-promo-card-open').first().css("display", "flex").hide().fadeIn(200);
                });
            });

        });

        // Listen for close
        $('.olympus-promo-card').each(function(){
            const promo_wrapper = $(this);
            const promo_id = $(this).data('promoid');

            promo_wrapper.find('.olympus-promo-card-close-btn').first().on('click', function(){
                localStorage.setItem('promo_state_' + promo_id, 'closed');

                promo_wrapper.find('.olympus-promo-card-open').fadeOut(200, function(){
                    promo_wrapper.find('.olympus-promo-card-closed').first().css("display", "flex").hide().fadeIn(200);
                });
            });

        });
    });

})( jQuery );