// Royal Event Options scripts

jQuery(document).ready(function(){
	"use strict";

	ROYAL_EVENT_STORAGE['to_media_frame'] = [];
	
	// Init fields and groups
	//----------------------------------------------------------------
	royal_event_options_init(jQuery('.royal_event_options_body'));

	// Check top section for fixed position
	//----------------------------------------------------------------
	royal_event_options_fix_scroll_menu();

	// Override menu
	//----------------------------------------------------------------
	jQuery('.royal_event_options').on('click', '.royal_event_options_override_title', function (e) {
		jQuery(this).toggleClass('opened').parents('.royal_event_options_title').find('.royal_event_options_override_menu').slideToggle();
		e.preventDefault();
		return false;
	});

	// Save options
	//----------------------------------------------------------------
	jQuery('.royal_event_options').on('click', '.royal_event_options_button_save', function (e) {
		"use strict";
		// Save editors content
		if (typeof(tinymce) != 'undefined') {
			var editor = tinymce.activeEditor;
			if ( editor!=null && 'mce_fullscreen' == editor.id )
				tinymce.get('content').setContent(editor.getContent({format : 'raw'}), {format : 'raw'});
			tinymce.triggerSave();
		}
		// Prepare data
		var data = {
			action: 'royal_event_options_save',
			nonce: ROYAL_EVENT_STORAGE['ajax_nonce'],
			data: jQuery(".royal_event_options_form").serialize(),
			override: ROYAL_EVENT_STORAGE['to_override'],
			slug: ROYAL_EVENT_STORAGE['to_slug'],
			mode: "save"
		};
		setTimeout(function(){
			royal_event_message_info(ROYAL_EVENT_STORAGE['to_override']=='customizer' ? ROYAL_EVENT_STORAGE['to_strings']['recompile_styles'] : '', ROYAL_EVENT_STORAGE['to_strings']['wait'], 'spin3 animate-spin', 60000);
		}, ( data.mode != "undefined" && data.mode == "save" ) ? 10 : 600 );
		jQuery.post(ROYAL_EVENT_STORAGE['ajax_url'], data, function(response) {
			"use strict";
			royal_event_message_success(ROYAL_EVENT_STORAGE['to_override']=='customizer' ? ROYAL_EVENT_STORAGE['to_strings']['reload_page'] : '', ROYAL_EVENT_STORAGE['to_strings']['save_options']);
			if (ROYAL_EVENT_STORAGE['to_override']=='customizer') setTimeout(function() { location.reload(); }, 3000);
		});
		e.preventDefault();
		return false;
	});

	
	// Reset options
	//----------------------------------------------------------------
	jQuery('.royal_event_options').on('click', '.royal_event_options_button_reset', function (e) {
		"use strict";
		royal_event_message_confirm(ROYAL_EVENT_STORAGE['to_strings']['reset_options_confirm'], ROYAL_EVENT_STORAGE['to_strings']['reset_options'], function(btn) {
			"use strict";
			if (btn != 1) return;
			var data = {
				action: 'royal_event_options_save',
				nonce: ROYAL_EVENT_STORAGE['ajax_nonce'],
				override: ROYAL_EVENT_STORAGE['to_override'],
				slug: ROYAL_EVENT_STORAGE['to_slug'],
				mode: "reset"
			};
			setTimeout(function(){
				royal_event_message_info(ROYAL_EVENT_STORAGE['to_override']=='customizer' ? ROYAL_EVENT_STORAGE['to_strings']['recompile_styles'] : '', ROYAL_EVENT_STORAGE['to_strings']['wait'], 'spin3 animate-spin', 60000);
			}, 600);
			jQuery.post(ROYAL_EVENT_STORAGE['ajax_url'], data, function(response) {
				"use strict";
				royal_event_message_success(ROYAL_EVENT_STORAGE['to_strings']['reset_options_complete']+'<br>'+ROYAL_EVENT_STORAGE['to_strings']['reload_page'], ROYAL_EVENT_STORAGE['to_strings']['reset_options']);
				setTimeout(function() { location.reload(); }, 3000);
			});
			
		});
		e.preventDefault();
		return false;
	});


	// Export options
	//----------------------------------------------------------------
	jQuery('.royal_event_options').on('click', '.royal_event_options_button_export,.royal_event_options_button_import', function (e) {
		"use strict";
		var action = 'import';
		if (jQuery(this).hasClass('royal_event_options_button_export')) {
			action = 'export';
			// Save editors content
			if (typeof(tinymce) != 'undefined') {
				var editor = tinymce.activeEditor;
				if ( editor!=null && 'mce_fullscreen' == editor.id )
					tinymce.get('content').setContent(editor.getContent({format : 'raw'}), {format : 'raw'});
				tinymce.triggerSave();
			}
		}
		// Prepare dialog
		var html = '<div class="royal_event_options_export_set_name">'
			+'<form>'
			+(action=='import' 
				? ''
				: '<div class="royal_event_options_export_name_area">'
					+'<label for="royal_event_options_export_name">'+ROYAL_EVENT_STORAGE['to_strings']['export_options_label']+'</label>'
					+'<input id="royal_event_options_export_name" name="royal_event_options_export_name" class="royal_event_options_export_name" type="text">'
					+'</div>');
		var export_list = ROYAL_EVENT_STORAGE['to_export_list'];
		if (export_list.length > 0) { 
			html += '<div class="royal_event_options_export_name2_area">'
				+'<label for="royal_event_options_export_name2">'+(action=='import' ? ROYAL_EVENT_STORAGE['to_strings']['export_options_label'] : ROYAL_EVENT_STORAGE['to_strings']['export_options_label2'])+'</label>'
				+'<select id="royal_event_options_export_name2" name="royal_event_options_export_name2" class="royal_event_options_export_name2">'
				+'<option value="">'+ROYAL_EVENT_STORAGE['to_strings']['export_options_select']+'</option>';
			for (var i=0; i<export_list.length; i++) {
				html += '<option value="'+export_list[i]+'">'+export_list[i]+'</option>';
			}
			html += '</select>'
				+'</div>';
		} else if (action=='import') {
			html += '<div class="royal_event_options_export_empty">'+ROYAL_EVENT_STORAGE['to_strings']['export_empty']+'</div>';
		}
		if (action=='import') {
			html += '<div class="royal_event_options_export_textarea">'
				+'<label for="royal_event_options_export_data">'+ROYAL_EVENT_STORAGE['to_strings']['import_options_label']+'</label>'
				+'<textarea id="royal_event_options_export_data" name="royal_event_options_export_data" class="royal_event_options_export_data"></textarea>'
				+'</div>';
		}
		html += '</form>'
			+'</div>';

		// Show Dialog popup
		var export_popup = royal_event_message_dialog(html, action=='import' ? ROYAL_EVENT_STORAGE['to_strings']['import_options_header'] : ROYAL_EVENT_STORAGE['to_strings']['export_options_header'],
			function(popup) {
				"use strict";
				// Init code
			},
			function(btn, popup) {
				"use strict";
				if (btn != 1) return;

				var val2 = export_popup.find('#royal_event_options_export_name2').val();

				if (action=='import') {			// Import settings
					
					var text = export_popup.find('#royal_event_options_export_data').val();

					if (val2=='' && text=='') {
						royal_event_message_warning(ROYAL_EVENT_STORAGE['to_strings']['import_options_error'], ROYAL_EVENT_STORAGE['to_strings']['import_options_header']);
						return;
					}
					
					var data = {
						action: 'royal_event_options_import',
						nonce: ROYAL_EVENT_STORAGE['ajax_nonce'],
						name2: val2,
						text: text,
						override: ROYAL_EVENT_STORAGE['to_override'],
						slug: ROYAL_EVENT_STORAGE['to_slug']
					};
					jQuery.post(ROYAL_EVENT_STORAGE['ajax_url'], data, function(response) {
						"use strict";
						var rez = {};
						try {
							rez = JSON.parse(response);
						} catch (e) {
							rez = { error: ROYAL_EVENT_STORAGE['ajax_error'] };
							console.log(response);
						}
						if (rez.error === '') {
							royal_event_options_import_values(rez.data);
							royal_event_message_success(ROYAL_EVENT_STORAGE['to_strings']['import_options'], ROYAL_EVENT_STORAGE['to_strings']['import_options_header']);
						} else {
							royal_event_message_warning(ROYAL_EVENT_STORAGE['to_strings']['import_options_failed'], ROYAL_EVENT_STORAGE['to_strings']['import_options_header']);
						}
					});
					

				} else {						// Export settings

					var val = export_popup.find('#royal_event_options_export_name').val();
					if (val=='' && val2=='') {
						royal_event_message_warning(ROYAL_EVENT_STORAGE['to_strings']['export_options_error'], ROYAL_EVENT_STORAGE['to_strings']['export_options_header']);
						return;
					}
					// Prepare data
					var form = null;
					if (jQuery("form.royal_event_options_form").length === 1) {		// Main theme options
						form = jQuery("form.royal_event_options_form");
					} else if (jQuery("form#addtag").length === 1 ) {				// Options for the category (add new)
						form = jQuery("form#addtag");
					} else if (jQuery("form#edittag").length === 1 ) {				// Options for the category (edit)
						form = jQuery("form#edittag");
					} else if (jQuery("form#post").length === 1 ) {					// Options for the post or page
						form = jQuery("form#post");
					}
					var data = {
						action: 'royal_event_options_save',
						nonce: ROYAL_EVENT_STORAGE['ajax_nonce'],
						data: form.serialize(),
						name: val,
						name2: val2,
						mode: 'export',
						override: ROYAL_EVENT_STORAGE['to_override'],
						slug: ROYAL_EVENT_STORAGE['to_slug']
					};
					jQuery.post(ROYAL_EVENT_STORAGE['ajax_url'], data, function(response) {
						"use strict";
						var rez = {};
						try {
							rez = JSON.parse(response);
						} catch (e) {
							rez = { error: ROYAL_EVENT_STORAGE['ajax_error'] };
							console.log(response);
						}
						royal_event_message_success(ROYAL_EVENT_STORAGE['to_strings']['export_options']+'<br>'+ROYAL_EVENT_STORAGE['to_strings']['export_link'].replace('%s', '<br><a target="_blank" href="'+rez.link+'">'+ROYAL_EVENT_STORAGE['to_strings']['export_download']+'</a>'), ROYAL_EVENT_STORAGE['to_strings']['export_options_header']);
						if (val!='') {
							if (val2!='') {
								for (var i=0; i<ROYAL_EVENT_STORAGE['to_export_list'].length; i++) {
									if (ROYAL_EVENT_STORAGE['to_export_list'][i] == val2) {
										ROYAL_EVENT_STORAGE['to_export_list'][i] = val;
										break;
									}
								}
							} else
								ROYAL_EVENT_STORAGE['to_export_list'].push(val);
						}
					});
				}
			});
		e.preventDefault();
		return false;
	});

});


