<?php
/** 
 * @package     VikAppointments - Libraries
 * @subpackage  html.form
 * @author      E4J s.r.l.
 * @copyright   Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link        https://vikwp.com
 */

// No direct access
defined('ABSPATH') or die('No script kiddies please!');

?>

<script>

	(function($) {
		'use script';

		$(function() {
			// iterate all postbox containers generated by VikAppointments
			$('.postbox-container.vap').each(function() {
				// find postbox wrapper
				const postbox = $(this).find('.postbox');

				// find toggle button inside postbox and register click action
				$(this).find('button.toggler').off('click').on('click', function() {
					let status;

					if (postbox.hasClass('closed')) {
						postbox.removeClass('closed');
						$(this).find('i').removeClass('fa-caret-down').addClass('fa-caret-up');
						status = 1;
					} else {
						postbox.addClass('closed');
						$(this).find('i').removeClass('fa-caret-up').addClass('fa-caret-down');
						status = 0;
					}

					const tag = postbox.data('tag');

					if (tag) {
						const date = new Date();
						date.setYear(date.getFullYear() + 1);

						// keep status in a cookie for a year
						document.cookie = tag + '=' + status + '; expires=' + date.toUTCString() + '; path=/';
					}
				});
			});
		});
	})(jQuery);

</script>
