/*! ICS Calendar Pro admin scripts https://icscalendar.com */

jQuery(function() {

	// Copy text from input to clipboard
	jQuery('input + .icon.copy').on('click', function() {
		jQuery(this).siblings('input').select();
		document.execCommand('copy');
		jQuery(this).html('<span class="dashicons dashicons-yes" title="' + r34icspro_admin_vars.i18n['Copied'] + '"></span>');
		setTimeout(function() {
			jQuery('input + .icon.copy').html('<span class="dashicons dashicons-admin-page" title="' + r34icspro_admin_vars.i18n['Copy to clipboard...'] + '"></span>');
		}, 2000);
	});
	
	// Only allow valid HTML id characters in GUID field
	if (jQuery('.acf-field[data-name="guid"] input[type="text"]').length > 0) {
		jQuery('.acf-field[data-name="guid"] input[type="text"]').on('keyup change', function() {
			var new_val = jQuery(this).val().toLowerCase().replace(/(^[0-9\W]+|[^\w\-])/g,'');
			if (new_val != jQuery(this).val()) {
				jQuery(this).val(new_val);
			}
		});
	}

	// Admin "Add ICS Calendar" button Pro options -- replace regular insertion fields with saved calendar selector
	if (jQuery('.field-block').length > 0) {
		jQuery('.field-block:not(.saved_calendar)').hide();
		jQuery('#insert_r34ics_form').addClass('saved_calendar');
	}
	jQuery('#insert_r34ics_form').on('submit', function() {
		// Saved calendars are handled by the Pro plugin
		if (!jQuery(this).hasClass('saved_calendar')) { return false; }
		// Validate required fields
		if (jQuery('#insert_r34ics_id').val() == '') {
			alert(r34icspro_admin_vars.i18n['Please select a saved calendar from the menu.']);
			jQuery('#insert_r34ics_id').focus();
			return false;
		}
		// Concatenate shortcode
		var r34ics_shortcode = '[ics_calendar id="' + parseInt(jQuery('#insert_r34ics_id').val()) + '"]';
		// Insert shortcode and close window
		window.send_to_editor(r34ics_shortcode);
		jQuery('#insert_r34ics_form')[0].reset();
		jQuery('#r34ics_list_view_options').hide();
		jQuery('#insert_r34ics').removeClass('open');
		return false;
	});
	
	// Accessible interactions for help icons
	jQuery('.r34icspro-help').on('click', function() { return false; });
	jQuery('.r34icspro-help a').on('click', function(e) { e.stopPropagation(); return true; });
	
});

jQuery(window).on('load', function() {

	// Force Settings page to load to License tab if unlicensed
	if (jQuery('.wrap.r34ics #license').length > 0 && jQuery('#r34icspro-activate-license').length > 0) {
		jQuery('.r34ics-menu a[href="#license"]').trigger('click');
	}
	
});

// ACF input field customizations
if (typeof acf !== 'undefined') {

	// Date and Date Time Picker
	var r34icspro_acf_date_time_picker_keys = [
		'field_66686d344c404', // start_date
		'field_66686d644c405', // end_date
		'field_66686d754c406', // start_datetime
		'field_66686dc64c407', // end_datetime
	];
	acf.addFilter('date_picker_args', function(args, field) {
		if (r34icspro_acf_date_time_picker_keys.indexOf(field.data.key) != -1) {
			args.onSelect = r34icspro_event_date_time_picker_select;
		}
		return args;
	});
	acf.addFilter('date_time_picker_args', function(args, field) {
		if (r34icspro_acf_date_time_picker_keys.indexOf(field.data.key) != -1) {
			var step_minute = 5;
			if (typeof r34icspro_admin_vars['ics_events_step_minute'] != 'undefined') {
				step_minute = parseInt(r34icspro_admin_vars['ics_events_step_minute']);
			}
			args.onSelect = r34icspro_event_date_time_picker_select;
			args.showSecond = false;
			args.stepMinute = step_minute;
			// Workaround for ACF French time format ("G\hi") backslash bug in rendered datetime
			if (args.timeFormat == 'H\\hhmm') {
				args.pickerTimeFormat = 'H:mm';
				args.timeFormat = "H'h'mm";
			}
		}
		return args;
	});
	
}

function r34icspro_event_date_time_picker_select(datetimeText, datepickerInstance) {
	var field_name = jQuery(this).closest('.acf-field').data('name');
	// Compare start and end date/time, to prevent end from being earlier than start
	if (typeof field_name != 'undefined' && (field_name.indexOf('start_') == 0 || field_name.indexOf('end_') == 0)) {
		var start_name, start_display, start_hidden, end_name, end_display, end_hidden;
		if (field_name.indexOf('start_') == 0) {
			start_display = jQuery(this);
			start_hidden = jQuery(this).siblings('input[type="hidden"]');
			end_name = field_name.replace('start', 'end');
			end_display = jQuery('.acf-field[data-name="' + end_name + '"]').find('input[type="text"]');
			end_hidden = jQuery('.acf-field[data-name="' + end_name + '"]').find('input[type="hidden"]');
			if (end_hidden.val().localeCompare(start_hidden.val()) == -1) {
				end_display.val(start_display.val());
				end_hidden.val(start_hidden.val());
			}
		}
		else {
			end_display = jQuery(this);
			end_hidden = jQuery(this).siblings('input[type="hidden"]');
			start_name = field_name.replace('end', 'start');
			start_display = jQuery('.acf-field[data-name="' + start_name + '"]').find('input[type="text"]');
			start_hidden = jQuery('.acf-field[data-name="' + start_name + '"]').find('input[type="hidden"]');
			// Change end to match start if end is earlier than start
			if (end_hidden.val().localeCompare(start_hidden.val()) == -1) {
				end_display.val('');
				end_hidden.val('');
				// Show alert when end date/time is set earlier than start date/time
				if (field_name.indexOf('end_') == 0) {
					alert(r34icspro_admin_vars.i18n['End date/time must occur after start date/time. Please select another end date/time.']);
					return false;
				}
			}
		}
	}
}

function r34icspro_set_iris_palette(palette_name) {
	if (typeof r34icspro_admin_vars != 'undefined' && typeof r34icspro_admin_vars['palettes'][palette_name] != 'undefined') {
		var palette_source = r34icspro_admin_vars['palettes'][palette_name];
		var palette = [
			palette_source['white'],
			palette_source['whitesmoke'],
			palette_source['gainsboro'],
			palette_source['darkgray'],
			palette_source['gray'],
			palette_source['dimgray'],
			palette_source['black']
		];
		return palette;
	}
	return false;
}