// Init all elements
//-----------------------------------------------------------------
function royal_event_options_init(to_body) {
	"use strict";
	ROYAL_EVENT_STORAGE['to_body'] = to_body;

	// Init Dependencies
	//----------------------------------------------------------------
	// Add data-param to all editor areas
	to_body.find('.wp-editor-area').each(function() {
		"use strict";
		jQuery(this).attr('data-param', jQuery(this).attr('id'));
	});

	// Check dependencies
	to_body.on('change', '[data-param]', function() {
		"use strict";
		var cont = jQuery(this).parents('.royal_event_options_tab_content');
		if (cont.length==0) cont = jQuery(this).parents('.royal_event_options_partition_content');
		royal_event_options_check_dependency(cont);
	});
	
	// Popups init
	//----------------------------------------------------------------
	royal_event_options_popup_init(to_body);

	// Tabs and partitions init
	//----------------------------------------------------------------
	to_body.find('.royal_event_options_tab,.royal_event_options_partition').tabs({
		// Init options, which depends from width() or height() only after open it's parent tab or partition
		create: function(e, ui) {
			"use strict";
			if (ui.panel) {
				royal_event_options_init_hidden_elements(ui.panel);
				if (window.royal_event_init_hidden_elements) royal_event_init_hidden_elements(ui.panel);
			}
		},
		activate: function(e, ui) {
			"use strict";
			if (ui.newPanel) {
				royal_event_options_init_hidden_elements(ui.newPanel);
				if (window.royal_event_init_hidden_elements) royal_event_init_hidden_elements(ui.newPanel);
			}
		}
	});


	// Accordion init
	//----------------------------------------------------------------
	to_body.find('.royal_event_options_accordion').accordion({
		header: ".royal_event_options_accordion_header",
		collapsible: true,
		heightStyle: "content",
		// Init options, which depends from width() or height() only after open it's parent accordion
		create: function (e, ui) {
			if (ui.panel) {
				royal_event_options_init_hidden_elements(ui.panel);
				if (window.royal_event_init_hidden_elements) royal_event_init_hidden_elements(ui.panel);
			}
		},
		activate: function (e, ui) {
			if (ui.newPanel) {
				royal_event_options_init_hidden_elements(ui.newPanel);
				if (window.royal_event_init_hidden_elements) royal_event_init_hidden_elements(ui.newPanel);
			}
		}
	});


	// Toggles
	//----------------------------------------------------------------
	to_body.on('click', '.royal_event_options_toggle .royal_event_options_toggle_header', function () {
		"use strict";
		if (jQuery(this).hasClass('ui-state-active')) {
			jQuery(this).removeClass('ui-state-active');
			jQuery(this).siblings('div').slideUp();
		} else {
			jQuery(this).addClass('ui-state-active');
			jQuery(this).siblings('div').slideDown();
			royal_event_options_init_hidden_elements(jQuery(this));
			if (window.royal_event_init_hidden_elements) royal_event_init_hidden_elements(jQuery(this));
		}
	});

	// Masked input init
	//----------------------------------------------------------------
	to_body.find('.royal_event_options_input_masked').each(function () {
		"use strict";
		if (jQuery.mask) jQuery(this).mask(''+jQuery(this).data('mask'));
	});


	// Datepicker init
	//----------------------------------------------------------------
	to_body.find('.royal_event_options_input_date').each(function () {
		"use strict";
		var linked = jQuery(this).data('linked-field');
		var curDate = linked ? jQuery('#'+linked).val() : jQuery(this).val();
		jQuery(this).datepicker({
			dateFormat: jQuery(this).data('format'),
			numberOfMonths: jQuery(this).data('months'),
			gotoCurrent: true,
			changeMonth: true,
			changeYear: true,
			defaultDate: curDate,
			onSelect: function (text, ui) {
				var linked = jQuery(this).data('linked-field');
				if (!royal_event_empty(linked)) {
					jQuery('#'+linked).val(text).trigger('change');
				} else {
					ui.input.trigger('change');
				}
			}
		});
	});


	// Spinner arrows click
	//----------------------------------------------------------------
	to_body.on('click', '.royal_event_options_field_spinner .royal_event_options_arrow_up,.royal_event_options_field_spinner .royal_event_options_arrow_down', function () {
		"use strict";
		var field = jQuery(this).parent().siblings('input');
		var step = field.data('step') ? String(field.data('step')) : "1";
		var prec = step.indexOf('.')==-1 ? 0 : step.length - step.indexOf('.') - 1;
		step = Math.round((jQuery(this).hasClass('royal_event_options_arrow_up') ? 1 : -1) * parseFloat(step) * Math.pow(10, prec) ) / Math.pow(10, prec);
		var minValue = field.data('min');
		var maxValue = field.data('max');
		var newValue = Math.round( (isNaN(field.val()) ? 0 : parseFloat(field.val()) + step) * Math.pow(10, prec) ) / Math.pow(10, prec);
		if (!isNaN(maxValue) && newValue > maxValue) {
			newValue = maxValue;
		}
		if (!isNaN(minValue) && newValue < minValue) {
			newValue = minValue;
		}
		field.val(newValue).trigger('change');
	});

	
	// Tags
	//----------------------------------------------------------------
	to_body.find('.royal_event_options_field_tags .royal_event_options_field_content').sortable({
		items: "span",
		update: function(event, ui) {
			var tags = '';
			ui.item.parent().find('.royal_event_options_tag').each(function() {
				tags += (tags ? ROYAL_EVENT_STORAGE['to_delimiter'] : '') + jQuery(this).text();
			});
			ui.item.siblings('input[type="hidden"]').eq(0).val(tags).trigger('change');
		}
	}).disableSelection();
	to_body.on('keypress', '.royal_event_options_field_tags input[type="text"]', function (e) {
		"use strict";
		if (e.which===44) {
			royal_event_options_add_tag_in_list(jQuery(this));
			e.preventDefault();
			return false;
		}
	});
	to_body.on('keydown', '.royal_event_options_field_tags input[type="text"]', function (e) {
		"use strict";
		if (e.which===13) {
			royal_event_options_add_tag_in_list(jQuery(this));
			e.preventDefault();
			return false;
		}
	});
	function royal_event_options_add_tag_in_list(obj) {
		"use strict";
		if (obj.val().trim()!='') {
			var text = obj.val().trim();
			obj.before('<span class="royal_event_options_tag iconadmin-cancel">'+text+'</span>');
			var tags = obj.next().val();
			obj.next().val(tags + (tags ? ROYAL_EVENT_STORAGE['to_delimiter'] : '') + text).trigger('change');
			obj.val('');
		}
	}
	to_body.on('click', '.royal_event_options_field_tags .royal_event_options_field_content span', function (e) {
		"use strict";
		var text = jQuery(this).text();
		var tags = jQuery(this).siblings('input[type="hidden"]').eq(0).val()+ROYAL_EVENT_STORAGE['to_delimiter'];
		tags = tags.replace(text+ROYAL_EVENT_STORAGE['to_delimiter'], '');
		tags = tags.substring(0, tags.length-1);
		jQuery(this).siblings('input[type="hidden"]').eq(0).val(tags).trigger('change');
		jQuery(this).siblings('input[type="text"]').focus();
		jQuery(this).remove();
		e.preventDefault();
		return false;
	});
	to_body.on('click', '.royal_event_options_field_tags .royal_event_options_field_content', function (e) {
		"use strict";
		jQuery(this).find('input[type="text"]').focus();
		e.preventDefault();
		return false;
	});

	
	// Checkbox
	//----------------------------------------------------------------
	to_body.on('change', '.royal_event_options_field_checkbox input', function (e) {
		"use strict";
		jQuery(this).next('label').eq(0).toggleClass('royal_event_options_state_checked');
		if (jQuery(this).next('label').eq(0).hasClass('royal_event_options_state_checked'))
			jQuery(this).attr('checked', 'checked');
		else
			jQuery(this).removeAttr('checked');
		e.preventDefault();
		return false;
	});


	// Radio button
	//----------------------------------------------------------------
	to_body.on('change', '.royal_event_options_field_radio input[type="radio"]', function (e) {
		"use strict";
		jQuery(this).parent().parent().find('label').removeClass('royal_event_options_state_checked').find('span').removeClass('iconadmin-dot-circled');
		jQuery(this).parent().parent().find('input:checked').next('label').eq(0).addClass('royal_event_options_state_checked').find('span').addClass('iconadmin-dot-circled');
		jQuery(this).parent().parent().find('input[type="hidden"]').val(jQuery(this).parent().parent().find('input:checked').val()).trigger('change');
		e.preventDefault();
		return false;
	});


	// Switch button
	//----------------------------------------------------------------
	to_body.on('click', '.royal_event_options_field_switch .royal_event_options_switch_inner', function (e) {
		"use strict";
		var val = parseInt(jQuery(this).css('marginLeft'))==0 ? 2 : 1;
		var data = jQuery(this).find('span').eq(val-1).data('value');
		jQuery(this).parent().siblings('input[type="hidden"]').eq(0).val(data).trigger('change');
		jQuery(this).parent().toggleClass('royal_event_options_state_off', val==2)
		e.preventDefault();
		return false;
	});


	// Checklist
	//----------------------------------------------------------------
	to_body.on('click', '.royal_event_options_field_checklist .royal_event_options_listitem', function (e) {
		"use strict";
		var multiple = jQuery(this).parents('.royal_event_options_field_checklist').hasClass('royal_event_options_multiple');
		if (!multiple) {
			jQuery(this).siblings('.royal_event_options_listitem').removeClass('royal_event_options_state_checked');
		}
		jQuery(this).toggleClass('royal_event_options_state_checked');
		collectCheckedItems(jQuery(this).parent());
		e.preventDefault();
		return false;
	});
	to_body.find('.royal_event_options_field_checklist.royal_event_options_multiple .royal_event_options_field_content').sortable({
		update: function(event, ui) {
			"use strict";
			collectCheckedItems(ui.item.parent());
		}
	}).disableSelection();


	// Select, list, images, icons, fonts
	//----------------------------------------------------------------
	to_body.on('click', '.royal_event_options_field_select .royal_event_options_input,.royal_event_options_field_select .royal_event_options_field_after,.royal_event_options_field_images .royal_event_options_caption_image,.royal_event_options_field_icons .royal_event_options_caption_icon', function (e) {
		"use strict";
		jQuery(this).siblings('.royal_event_options_input_menu').slideToggle();
		e.preventDefault();
		return false;
	});

	to_body.on('click', '.royal_event_options_field .royal_event_options_menuitem', function (e) {
		"use strict";
		var multiple = jQuery(this).parents('.royal_event_options_field').hasClass('royal_event_options_multiple');
		if (!multiple) {
			jQuery(this).siblings('.royal_event_options_menuitem').removeClass('royal_event_options_state_checked');
			jQuery(this).addClass('royal_event_options_state_checked');
		} else {
			jQuery(this).toggleClass('royal_event_options_state_checked');
		}
		collectCheckedItems(jQuery(this).parent());
		if (!multiple && !jQuery(this).parent().hasClass('royal_event_options_input_menu_list'))
			jQuery(this).parent().slideToggle();
		e.preventDefault();
		return false;
	});

	to_body.find('.royal_event_options_field.royal_event_options_multiple .royal_event_options_input_menu').sortable({
		update: function(event, ui) {
			"use strict";
			collectCheckedItems(ui.item.parent());
		}
	}).disableSelection();

	// Collect checked items
	function collectCheckedItems(list) {
		"use strict";
		var val = '', caption = '', image = '', icon = '';
		list.find('.royal_event_options_menuitem,.royal_event_options_listitem').each(function() {
			"use strict";
			if (jQuery(this).hasClass('royal_event_options_state_checked')) {
				val += (val ? ROYAL_EVENT_STORAGE['to_delimiter'] : '') + jQuery(this).data('value');
				var img = jQuery(this).find('.royal_event_options_input_image');
				if (img.length > 0) {
					image = img.eq(0).data('src');
				} else if (jQuery(this).parents('.royal_event_options_field_icons').length > 0) {
					icon = jQuery(this).data('value');
				} else {
					caption += (caption ? ROYAL_EVENT_STORAGE['to_delimiter'] : '') + jQuery(this).html();
				}
			}
		});
		list.parent().find('input[type="hidden"]').eq(0).val(val).trigger('change');
		if (caption != '')
			list.parent().find('input[type="text"]').eq(0).val(caption);
		if (image != '')
			list.parent().find('.royal_event_options_caption_image span').eq(0).css('backgroundImage', 'url('+image+')');
		if (icon != '') {
			var field = list.parent().find('.royal_event_options_input_socials');
			if (field.length > 0) {
				var btn = field.next();
				var cls = btn.attr('class');
				cls = (cls.indexOf(' icon') > 0 ? cls.substr(0, cls.indexOf(' icon')) : cls) + ' ' + icon;
				btn.removeClass().addClass(cls).trigger('change');
			} else
				list.parent().find('.royal_event_options_caption_icon span').eq(0).removeClass().addClass(icon).trigger('change');
		}
	}



	// Color selector
	//----------------------------------------------------------------
	
	// Standard WP Color Picker
	if (to_body.find('.royal_event_options_input_color_wp').length > 0) {
		to_body.find('.royal_event_options_input_color_wp').wpColorPicker({

			// a callback to fire whenever the color changes to a valid color
			change: function(e, ui){
				jQuery(e.target).val(ui.color).trigger('change');
			},
	
			// a callback to fire when the input is emptied or an invalid color
			clear: function(e) {
				jQuery(e.target).prev().trigger('change')
			},
		});
	}
	
	// Tiny Color Picker
	if (to_body.find('.royal_event_options_input_color_tiny').length > 0) {
		to_body.find('.royal_event_options_input_color_tiny').colorPicker({
			selector: '.royal_event_options_input_color_tiny',
			animationSpeed: 0,
			margin: '1px 0 0 0',
			cssAddon: '.cp-color-picker { background-color: #ddd; z-index:1000; }',
			renderCallback: function($elm, toggled) {
				var colors = this.color.colors,
					rgb = colors.RND.rgb,
					clr = colors.alpha == 1 
							? '#'+colors.HEX 
							: 'rgba(' + rgb.r + ', ' + rgb.g + ', ' + rgb.b + ', ' + (Math.round(colors.alpha * 100) / 100) + ')';
				$elm.val(clr).data('last-color', clr);
			}
			
		});
	}

	// Internal Theme Color Picker
	if (to_body.find('.royal_event_options_input_color + .iColorPicker').length > 0) {
		royal_event_color_picker();
		to_body.find('.royal_event_options_input_color + .iColorPicker').each(function() {
			jQuery(this).on('click', function (e) {
				"use strict";
				royal_event_color_picker_show(null, jQuery(this), function(fld, clr) {
					"use strict";
					fld.css('backgroundColor', clr);
					fld.siblings('input').attr('value', clr).trigger('change');
				});
			});
			var prev_fld = jQuery(this).prev();
			var prev_val = prev_fld.val();
			if (prev_val!='') {
				jQuery(this).css('backgroundColor', prev_val);
			}
			prev_fld.on('change', function() {
				"use strict";
				jQuery(this).next().css('backgroundColor', jQuery(this).val());
			});
		});
	}

	// Clone buttons
	//----------------------------------------------------------------
	to_body.on('click', '.royal_event_options_clone_button_add', function (e) {
		"use strict";
		var clone_area = jQuery(this).parents('.royal_event_options_cloneable_area').eq(0);
		var clone_item = null;
		var max_num = 0;
		clone_area.find('.royal_event_options_cloneable_item').each(function() {
			"use strict";
			var cur_item = jQuery(this);
			if (clone_item == null) 
				clone_item = cur_item;
			var num = Number(cur_item.find('input[name*="_numbers[]"]').eq(0).val());
			if (num > max_num)
				max_num = num;
		});
		var clonedObj = clone_item.clone();
		clonedObj.find('input[type="text"],textarea').val('');
		clonedObj.find('input[name*="_numbers[]"]').val(max_num+1);
		jQuery(this).before(clonedObj);
		e.preventDefault();
		return false;
	});

	to_body.on('click', '.royal_event_options_clone_button_del', function (e) {
		"use strict";
		if (jQuery(this).parents('.royal_event_options_cloneable_item').parent().find('.royal_event_options_cloneable_item').length > 1)
			jQuery(this).parents('.royal_event_options_cloneable_item').eq(0).remove();
		else
			royal_event_message_warning(ROYAL_EVENT_STORAGE['to_strings']['del_item_error'], ROYAL_EVENT_STORAGE['to_strings']['del_item']);
		e.preventDefault();
		return false;
	});



	// Inherit buttons
	//----------------------------------------------------------------
	to_body.on('click', '.royal_event_options_button_inherit', function (e) {
		"use strict";
		var inherit = !jQuery(this).hasClass('royal_event_options_inherit_off');
		if (inherit) {
			jQuery(this).addClass('royal_event_options_inherit_off');
			jQuery(this).parents('.royal_event_options_field').find('.royal_event_options_content_inherit').fadeOut().find('input').val('');
		} else {
			jQuery(this).removeClass('royal_event_options_inherit_off');
			jQuery(this).parents('.royal_event_options_field').find('.royal_event_options_content_inherit').fadeIn().find('input').val('inherit');
		}
		e.preventDefault();
		return false;
	});
	to_body.on('click', '.royal_event_options_content_inherit', function (e) {
		"use strict";
		jQuery(this).parents('.royal_event_options_field').find('.royal_event_options_button_inherit').addClass('royal_event_options_inherit_off');
		jQuery(this).fadeOut().find('input').val('');
		e.preventDefault();
		return false;
	});
}


