/**
 * Theme Name: errigal
 * Theme URI: https://projecterrigal.com/themes/errigal/
 * Author: Project errigal
 * Author URI: https://projecterrigal.com/
 * Version: 1.2.1
 */
( function() {
	'use strict';

	var ADMIN_DISMISS_ADMIN_NOTICE_WELCOME = 'errigal_dismiss_admin_notice_welcome';
	var CLASS_ADMIN_NOTICE_DISMISS = 'notice-dismiss';
	var ID_ADMIN_NOTICE_WELCOME = 'errigal-welcome-notice';
	var SELECTOR_ADMIN_NOTICE_WELCOME_DISMISS = '#'.concat( ID_ADMIN_NOTICE_WELCOME, ' .' ).concat( CLASS_ADMIN_NOTICE_DISMISS );
	var OBJECT_NAME_WP_AJAXURL = 'ajaxurl';
	var EMPTY_STRING = '';
	var NULL_VALUE = null;

	function isObject( value ) {
		return value instanceof Object;
	}

	function getObjectValue( obj, key ) {
		return isObject( obj ) && key in obj ? obj[ key ] : NULL_VALUE;
	}

	function isObjectType( value, expectedType ) {
		return Object.prototype.toString.call( value ) === '[object '.concat( expectedType, ']' );
	}

	function isString( value ) {
		return isObjectType( value, 'String' );
	}

	function getGlobalString( propertyName ) {
		var GLOBAL_STRING = getObjectValue( window, propertyName );

		return isString( GLOBAL_STRING ) ? GLOBAL_STRING : EMPTY_STRING;
	}

	function getAjaxUrl() {
		return getGlobalString( OBJECT_NAME_WP_AJAXURL );
	}

	var EVENT_CLICK = 'click';
	var OBJECT_NAME_JQUERY = 'jQuery';
	var NULL_OBJECT = null;

	function isFunction( value ) {
		return value instanceof Function;
	}

	function getGlobalFunction( functionName ) {
		var GLOBAL_FUNCTION = getObjectValue( window, functionName );

		return isFunction( GLOBAL_FUNCTION ) ? GLOBAL_FUNCTION : NULL_OBJECT;
	}

	function isJqueryDocument( value ) {
		return !! value && isFunction( value.ready );
	}

	function getJqueryDocument() {
		var JQUERY = getGlobalFunction( OBJECT_NAME_JQUERY );

		if ( ! JQUERY ) {
			return NULL_OBJECT;
		}

		var JQUERY_DOCUMENT = JQUERY( document );

		return isJqueryDocument( JQUERY_DOCUMENT ) ? JQUERY_DOCUMENT : NULL_OBJECT;
	}

	function isJqueryObject( obj ) {
		return !! obj && isFunction( obj ) && isObject( obj() ) && isFunction( obj().on ) && isFunction( obj.post );
	}

	function isNonEmptyString( value ) {
		return isString( value ) && isFunction( value.trim ) && value.trim() !== EMPTY_STRING;
	}

	function onJqueryEvent( eventType, selector, callback ) {
		if ( ! isNonEmptyString( eventType ) || ! isNonEmptyString( selector ) || ! isFunction( callback ) ) {
			return;
		}

		var JQUERY_DOCUMENT = getJqueryDocument();

		if ( ! JQUERY_DOCUMENT ) {
			return;
		}

		JQUERY_DOCUMENT.ready( function( $ ) {
			if ( ! isJqueryObject( $ ) ) {
				return;
			}

			$( selector ).on( eventType, function() {
				return callback( $ );
			} );
		} );
	}

	function onJqueryClick( selector, callback ) {
		onJqueryEvent( EVENT_CLICK, selector, callback );
	}

	function sendJqueryPostRequest( $, url, data ) {
		if ( ! isJqueryObject( $ ) || ! isNonEmptyString( url ) || ! isObject( data ) ) {
			return;
		}

		$.post( url, data );
	}

	var OBJECT_KEY_ADMIN_NONCE = 'nonce';
	var OBJECT_KEY_ADMIN_FONT_SIZE_CLASS = 'font_size_class';
	var OBJECT_KEY_ADMIN_MEDIA_MODAL_BUTTON_TEXT_SELECT = 'admin_media_modal_button_text_select';
	var OBJECT_KEY_ADMIN_MEDIA_MODAL_TITLE = 'admin_media_modal_title';
	var OBJECT_KEYS_ADMIN = [ OBJECT_KEY_ADMIN_MEDIA_MODAL_TITLE, OBJECT_KEY_ADMIN_MEDIA_MODAL_BUTTON_TEXT_SELECT, OBJECT_KEY_ADMIN_NONCE, OBJECT_KEY_ADMIN_FONT_SIZE_CLASS ];
	var OBJECT_NAME_ADMIN = 'ERRIGAL_ADMIN';
	var EMPTY_ARRAY = [];
	var INDEX_FIRST = 0;

	function isHTMLCollection( elements ) {
		return elements instanceof HTMLCollection;
	}

	function isNumber( value ) {
		return ! isNaN( value ) && isObjectType( value, 'Number' ) && isFinite( value );
	}

	function isPositiveNumber( value ) {
		return isNumber( value ) && value > 0;
	}

	function isIterable( items ) {
		return !! items && isPositiveNumber( items.length );
	}

	function isTrue( value ) {
		return value === true;
	}

	function forEach( items, callback ) {
		if ( ! isIterable( items ) || ! isFunction( callback ) ) {
			return false;
		}

		if ( isHTMLCollection( items ) ) {
			items = Array.prototype.slice.call( items );
		}

		for ( var i = INDEX_FIRST; i < items.length; i++ ) {
			if ( isTrue( callback( items[ i ], i, items ) ) ) {
				return true;
			}
		}

		return false;
	}

	function hasObjectKey( value, index ) {
		return !! value && !! index && isTrue( Object.prototype.hasOwnProperty.call( value, index ) );
	}

	function isStringArray( items ) {
		return Array.isArray( items ) && isIterable( items ) ? ! forEach( items, function( item ) {
			return ! isNonEmptyString( item );
		} ) : false;
	}

	function getGlobalObject( objectName ) {
		var expectedKeys = arguments.length > 1 && arguments[ 1 ] !== undefined ? arguments[ 1 ] : EMPTY_ARRAY;

		if ( ! ( expectedKeys === EMPTY_ARRAY || isStringArray( expectedKeys ) ) ) {
			return NULL_OBJECT;
		}

		var GLOBAL_OBJECT = getObjectValue( window, objectName );

		if ( ! isObject( GLOBAL_OBJECT ) ) {
			return NULL_OBJECT;
		}

		if ( ! isPositiveNumber( expectedKeys.length ) ) {
			return GLOBAL_OBJECT;
		}

		var HAS_EXPECTED_KEYS = ! forEach( expectedKeys, function( key ) {
			return ! hasObjectKey( GLOBAL_OBJECT, key );
		} );

		return isTrue( HAS_EXPECTED_KEYS ) ? GLOBAL_OBJECT : NULL_OBJECT;
	}

	function getAdminObject() {
		return getGlobalObject( OBJECT_NAME_ADMIN, OBJECT_KEYS_ADMIN );
	}

	function getAdminNonce() {
		return getObjectValue( getAdminObject(), OBJECT_KEY_ADMIN_NONCE );
	}

	function addAdminNoticeDismissButtonEvent() {
		var AJAX_URL = getAjaxUrl();

		if ( ! isNonEmptyString( AJAX_URL ) ) {
			return;
		}

		var NONCE = getAdminNonce();

		if ( ! isNonEmptyString( NONCE ) ) {
			return;
		}

		onJqueryClick( SELECTOR_ADMIN_NOTICE_WELCOME_DISMISS, function( $ ) {
			return sendJqueryPostRequest( $, AJAX_URL, {
				action: ADMIN_DISMISS_ADMIN_NOTICE_WELCOME,
				nonce: NONCE,
			} );
		} );
	}

	function addAdminNoticeEvents() {
		addAdminNoticeDismissButtonEvent();
	}

	var TAG_BODY = 'body';
	var TAG_HTML = 'html';
	var EVENT_ADDEDITOR = 'AddEditor';
	var EVENT_INIT = 'init';
	var ID_TINY_MCE_EDITOR_CONTENT = 'content';
	var OBJECT_NAME_WP_TINY_MCE = 'tinymce';

	function isTinyMce( obj ) {
		return !! obj && isFunction( obj.on );
	}

	function getTinyMce() {
		var TINY_MCE = getGlobalObject( OBJECT_NAME_WP_TINY_MCE );

		return isTinyMce( TINY_MCE ) ? TINY_MCE : NULL_OBJECT;
	}

	function isTinyMceEvent( event ) {
		return !! event && isObject( event.editor ) && isNonEmptyString( event.editor.id ) && isFunction( event.editor.on ) && isFunction( event.editor.getBody ) && event.editor.id === ID_TINY_MCE_EDITOR_CONTENT;
	}

	function onClassicEditorBodyReady( callback ) {
		if ( ! isFunction( callback ) ) {
			return;
		}

		var TINY_MCE = getTinyMce();

		if ( ! TINY_MCE ) {
			return;
		}

		TINY_MCE.on( EVENT_ADDEDITOR, function( event ) {
			if ( ! isTinyMceEvent( event ) ) {
				return;
			}

			event.editor.on( EVENT_INIT, function() {
				return callback( event.editor.getBody() );
			} );
		} );
	}

	var EVENT_DOMCONTENTLOADED = 'DOMContentLoaded';

	function isBoolean( value ) {
		return isObjectType( value, 'Boolean' );
	}

	function isUndefined( value ) {
		return isObjectType( value, 'Undefined' );
	}

	function onDocumentEvent( eventType, callback, options ) {
		if ( ! isNonEmptyString( eventType ) || ! isFunction( callback ) || ! ( isUndefined( options ) || isObject( options ) || isBoolean( options ) ) ) {
			return;
		}

		document.addEventListener( eventType, callback, options || false );
	}

	function onDocumentDomContentLoaded( callback, options ) {
		onDocumentEvent( EVENT_DOMCONTENTLOADED, callback, options );
	}

	function toLowerCase( value ) {
		return isNonEmptyString( value ) && isFunction( value.toLowerCase ) ? value.toLowerCase() : EMPTY_STRING;
	}

	function areStringsEqualIgnoreCase( str1, str2 ) {
		return toLowerCase( str1 ) === toLowerCase( str2 );
	}

	function hasTagName( element ) {
		return !! element && isNonEmptyString( element.tagName );
	}

	function isElementOfType( element, tagName ) {
		return hasTagName( element ) && isNonEmptyString( tagName ) && areStringsEqualIgnoreCase( element.tagName, tagName );
	}

	function getAdminFontSizeClass() {
		return getObjectValue( getAdminObject(), OBJECT_KEY_ADMIN_FONT_SIZE_CLASS );
	}

	function isHtml( element ) {
		return isElementOfType( element, TAG_HTML ) && !! element.classList && !! element.classList.add;
	}

	function isBody( element ) {
		return isElementOfType( element, TAG_BODY );
	}

	function addSetClassicEditorFontSize() {
		var FONT_SIZE_CLASS = getAdminFontSizeClass();

		if ( ! isNonEmptyString( FONT_SIZE_CLASS ) ) {
			return;
		}

		onClassicEditorBodyReady( function( body ) {
			if ( ! isBody( body ) ) {
				return;
			}

			var HTML = body.parentElement;

			if ( ! isHtml( HTML ) ) {
				return;
			}

			HTML.classList.add( FONT_SIZE_CLASS );
		} );
	}

	function addClassicEditorEvents() {
		onDocumentDomContentLoaded( addSetClassicEditorFontSize );
	}

	var ATTRIBUTE_SRC = 'src';
	var CLASS_WIDGET_HAS_IMAGE = 'has-image';
	var CSS_BACKGROUND_IMAGE = 'background-image';
	var CLASS_WIDGET_IMAGE = 'errigal-widget__image';
	var NULL_ELEMENT = null;

	function getFirstItem( items ) {
		return isIterable( items ) ? items[ INDEX_FIRST ] : NULL_VALUE;
	}

	function hasClassList( element ) {
		return !! element && element.classList instanceof DOMTokenList && isFunction( element.classList.contains );
	}

	function isHTMLElement( element ) {
		if ( ! ( element instanceof HTMLElement ) ) {
			return false;
		}

		for ( var _len = arguments.length, classNames = new Array( _len > 1 ? _len - 1 : 0 ), _key = 1; _key < _len; _key++ ) {
			classNames[ _key - 1 ] = arguments[ _key ];
		}

		return hasClassList( element ) && ! forEach( classNames, function( className ) {
			return ! element.classList.contains( className );
		} );
	}

	function isDocument( element ) {
		return element instanceof Document;
	}

	function isisHTMLElementOrDocument( element ) {
		return isHTMLElement( element ) || isDocument( element );
	}

	function callElementMethod( methodName, nameOrSelectors, rootElement ) {
		return isNonEmptyString( methodName ) && isNonEmptyString( nameOrSelectors ) && isisHTMLElementOrDocument( rootElement ) && isFunction( rootElement[ methodName ] ) ? rootElement[ methodName ]( nameOrSelectors ) : NULL_ELEMENT;
	}

	function getElementsByClassName( className ) {
		var rootElement = arguments.length > 1 && arguments[ 1 ] !== undefined ? arguments[ 1 ] : document;
		var ELEMENTS = callElementMethod( 'getElementsByClassName', className, rootElement );

		return isHTMLCollection( ELEMENTS ) ? ELEMENTS : NULL_ELEMENT;
	}

	function getElementByClassName( className ) {
		var rootElement = arguments.length > 1 && arguments[ 1 ] !== undefined ? arguments[ 1 ] : document;

		return getFirstItem( getElementsByClassName( className, rootElement ) );
	}

	var CLASS_WIDGET_IMAGE_CONTROL = 'errigal-widget__image-control';

	function isWidgetImageControl( element ) {
		return isHTMLElement( element, CLASS_WIDGET_IMAGE_CONTROL );
	}

	function getWidgetImage( widgetImageControl ) {
		return isWidgetImageControl( widgetImageControl ) ? getElementByClassName( CLASS_WIDGET_IMAGE, widgetImageControl ) : NULL_ELEMENT;
	}

	function getParentElement( element ) {
		return isHTMLElement( element ) ? element.parentElement : NULL_ELEMENT;
	}

	var CLASS_WIDGET_ADD_IMAGE_BUTTON_CONTAINER = 'errigal-widget__add-image-button-container';

	function isWidgetAddImageButtonContainer( element ) {
		return isHTMLElement( element, CLASS_WIDGET_ADD_IMAGE_BUTTON_CONTAINER );
	}

	var CLASS_WIDGET_ADD_IMAGE_BUTTON = 'errigal-widget__add-image-button';

	function isWidgetAddImageButton( element ) {
		return isHTMLElement( element, CLASS_WIDGET_ADD_IMAGE_BUTTON );
	}

	var CLASS_WIDGET_CHANGE_IMAGE_BUTTON = 'errigal-widget__change-image-button';

	function isWidgetChangeImageButton( element ) {
		return isHTMLElement( element, CLASS_WIDGET_CHANGE_IMAGE_BUTTON );
	}

	var CLASS_WIDGET_REMOVE_IMAGE_BUTTON = 'errigal-widget__remove-image-button';

	function isWidgetRemoveImageButton( element ) {
		return isHTMLElement( element, CLASS_WIDGET_REMOVE_IMAGE_BUTTON );
	}

	function isWidgetImageButton( element ) {
		return isWidgetAddImageButton( element ) || isWidgetChangeImageButton( element ) || isWidgetRemoveImageButton( element );
	}

	var CLASS_WIDGET_IMAGE_BUTTONS_CONTAINER = 'errigal-widget__image-buttons-container';

	function isWidgetImageButtonsContainer( element ) {
		return isHTMLElement( element, CLASS_WIDGET_IMAGE_BUTTONS_CONTAINER );
	}

	function getWidgetImageControl( widgetImageButton ) {
		if ( ! isWidgetImageButton( widgetImageButton ) ) {
			return NULL_ELEMENT;
		}

		var WIDGET_IMAGE_BUTTON_CONTAINER = getParentElement( widgetImageButton );

		if ( ! ( isWidgetAddImageButtonContainer( WIDGET_IMAGE_BUTTON_CONTAINER ) || isWidgetImageButtonsContainer( WIDGET_IMAGE_BUTTON_CONTAINER ) ) ) {
			return NULL_ELEMENT;
		}

		var WIDGET_IMAGE_CONTROL = getParentElement( WIDGET_IMAGE_BUTTON_CONTAINER );

		return isWidgetImageControl( WIDGET_IMAGE_CONTROL ) ? WIDGET_IMAGE_CONTROL : NULL_ELEMENT;
	}

	var CLASS_WIDGET_IMAGE_PATH = 'errigal-widget__image-path';

	function getWidgetImagePathInput( widgetImageControl ) {
		return isWidgetImageControl( widgetImageControl ) ? getElementByClassName( CLASS_WIDGET_IMAGE_PATH, widgetImageControl ) : NULL_ELEMENT;
	}

	function isWidgetImage( element ) {
		return isHTMLElement( element, CLASS_WIDGET_IMAGE );
	}

	var TAG_INPUT = 'input';

	function isHTMLElementOfType( tagName, element ) {
		for ( var _len = arguments.length, classNames = new Array( _len > 2 ? _len - 2 : 0 ), _key = 2; _key < _len; _key++ ) {
			classNames[ _key - 2 ] = arguments[ _key ];
		}

		return isHTMLElement.apply( void 0, [ element ].concat( classNames ) ) && isElementOfType( element, tagName );
	}

	function isInputElement( element ) {
		for ( var _len = arguments.length, classNames = new Array( _len > 1 ? _len - 1 : 0 ), _key = 1; _key < _len; _key++ ) {
			classNames[ _key - 1 ] = arguments[ _key ];
		}

		return isHTMLElementOfType.apply( void 0, [ TAG_INPUT, element ].concat( classNames ) );
	}

	function isWidgetImagePathInput( element ) {
		return isInputElement( element, CLASS_WIDGET_IMAGE_PATH );
	}

	function setAttribute( element, name, value ) {
		if ( ! isHTMLElement( element ) || ! isFunction( element.setAttribute ) || ! isNonEmptyString( name ) ) {
			return;
		}

		element.setAttribute( name, value );
	}

	var TAG_TEXTAREA = 'textarea';

	function isTextAreaElement( element ) {
		for ( var _len = arguments.length, classNames = new Array( _len > 1 ? _len - 1 : 0 ), _key = 1; _key < _len; _key++ ) {
			classNames[ _key - 1 ] = arguments[ _key ];
		}

		return isHTMLElementOfType.apply( void 0, [ TAG_TEXTAREA, element ].concat( classNames ) );
	}

	function setInputValue( element, value ) {
		if ( ! ( isInputElement( element ) || isTextAreaElement( element ) ) ) {
			return;
		}

		element.value = value;
	}

	function addClass( element, className ) {
		if ( ! isHTMLElement( element ) || ! hasClassList( element ) || ! isFunction( element.classList.add ) || ! isNonEmptyString( className ) ) {
			return;
		}

		element.classList.add( className );
	}

	function removeClass( element, className ) {
		if ( ! isHTMLElement( element ) || ! hasClassList( element ) || ! isFunction( element.classList.remove ) || ! isNonEmptyString( className ) ) {
			return;
		}

		element.classList.remove( className );
	}

	function toggleClass( add, element, className ) {
		if ( ! isBoolean( add ) ) {
			return;
		}

		var TOGGLE_CLASS = isTrue( add ) ? addClass : removeClass;

		TOGGLE_CLASS( element, className );
	}

	function hasStyle( element ) {
		return !! element && element.style instanceof CSSStyleDeclaration && isFunction( element.style.setProperty );
	}

	function isStyleProperty( property ) {
		return isString( property ) && hasStyle( document.documentElement ) && property in document.documentElement.style;
	}

	function updateInlineStyle( element, property, value, isPriority ) {
		if ( ! element || ! hasStyle( element ) || ! isStyleProperty( property ) || ! ( isString( value ) || isNumber( value ) ) || ! isBoolean( isPriority ) ) {
			return;
		}

		if ( isTrue( isPriority ) ) {
			element.style.setProperty( property, value, 'important' );

			return;
		}

		element.style.setProperty( property, value );
	}

	function updateElementInlineStyle( element, property, value ) {
		var isPriority = arguments.length > 3 && arguments[ 3 ] !== undefined ? arguments[ 3 ] : false;

		if ( ! isHTMLElement( element ) ) {
			return;
		}

		updateInlineStyle( element, property, value, isPriority );
	}

	var EVENT_CHANGE = 'change';
	var EVENT_TYPE_EVENT = 'Event';

	function isIe() {
		return !! window.MSInputMethodContext && !! document.documentMode;
	}

	function createEvent( type ) {
		if ( ! isNonEmptyString( type ) ) {
			return NULL_VALUE;
		}

		if ( isIe() ) {
			var EVENT_OBJECT = document.createEvent( EVENT_TYPE_EVENT );

			EVENT_OBJECT.initEvent( type, true, true );

			return EVENT_OBJECT;
		}

		return new Event( type, {
			bubbles: true,
			cancelable: true,
		} );
	}

	function createChangeEvent() {
		return createEvent( EVENT_CHANGE );
	}

	function isEvent( value ) {
		return value instanceof Event && isFunction( value.preventDefault ) && isFunction( value.stopPropagation );
	}

	function dispatchEvent( element, event ) {
		if ( ! isHTMLElement( element ) || ! isFunction( element.dispatchEvent ) || ! isEvent( event ) ) {
			return;
		}

		element.dispatchEvent( event );
	}

	function onDocumentClick( callback, options ) {
		onDocumentEvent( EVENT_CLICK, callback, options );
	}

	var EVENT_SELECT = 'select';
	var MEDIA_TYPE_IMAGE = 'image';
	var OBJECT_NAME_WP = 'wp';

	function getWp() {
		return getGlobalObject( OBJECT_NAME_WP );
	}

	function isMedia( mediaUploader ) {
		return !! mediaUploader && isFunction( mediaUploader.on ) && isFunction( mediaUploader.open );
	}

	function isWpMedia( wpObj ) {
		return !! wpObj && isFunction( wpObj.media );
	}

	function getMedia( args ) {
		if ( ! isObject( args ) ) {
			return NULL_OBJECT;
		}

		var WP = getWp();

		if ( ! isWpMedia( WP ) ) {
			return NULL_OBJECT;
		}

		var MEDIA = WP.media( args );

		return isMedia( MEDIA ) ? MEDIA : NULL_OBJECT;
	}

	function getMediaUploader() {
		var ADMIN = getAdminObject();
		var TITLE = getObjectValue( ADMIN, OBJECT_KEY_ADMIN_MEDIA_MODAL_TITLE );

		if ( ! isNonEmptyString( TITLE ) ) {
			return NULL_OBJECT;
		}

		var BUTTON_TEXT = getObjectValue( ADMIN, OBJECT_KEY_ADMIN_MEDIA_MODAL_BUTTON_TEXT_SELECT );

		return isNonEmptyString( BUTTON_TEXT ) ? getMedia( {
			title: TITLE,
			library: {
				type: MEDIA_TYPE_IMAGE,
			},
			button: {
				text: BUTTON_TEXT,
			},
			multiple: false,
		} ) : NULL_OBJECT;
	}

	var MEDIA_STATE_SELECTION = 'selection';

	function isMediaAttachment( value ) {
		return !! value && isString( value.url );
	}

	function isMediaAttachmentObject( obj ) {
		return !! obj && isFunction( obj.toJSON );
	}

	function isMediaStateSelection( value ) {
		return !! value && isFunction( value.first );
	}

	function isMediaUploaderInSelectEvent( mediaUploader ) {
		return !! mediaUploader && isFunction( mediaUploader.state ) && isFunction( mediaUploader.state().get );
	}

	function getMediaImagePath( mediaUploader ) {
		if ( ! isMediaUploaderInSelectEvent( mediaUploader ) ) {
			return EMPTY_STRING;
		}

		var STATE_SELECTION = mediaUploader.state().get( MEDIA_STATE_SELECTION );

		if ( ! isMediaStateSelection( STATE_SELECTION ) ) {
			return EMPTY_STRING;
		}

		var ATTACHMENT_OBJECT = STATE_SELECTION.first();

		if ( ! isMediaAttachmentObject( ATTACHMENT_OBJECT ) ) {
			return EMPTY_STRING;
		}

		var ATTACHMENT = ATTACHMENT_OBJECT.toJSON();

		return isMediaAttachment( ATTACHMENT ) ? ATTACHMENT.url : EMPTY_STRING;
	}

	function onMediaUploaderSelect( callback ) {
		if ( ! isFunction( callback ) ) {
			return;
		}

		var MEDIA_UPLOADER = getMediaUploader();

		if ( ! MEDIA_UPLOADER ) {
			return;
		}

		MEDIA_UPLOADER.on( EVENT_SELECT, function() {
			return callback( getMediaImagePath( MEDIA_UPLOADER ) );
		} );
		MEDIA_UPLOADER.open();
	}

	function dispatchWidgetImagePathInputChangeEvent( widgetImagePathInput ) {
		if ( ! isWidgetImagePathInput( widgetImagePathInput ) ) {
			return;
		}

		dispatchEvent( widgetImagePathInput, createChangeEvent() );
	}

	function updateWidgetImagePathInputUrl( widgetImagePathInput, imagePath ) {
		if ( ! isWidgetImagePathInput( widgetImagePathInput ) || ! isString( imagePath ) ) {
			return;
		}

		setInputValue( widgetImagePathInput, imagePath );
	}

	function updateWidgetImageUrl( widgetImage, imagePath ) {
		if ( ! isWidgetImage( widgetImage ) || ! isString( imagePath ) ) {
			return;
		}

		if ( isIe() ) {
			updateElementInlineStyle( widgetImage, CSS_BACKGROUND_IMAGE, "url('".concat( imagePath, "')" ) );

			return;
		}

		setAttribute( widgetImage, ATTRIBUTE_SRC, imagePath );
	}

	function updateWidgetImageAndControlVisibilities( widgetImageButton, imagePath ) {
		if ( ! isString( imagePath ) ) {
			return;
		}

		var WIDGET_IMAGE_CONTROL = getWidgetImageControl( widgetImageButton );

		if ( ! WIDGET_IMAGE_CONTROL ) {
			return;
		}

		var WIDGET_IMAGE = getWidgetImage( WIDGET_IMAGE_CONTROL );

		if ( ! WIDGET_IMAGE ) {
			return;
		}

		var WIDGET_IMAGE_PATH_INPUT = getWidgetImagePathInput( WIDGET_IMAGE_CONTROL );

		if ( ! WIDGET_IMAGE_PATH_INPUT ) {
			return;
		}

		updateWidgetImageUrl( WIDGET_IMAGE, imagePath );
		updateWidgetImagePathInputUrl( WIDGET_IMAGE_PATH_INPUT, imagePath );
		dispatchWidgetImagePathInputChangeEvent( WIDGET_IMAGE_PATH_INPUT );
		toggleClass( isNonEmptyString( imagePath ), WIDGET_IMAGE_CONTROL, CLASS_WIDGET_HAS_IMAGE );
	}

	function addWidgetImageEvents() {
		onDocumentClick( function( event ) {
			if ( ! isEvent( event ) ) {
				return;
			}

			if ( isWidgetAddImageButton( event.target ) || isWidgetChangeImageButton( event.target ) ) {
				onMediaUploaderSelect( function( imagePath ) {
					return updateWidgetImageAndControlVisibilities( event.target, imagePath );
				} );

				return;
			}

			if ( isWidgetRemoveImageButton( event.target ) ) {
				updateWidgetImageAndControlVisibilities( event.target, EMPTY_STRING );
			}
		} );
	}

	function addWidgetEvents() {
		addWidgetImageEvents();
	}

	function addAdminEvents() {
		addAdminNoticeEvents();
		addClassicEditorEvents();
		addWidgetEvents();
	}

	addAdminEvents();
}() );
