(function( $ ) {
	"use strict";

	function initColorPickers( $context ) {
		if ( typeof $.fn.wpColorPicker === 'undefined' ) {
			return;
		}
		$context.find('.hoot-maglume-color').each( function() {
			var $input = $(this);
			if ( $input.hasClass('wp-color-picker') ) {
				return; // already initialized
			}
			$input.wpColorPicker();
			$input.closest('.wp-picker-container').find('.wp-color-result').on('click', function() {
				setTimeout( function() {
					$input.trigger('change');
				}, 10 );
			});
		});
		$context.find('.maglume-collapse-head').on( "click", function() {
			var $head = $(this);
			if ( $head.hasClass('maglume-head-init') ) {
				return; // already initialized
			}
			$(this).addClass('maglume-head-init').siblings('.maglume-collapse-body').toggle();
		});
	}

	// Init on page load
	$(function() {
		initColorPickers( $('body.widgets-php:not(.block-editor-page)') );
	});

	// Init when a widget is added or updated
	$(document).on( 'widget-added widget-updated', function( event, $widget ) {
		initColorPickers( $widget );
	});



}(jQuery));