// Standard actions
//-----------------------------------------------------------------

// Open Wordpress media manager window
function royal_event_options_action_media_upload(obj) {
	"use strict";
	var button = jQuery(obj);
	var field  = button.data('linked-field') ? jQuery("#"+button.data('linked-field')).eq(0) : button.siblings('input');
	var fieldId = field.attr('id');
	if ( ROYAL_EVENT_STORAGE['to_media_frame'][fieldId] ) {
		ROYAL_EVENT_STORAGE['to_media_frame'][fieldId]['field'] = field;
		ROYAL_EVENT_STORAGE['to_media_frame'][fieldId]['frame'].open();
		return;
	}
	ROYAL_EVENT_STORAGE['to_media_frame'][fieldId] = [];
	ROYAL_EVENT_STORAGE['to_media_frame'][fieldId]['field'] = field;
	ROYAL_EVENT_STORAGE['to_media_frame'][fieldId]['sizes'] = button.data('sizes');
	ROYAL_EVENT_STORAGE['to_media_frame'][fieldId]['multi'] = button.data('multiple');
	// Create media selector
	var media_args = {
		// Popup layout (if comment next row - hide filters and image sizes popups)
		frame: 'post',
		// Multiple choise
		multiple: ROYAL_EVENT_STORAGE['to_media_frame'][fieldId]['multi'] ? 'add' : false,
		// Set the title of the modal.
		title: button.data('caption-choose'),
		// Tell the modal to show only specified type 
		library: {
			type: button.data('type') ? button.data('type') : 'image',
		},
		// Customize the submit button.
		button: {
			// Set the text of the button.
			text: button.data('caption-update'),
			// Tell the button to close the modal
			close: true
		}
	};
	ROYAL_EVENT_STORAGE['to_media_frame'][fieldId]['frame'] = wp.media(media_args);
	ROYAL_EVENT_STORAGE['to_media_frame'][fieldId]['frame'].on( 'insert select', function(e) {
		"use strict";
		var attachment = '', attachment_url = '', pos = -1, init = false;
		if (ROYAL_EVENT_STORAGE['to_media_frame'][fieldId]['multi']) {
			ROYAL_EVENT_STORAGE['to_media_frame'][fieldId]['frame'].state().get('selection').map( function( att ) {
				"use strict";
				attachment += (attachment ? "\n" : "") + att.toJSON().url;
			});
			var val = ROYAL_EVENT_STORAGE['to_media_frame'][fieldId]['field'].val();
			attachment_url = val + (val ? "\n" : '') + attachment;
		} else {
			var state = ROYAL_EVENT_STORAGE['to_media_frame'][fieldId]['frame'].state();
			var type = state.get('type');
			if (type!==undefined) {					// Insert from URL
				var attachment = state.props.toJSON();
			} else {
				var selection = state.get('selection');
				if (selection) {
					var attachment = selection.first().toJSON();
					var sizes_selector = jQuery('.media-modal-content .attachment-display-settings select.size');
					if (ROYAL_EVENT_STORAGE['to_media_frame'][fieldId]['sizes'] && sizes_selector.length > 0) {
						var size = royal_event_get_listbox_selected_value(sizes_selector.get(0));
						if (size != '') attachment_url = attachment.sizes[size].url;
					}
				}
			}
			if (attachment_url=='' && attachment!='' && attachment.url!='') attachment_url = attachment.url;
			if (attachment_url!='') {
				if (!button.data('linked-field')) {
					var output = '';
					if ((pos = attachment_url.lastIndexOf('.'))>=0) {
						var ext = attachment_url.substr(pos+1);
						output = '<a class="royal_event_options_image_preview" data-rel="popup" target="_blank" href="' + attachment_url + '">';
						if ('jpg,png,gif'.indexOf(ext)>=0) {
							output += '<img src="'+attachment_url+'" alt="" />';
							init = true;
						} else {
							output += '<span>'+attachment_url.substr(attachment_url.lastIndexOf('/')+1)+'</span>';
						}
						output += '</a>';
					}
					button.siblings('.royal_event_options_image_preview').remove();
					if (output != '') {
						button.parent().append(output);
						if (init) royal_event_options_popup_init(ROYAL_EVENT_STORAGE['to_body']);
					}
				}
			} else {
				alert('Undefined selection');
			}
		}
		ROYAL_EVENT_STORAGE['to_media_frame'][fieldId]['field'].val(attachment_url).trigger('change');
	});
	ROYAL_EVENT_STORAGE['to_media_frame'][fieldId]['frame'].open();
}

// Clear media field
function royal_event_options_action_media_reset(obj) {
	"use strict";
	var button = jQuery(obj);
	var field  = button.data('linked-field') ? jQuery("#"+button.data('linked-field')).eq(0) : button.siblings('input');
	button.siblings('.royal_event_options_image_preview').remove();
	field.val('').trigger('change');
}

// Clear color field
function royal_event_options_action_color_reset(obj) {
	"use strict";
	var button = jQuery(obj);
	var field  = button.data('linked-field') ? jQuery("#"+button.data('linked-field')).eq(0) : button.siblings('input');
	field.val('').css('backgroundColor', '#ffffff').trigger('change');
}

// Select fontello icon
function royal_event_options_action_select_icon(obj) {
	"use strict";
	var button = jQuery(obj);
	var field  = button.data('linked-field') ? jQuery("#"+button.data('linked-field')).eq(0) : button.siblings('input[type="hidden"]').eq(0);
	button.siblings('.royal_event_options_input_menu').slideToggle();
}

// Select menu (dropdown list)
function royal_event_options_action_show_menu(obj) {
    "use strict";
	return false;
}


// Popup init
function royal_event_options_popup_init(to_body) {
	"use strict";
	to_body.find("a[data-rel='popup']:not(.inited)").each(function() {
		"use strict";
		if (ROYAL_EVENT_STORAGE['to_popup']=='pretty') {
			jQuery(this).addClass('inited').prettyPhoto({
				social_tools: '',
				theme: 'facebook',
				deeplinking: false
			});
		} else if (ROYAL_EVENT_STORAGE['to_popup']=='magnific') {
			jQuery(this).addClass('inited').magnificPopup({
				type: 'image',
				mainClass: 'mfp-img-mobile',
				closeOnContentClick: true,
				closeBtnInside: true,
				fixedContentPos: true,
				midClick: true,
				preloader: true,
				image: {
					verticalFit: true
				}
			});
		}
	});
}


// Init previously hidden elements
//-----------------------------------------------------------------------------------
function royal_event_options_init_hidden_elements(container) {
	"use strict";
	// Fields visibility
	royal_event_options_check_dependency(container);
	// Range sliders
	container.find('.royal_event_options_field_range').each(function () {
		"use strict";
		var obj = jQuery(this);
		var scale = obj.find('.royal_event_options_range_scale');
		var scaleWidth = scale.width();
		if (scaleWidth <= 0) return;
		var step = parseFloat(obj.find('.royal_event_options_input_range').data('step'));
		var prec = Math.pow(10, step.toString().indexOf('.') < 0 ? 0 : step.toString().length - step.toString().indexOf('.') - 1);
		var field = obj.find('.royal_event_options_input_range input[type="hidden"]').eq(0);
		var val = field.val().split(ROYAL_EVENT_STORAGE['to_delimiter']);
		var rangeMin = parseFloat(obj.find('.royal_event_options_range_min').html());
		var rangeMax = parseFloat(obj.find('.royal_event_options_range_max').html());
		var scaleStep = scaleWidth / ((rangeMax - rangeMin) / step);
		var i = 0;
		obj.find('.royal_event_options_range_slider').each(function () {
			"use strict";
			var fill = val.length==1 || i==1 ? 'width' : 'left';
			jQuery(this).css('left', (val[i]-rangeMin)*scaleStep/step+'px');
			scale.find('span').css(fill, ((val[i]-rangeMin)*scaleStep/step-(i==1 ? (val[0]-rangeMin)*scaleStep/step : 0))+'px');
			i++;
		});
		if (!obj.hasClass('inited')) {
			obj.addClass('inited').find('.royal_event_options_range_slider').draggable({
				axis: 'x',
				grid: [scaleStep, scaleStep],
				containment: '.royal_event_options_input_range',
				scroll: false,
				drag: function (e, ui) {
					"use strict";
					var field = obj.find('.royal_event_options_input_range input[type="hidden"]').eq(0);
					var val = field.val().split(ROYAL_EVENT_STORAGE['to_delimiter']);
					var slider = ui.helper;
					var idx = slider.index()-1;
					var newVal = Math.min(rangeMax, Math.max(rangeMin, Math.round(ui.position.left / scaleStep * step * prec) / prec + rangeMin));
					if (val.length==2) {
						if (idx==0 && newVal > val[1]) {
							newVal = val[1];
							ui.position.left = (newVal-rangeMin)*scaleStep/step;
						}
						if (idx==1 && newVal < val[0]) {
							newVal = val[0];
							ui.position.left = (newVal-rangeMin)*scaleStep/step;
						}
					}
					if (val[idx] != newVal) {
						slider.find('.royal_event_options_range_slider_value').html(newVal);
						val[idx] = newVal;
						field.val(val.join(ROYAL_EVENT_STORAGE['to_delimiter'])).trigger('change');
						if (val.length==2)
							scale.find('span').css('left', (val[0]-rangeMin)*scaleStep/step+'px');
						scale.find('span').css('width', ((val[val.length==2 ? 1 : 0]-rangeMin)*scaleStep/step-(val.length==2 ? (val[0]-rangeMin)*scaleStep/step : 0))+'px');
					}
				}
			});
		}
	});
}


// Check dependencies
function royal_event_options_check_dependency(cont) {
	"use strict";
	if (cont.parents('.royal_event_shortcodes_body').length==1) {
		if (typeof ROYAL_EVENT_SHORTCODES_DATA == 'undefined') return;
		var sc_name = ROYAL_EVENT_STORAGE['shortcodes_current_idx'];
		if (sc_name == '') return;
		var sc = ROYAL_EVENT_SHORTCODES_DATA[sc_name];
	} else if (cont.parents('.royal_event_options_body').length==1) {
		if (typeof ROYAL_EVENT_OPTIONS_DATA == 'undefined') return;
		var sc = ROYAL_EVENT_OPTIONS_DATA;
	} else {
		return;
	}
	var popup = cont.parents('.royal_event_options_tab');
	if (popup.length==0) popup = cont;
	cont.find('[data-param]').each(function() {
		"use strict";
		var field = jQuery(this);
		var param = field.data('param');
		var value = field.attr('type') != 'checkbox' || field.get(0).checked ? field.val() : '';
		var depend = false;
		if (typeof sc.params != 'undefined' && typeof sc.params[param] != 'undefined' && typeof sc.params[param].dependency != 'undefined')
			depend = sc.params[param].dependency;
		if (depend === false && typeof sc.children != 'undefined' && typeof sc.children.params != 'undefined' && typeof sc.children.params[param] != 'undefined' && typeof sc.children.params[param].dependency != 'undefined')
			depend = sc.children.params[param].dependency;
		if (depend === false && typeof sc[param] != 'undefined' && typeof sc[param].dependency != 'undefined')
			depend = sc[param].dependency;
		if (depend) {
			var dep_cnt = 0, dep_all = 0;
			var dep_cmp = typeof depend.compare != 'undefined' ? depend.compare.toLowerCase() : 'and';
			var fld=null, val='';
			for (var i in depend) {
				if (i == 'compare') continue;
				dep_all++;
				fld = cont.find('[data-param="'+i+'"]');
				if (fld.length==0) fld = popup.find('[data-param="'+i+'"]');
				if (fld.length > 0) {
					val = fld.attr('type') != 'checkbox' || fld.get(0).checked ? fld.val() : '';
					for (var j in depend[i]) {
						if ( 
							   (depend[i][j]=='not_empty' && val!='') 										// Main field value is not empty - show current field
							|| (depend[i][j]=='is_empty' && val=='')										// Main field value is empty - show current field
							|| (depend[i][j]=='refresh' && royal_event_options_refresh_field(field, i, val))	// Main field value changed - refresh current field
							|| (val!='' && val.indexOf(depend[i][j])==0)									// Main field value equal to specified value - show current field
						) {
							dep_cnt++;
							break;
						}
					}
				}
				if (dep_cnt > 0 && dep_cmp == 'or')
					break;
			}
			if ((dep_cnt > 0 && dep_cmp == 'or') || (dep_cnt == dep_all && dep_cmp == 'and')) {
				field.parents('.royal_event_options_field').show().removeClass('royal_event_options_no_use');
			} else {
				field.parents('.royal_event_options_field').hide().addClass('royal_event_options_no_use');
			}
		}
	});
}

// Fix header on scroll
jQuery(window).scroll(function () {
	"use strict";
	royal_event_options_fix_scroll_menu();
});

// Fix/unfix top panel
function royal_event_options_fix_scroll_menu() {
	"use strict";
	var scroll_offset = jQuery(window).scrollTop();
	var adminbar_height = Math.max(0, jQuery('#wpadminbar').height());
	var header = jQuery('.royal_event_options_form .royal_event_options_header');
	if (header.length > 0) {
		if (header.data('wrap') != 1) {
			header.wrap('<div class="royal_event_options_header_wrap" style="height:'+header.height()+'px;"></div>' );
			header.attr('data-wrap', '1');
		}
		var wrap = header.parent();
		var wrap_offset = wrap.offset().top;
		var wrap_h = wrap.height();
		var h = header.height();
		if (scroll_offset + adminbar_height > wrap_offset + wrap_h - 30) {
			jQuery('.royal_event_options_header').addClass('royal_event_options_header_fixed');
		} else {
			jQuery('.royal_event_options_header').removeClass('royal_event_options_header_fixed');
		}
	}
}


// Import values
function royal_event_options_import_values(data) {
	"use strict";
	var msg = '', res = '';
	for (var opt in data) {
		if ((res = royal_event_options_set_value(opt, data[opt])) != '') {
			msg += (msg!='' ? ',<br>' : '') + res;
		}
	}
	if (msg != '') {
		royal_event_message_warning(ROYAL_EVENT_STORAGE['to_strings']['import_options_broken']+'<br>'+msg, ROYAL_EVENT_STORAGE['to_strings']['import_options_header']);
	}
}

// Set new value for one field
function royal_event_options_set_value(opt, val) {
	"use strict";
	var result = '';
	var suffix = (typeof val == 'object' ? '[]' : '');
	var fld = jQuery('[name="'+opt+suffix+'"]');
	if (fld.length == 0) return false;
	var parent = fld.parents('.royal_event_options_field');
	var type = royal_event_options_get_type(parent);
	var clone_area = fld.parents('.royal_event_options_cloneable_area');
	var clone_item = null;
	if (clone_area.length > 0) {
		clone_area.find('.royal_event_options_cloneable_item').each(function(idx) {
			if (idx == 0) {
				clone_item = jQuery(this);
				fld.eq(0).val('');
				jQuery(this).find('[name="'+opt+'_numbers[]"]').val(0);
				if (type=='socials') jQuery(this).find('[name="'+opt+'_icon[]"]').val('');
			} else
				jQuery(this).remove();
		});
	}
	if (typeof val != 'object' || typeof val[0] == 'undefined')
		val = [val];
	var cnt = 0;
	for (var i in val) {
		if (ROYAL_EVENT_STORAGE['to_override']!='general') {
			if (val[i] != 'inherit') {
				parent.find('.royal_event_options_button_inherit').addClass('royal_event_options_inherit_off');
				parent.find('.royal_event_options_content_inherit').fadeOut().find('input').val('');
			} else {
				parent.find('.royal_event_options_button_inherit').removeClass('royal_event_options_inherit_off');
				parent.find('.royal_event_options_content_inherit').fadeIn().find('input').val('inherit');
			}
		}
		if (cnt > 0 && clone_area.length > 0) {
			var clonedObj = clone_item.clone();
			clonedObj.find('input[name*="_numbers[]"]').val(i);
			clone_area.find('.royal_event_options_clone_button_add').before(clonedObj);
			fld = jQuery('[name="'+opt+'[]"]');
		}
		if (ROYAL_EVENT_STORAGE['to_override']=='general' || val[i] != 'inherit') {
			if (type=='text' || type=='textarea' || type=='hidden' || type=='spinner') {
				fld.eq(cnt).val(val[i]).trigger('change');
			} else if (type=='editor') {
				fld.eq(cnt).val(val[i]).trigger('change');
				if (typeof(tinymce) != 'undefined' && typeof(tinymce.editors[opt])!='undefined') {
					tinymce.editors[opt].setContent(val[i]);
				}
			} else if (type=='date') {
				parent.datepicker( "setDate", val[i] );
				fld.eq(cnt).val(val[i]).trigger('change');
			} else if (type=='tags') {
				fld.eq(cnt).val(val[i]).trigger('change');
				fld.eq(cnt).parent().find('.royal_event_options_tag').remove();
				fld.eq(cnt).prev().val('');
				var tags = val[i].split(ROYAL_EVENT_STORAGE['to_delimiter']);
				for (var j=0; j<tags.length; j++)
					fld.eq(cnt).prev().before('<span class="royal_event_options_tag iconadmin-cancel">'+tags[j]+'</span>');
			} else if (type=='checkbox') {
				fld.eq(cnt).next('label').eq(0).toggleClass('royal_event_options_state_checked', val[i]=='true');
				if (val[i]=='true')
					fld.eq(cnt).attr('checked', 'checked');
				else
					fld.eq(cnt).removeAttr('checked');
			} else if (type=='radio') {
				fld.eq(cnt).removeAttr('checked').parent().parent().find('label').removeClass('royal_event_options_state_checked').find('span').removeClass('iconadmin-dot-circled');
				fld.eq(cnt).parent().parent().find('input[value="'+val[i]+'"]').attr('checked', 'checked').next('label').eq(0).addClass('royal_event_options_state_checked').find('span').addClass('iconadmin-dot-circled');
				fld.eq(cnt).parent().parent().find('input[type="hidden"]').val(val[i]).trigger('change');
			} else if (type=='switch') {
				fld.eq(cnt).val(val[i]).trigger('change');
				var idx = fld.siblings('.royal_event_options_switch').find('[data-value="'+val[i]+'"]').index();
				fld.eq(cnt).siblings('.royal_event_options_switch').toggleClass('royal_event_options_state_off', idx==1);
			} else if (type=='checklist') {
				fld.eq(cnt).val(val[i]).trigger('change');
				fld.eq(cnt).siblings('.royal_event_options_listitem').removeClass('royal_event_options_state_checked');
				var items = val[i].split(ROYAL_EVENT_STORAGE['to_delimiter']);
				for (var j=0; j<items.length; j++)
					fld.eq(cnt).siblings('.royal_event_options_listitem[data-value="'+items[j]+'"]').addClass('royal_event_options_state_checked');
			} else if (type=='media') {
				fld.eq(cnt).val(val[i]).trigger('change');
				fld.eq(cnt).siblings('.royal_event_options_image_preview').remove();
				if (val[i]!='') {
					var file = val[i].split('/').pop();
					if (file!='') {
						var parts = file.split('.');
						var fname = parts[0];
						var ext = parts.length > 1 ? parts[1] : '';
						fld.eq(cnt).after('<a class="royal_event_options_image_preview" data-rel="popup" target="_blank" href="'+val[i]+'">'+('jpg,png,gif'.indexOf(ext)>=0 ? '<img src="'+val[i]+'" alt="" />' : '<span>'+fname+'</span>')+'</a>');
					}
				}
			} else if (type=='range') {
				fld.eq(cnt).val(val[i]).trigger('change');
				var scale = parent.find('.royal_event_options_range_scale');
				var step = parseInt(parent.find('.royal_event_options_input_range').data('step'));
				var rangeMin = parseInt(parent.find('.royal_event_options_range_min').html());
				var rangeMax = parseInt(parent.find('.royal_event_options_range_max').html());
				var scaleWidth = scale.width();
				var scaleStep = scaleWidth / (rangeMax - rangeMin) * step;
				var items = val[i].split(ROYAL_EVENT_STORAGE['to_delimiter']);
				for (var j=0; j<items.length; j++) {
					var slider = fld.eq(cnt).siblings('.royal_event_options_range_slider').eq(j);
					slider.find('.royal_event_options_range_slider_value').html(items[j]);
					var fill = items.length==1 || j==1 ? 'width' : 'left';
					slider.css('left', (items[j]-rangeMin)*scaleStep+'px');
					scale.find('span').css(fill, ((items[j]-rangeMin)*scaleStep-(j==1 ? (items[0]-rangeMin)*scaleStep : 0))+'px');
				}
			} else if (type=='select' || type=='images' || type=='icons') {
				fld.eq(cnt).val(val[i]).trigger('change');
				fld.eq(cnt).siblings('.royal_event_options_input_menu').find('.royal_event_options_menuitem').removeClass('royal_event_options_state_checked');
				var items = val[i].split(ROYAL_EVENT_STORAGE['to_delimiter']);
				for (var j=0; j<items.length; j++) {
					fld.eq(cnt).siblings('.royal_event_options_input_menu').find('.royal_event_options_menuitem[data-value="'+items[j]+'"]').addClass('royal_event_options_state_checked');
					if (type=='images') {
						var src = fld.eq(cnt).siblings('.royal_event_options_input_menu').find('.royal_event_options_menuitem[data-value="'+items[j]+'"]').find('span').data('src');
						fld.eq(cnt).siblings('.royal_event_options_caption_image').find('span').css('backgroundImage', 'url('+src+')');
					} else if (type=='icons') {
						var cls = fld.eq(cnt).siblings('.royal_event_options_caption_icon').find('span').attr('class');
						cls = (cls.indexOf(' icon') > 0 ? cls.substr(0, cls.indexOf(' icon')) : cls) + ' ' + items[i];
						fld.eq(cnt).siblings('.royal_event_options_caption_icon').find('span').removeClass().addClass(cls);
					} else {
						var caption = fld.eq(cnt).siblings('.royal_event_options_input_menu').find('.royal_event_options_menuitem[data-value="'+items[j]+'"]').text();
						fld.eq(cnt).siblings('.royal_event_options_input').val(caption);
					}
				}
			} else if (type=='socials') {
				fld.eq(cnt).val(val[i].url).trigger('change');
				fld.eq(cnt).siblings('[name="social_icons_icon[]"]').val(val[i].icon);
				fld.eq(cnt).siblings('.royal_event_options_input_menu').find('.royal_event_options_menuitem').removeClass('royal_event_options_state_checked');
				fld.eq(cnt).siblings('.royal_event_options_input_menu').find('.royal_event_options_menuitem[data-value="'+val[i].icon+'"]').addClass('royal_event_options_state_checked');
				var subtype = parent.hasClass('royal_event_options_field_images') ? 'images' : 'icons';
				if (subtype=='images') {
					fld.eq(cnt).siblings('.royal_event_options_caption_image').find('span').css('backgroundImage', 'url('+val[i].icon+')');
				} else if (subtype=='icons') {
					var cls = fld.eq(cnt).siblings('.royal_event_options_field_after').attr('class');
					cls = (cls.indexOf(' icon') > 0 ? cls.substr(0, cls.indexOf(' icon')) : cls) + ' ' + val[i].icon;
					fld.eq(cnt).siblings('.royal_event_options_field_after').removeClass().addClass(cls);
				}
			} else if (type=='color') {
				fld.eq(cnt).val(val[i]).trigger('change');
			} else {
				fld.eq(cnt).val(val[i]).trigger('change');
				if (!result) result = opt+' ('+type+') = '+val[i];
			}
		}
		cnt++;
	}
	return result;
}

// Return type of the field
function royal_event_options_get_type(fld) {
	"use strict";
	var classes = fld.attr('class').split(' ');
	var type = 'text';
	for (var i=0; i < classes.length; i++) {
		if (classes[i].indexOf('royal_event_options_field_')==0) {
			type = classes[i].split('_').pop();
			break;
		}	
	}
	return type;
}

// Refresh field then main field changed
function royal_event_options_refresh_field(fld, main_name, main_val) {
	"use strict";
	if (main_name == 'post_type') {
		if (fld.data(main_name)==undefined)
			fld.data(main_name, main_val);
		else if (fld.data(main_name)!=main_val) {
			var cat_field = fld;
			var cat_list = cat_field.prev().slideToggle();
			var cat_lbl = cat_list.parent().prev();
			cat_lbl.append('<span class="sc_refresh iconadmin-spin3 animate-spin"></span>');
			// Prepare data
			var data = {
				action: 'royal_event_admin_change_post_type',
				nonce: ROYAL_EVENT_STORAGE['ajax_nonce'],
				post_type: main_val
			};
			jQuery.post(ROYAL_EVENT_STORAGE['ajax_url'], data, function(response) {
				"use strict";
				var rez = {};
				try {
					rez = JSON.parse(response);
				} catch (e) {
					rez = { error: ROYAL_EVENT_STORAGE['ajax_error'] };
					console.log(response);
				}
				if (rez.error === '') {
					var cat_str = '';
					for (var i in rez.data.ids) {
						cat_str += '<span class="royal_event_options_menuitem ui-sortable-handle" data-value="'+rez.data.ids[i]+'">'+rez.data.titles[i]+'</span>';
					}
					cat_field.data(main_name, main_val).val('');
					cat_list.empty().html(cat_str).slideToggle();
					cat_lbl.find('span').remove();
				}
			});
		}
	}
	return true;
}
