/**
 * 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 CSS_MARGIN_TOP = 'margin-top';
	var CSS_MIN_HEIGHT = 'min-height';
	var CSS_TOP = 'top';
	var ID_ADMIN_BAR = 'wpadminbar';
	var CHARACTER_HASH = '#';
	var INDEX_SECOND = 1;
	var EMPTY_STRING = '';

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

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

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

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

	var INDEX_FIRST = 0;

	function stringStartsWith( fullString, partialString ) {
		return isNonEmptyString( fullString ) && isNonEmptyString( partialString ) && fullString.indexOf( partialString ) === INDEX_FIRST;
	}

	var NULL_ELEMENT = null;

	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 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 getElementById( id ) {
		var rootElement = arguments.length > 1 && arguments[ 1 ] !== undefined ? arguments[ 1 ] : document;

		return isNonEmptyString( id ) && callElementMethod( 'getElementById', stringStartsWith( id, CHARACTER_HASH ) ? id.slice( INDEX_SECOND ) : id, rootElement );
	}

	function getAdminBar() {
		return getElementById( ID_ADMIN_BAR );
	}

	function hasAdminBar() {
		return !! getAdminBar();
	}

	var CLASS_NAV_MAIN_NAVIGATION = 'main-navigation';
	var NULL_VALUE = null;

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

	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 ) );
	}

	function getMainNavigation() {
		return getElementByClassName( CLASS_NAV_MAIN_NAVIGATION );
	}

	var CLASS_NAV_MENU_CONTAINER = 'menu-container';

	function getMenuContainer() {
		return getElementByClassName( CLASS_NAV_MENU_CONTAINER );
	}

	var CLASS_MOBILE_SEARCH = 'mobile-search';

	function getMobileSearch() {
		return getElementByClassName( CLASS_MOBILE_SEARCH );
	}

	var CLASS_SECTION_SITE = 'site';

	function getSite() {
		return getElementByClassName( CLASS_SECTION_SITE );
	}

	var CLASS_SECTION_SITE_HEADER = 'site-header';

	function getSiteHeader() {
		return getElementByClassName( CLASS_SECTION_SITE_HEADER );
	}

	var PIXEL_ZERO = 0;
	var RECT_BOTTOM = 'bottom';

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

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

	function getRectProp( element, property ) {
		if ( ! isHTMLElement( element ) || ! isFunction( element.getBoundingClientRect ) ) {
			return PIXEL_ZERO;
		}

		var RECT_PROP = getObjectValue( element.getBoundingClientRect(), property );

		return isNumber( RECT_PROP ) ? RECT_PROP : PIXEL_ZERO;
	}

	function getViewportBottom( element ) {
		return getRectProp( element, RECT_BOTTOM );
	}

	function getAdminBarVisibleHeight() {
		var ADMIN_BAR_VIEWPORT_BOTTOM = getViewportBottom( getAdminBar() );

		return isPositiveNumber( ADMIN_BAR_VIEWPORT_BOTTOM ) ? ADMIN_BAR_VIEWPORT_BOTTOM : PIXEL_ZERO;
	}

	function getViewportHeight() {
		return window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
	}

	var ATTRIBUTE_STYLE = 'style';

	function filter( items, callback ) {
		return !! items && isFunction( items.filter ) && isFunction( callback ) ? items.filter( callback ) : NULL_VALUE;
	}

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

	function removeAttribute( element ) {
		if ( ! isHTMLElement( element ) || ! isFunction( element.removeAttribute ) ) {
			return;
		}

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

		forEach( names, function( name ) {
			return element.removeAttribute( name );
		} );
	}

	function resetElementInlineStyle( element ) {
		if ( ! isHTMLElement( element ) || ! hasStyle( element ) ) {
			return;
		}

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

		forEach( filter( properties, function( property ) {
			return property in element.style;
		} ), function( property ) {
			return element.style[ property ] = EMPTY_STRING;
		} );

		if ( ! isNonEmptyString( element.style.cssText ) ) {
			removeAttribute( element, ATTRIBUTE_STYLE );
		}
	}

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

	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_LOAD = 'load';

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

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

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

	function onWindowLoad( callback, options ) {
		onWindowEvent( EVENT_LOAD, callback, options );
	}

	var EVENT_RESIZE = 'resize';

	function onWindowResize( callback, options ) {
		onWindowEvent( EVENT_RESIZE, callback, options );
	}

	var EVENT_SCROLL = 'scroll';

	function onWindowScroll( callback, options ) {
		onWindowEvent( EVENT_SCROLL, callback, options );
	}

	function toPx( value ) {
		return isNumber( value ) ? value + 'px' : EMPTY_STRING;
	}

	var CSS_POSITION = 'position';
	var CSS_VALUE_FIXED = 'fixed';

	function getStyleProp( element, property ) {
		if ( ! isHTMLElement( element ) ) {
			return NULL_VALUE;
		}

		var STYLE_PROP = getObjectValue( window.getComputedStyle( element ), property );

		return isString( STYLE_PROP ) ? STYLE_PROP : NULL_VALUE;
	}

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

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

	function isFixedElement( element ) {
		return areStringsEqualIgnoreCase( getStyleProp( element, CSS_POSITION ), CSS_VALUE_FIXED );
	}

	function ensureFooterAtPageBottom( adminBarVisibleHeight ) {
		if ( ! isNumber( adminBarVisibleHeight ) ) {
			return;
		}

		updateElementInlineStyle( getSite(), CSS_MIN_HEIGHT, toPx( getViewportHeight() - adminBarVisibleHeight ) );
	}

	function adjustFixedElementPageTop( element, property, AdminBarVisibleHeight ) {
		if ( ! isNumber( AdminBarVisibleHeight ) ) {
			return;
		}

		if ( isFixedElement( element ) ) {
			updateElementInlineStyle( element, property, toPx( AdminBarVisibleHeight ) );

			return;
		}

		resetElementInlineStyle( element, property );
	}

	function adjustFixedElementPageTops( adminBarVisibleHeight ) {
		adjustFixedElementPageTop( getSiteHeader(), CSS_TOP, adminBarVisibleHeight );
		adjustFixedElementPageTop( getMainNavigation(), CSS_MARGIN_TOP, adminBarVisibleHeight );
		adjustFixedElementPageTop( getMenuContainer(), CSS_MARGIN_TOP, adminBarVisibleHeight );
		adjustFixedElementPageTop( getMobileSearch(), CSS_MARGIN_TOP, adminBarVisibleHeight );
	}

	function adjustElementViewportPositions() {
		if ( ! hasAdminBar() ) {
			return;
		}

		var ADMIN_BAR_VISIBLE_HEIGHT = getAdminBarVisibleHeight();

		adjustFixedElementPageTops( ADMIN_BAR_VISIBLE_HEIGHT );
		ensureFooterAtPageBottom( ADMIN_BAR_VISIBLE_HEIGHT );
	}

	function addAdminBarAdjustmentEvents() {
		onWindowLoad( adjustElementViewportPositions );
		onWindowResize( adjustElementViewportPositions );
		onWindowScroll( adjustElementViewportPositions );
	}

	var CSS_OVERFLOW = 'overflow';
	var CSS_VALUE_ABSOLUTE = 'absolute';
	var IS_PRIORITY = true;
	var CSS_VALUE_VISIBLE = 'visible';
	var CSS_VALUE_ZERO = 0;
	var CLASS_SITE_HEADER_SEARCH_BACKGROUND = 'site-header__search-background';

	function isSiteHeaderSearchBackground( element ) {
		return isHTMLElement( element, CLASS_SITE_HEADER_SEARCH_BACKGROUND );
	}

	var CLASS_SITE_HEADER_SEARCH_BUTTON = 'site-header__search-button';

	function isSiteHeaderSearchButton( element ) {
		return isHTMLElement( element, CLASS_SITE_HEADER_SEARCH_BUTTON );
	}

	var CLASS_SITE_HEADER_SEARCH_CLOSE_BUTTON = 'site-header__close-search-button';

	function isSiteHeaderSearchCloseButton( element ) {
		return isHTMLElement( element, CLASS_SITE_HEADER_SEARCH_CLOSE_BUTTON );
	}

	var CLASS_SITE_HEADER_SEARCH = 'site-header__search';
	var CLASS_SHARED_SHOW = 'show';
	var SELECTOR_SITE_HEADER_SEARCH_OPENED = '.'.concat( CLASS_SITE_HEADER_SEARCH, '.' ).concat( CLASS_SHARED_SHOW );

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

		return callElementMethod( 'querySelector', selectors, rootElement );
	}

	function isSiteHeaderSearchOpened() {
		return !! querySelector( SELECTOR_SITE_HEADER_SEARCH_OPENED );
	}

	var CLASS_SECTION_SITE_CONTENT = 'site-content';

	function getSiteContent() {
		return getElementByClassName( CLASS_SECTION_SITE_CONTENT );
	}

	function isSite( element ) {
		return isHTMLElement( element, CLASS_SECTION_SITE );
	}

	var RECT_HEIGHT = 'height';

	function getHeight( element ) {
		return getRectProp( element, RECT_HEIGHT );
	}

	function getStylePropFloat( element, property ) {
		var STYLE_PROP = parseFloat( getStyleProp( element, property ) );

		return isNumber( STYLE_PROP ) ? STYLE_PROP : PIXEL_ZERO;
	}

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

	function isKey( event, key, keyCode ) {
		return isEvent( event ) && isNonEmptyString( key ) && isNumber( keyCode ) ? areStringsEqualIgnoreCase( event.key, key ) || event.keyCode === keyCode : false;
	}

	function isEnterKey( event ) {
		return isKey( event, 'Enter', 13 );
	}

	function isEscapeKey( event ) {
		return isKey( event, 'Escape', 27 );
	}

	var EVENT_CLICK = 'click';

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

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

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

	var EVENT_KEYDOWN = 'keydown';

	function onDocumentKeydown( callback, options ) {
		onDocumentEvent( EVENT_KEYDOWN, callback, options );
	}

	var CLASS_SHARED_MOBILE_DEVICE = 'mobile-device';

	function isMobile() {
		return isHTMLElement( document.body, CLASS_SHARED_MOBILE_DEVICE );
	}

	function removeSiteMarginTop( site ) {
		if ( ! isSite( site ) ) {
			return;
		}

		resetElementInlineStyle( site, CSS_MARGIN_TOP );
	}

	function removeSiteTop( site ) {
		if ( ! isSite( site ) ) {
			return;
		}

		resetElementInlineStyle( site, CSS_TOP );
	}

	function removeSitePosition( site ) {
		if ( ! isSite( site ) ) {
			return;
		}

		resetElementInlineStyle( site, CSS_POSITION );
	}

	function removeBodyOverflow() {
		resetElementInlineStyle( document.body, CSS_OVERFLOW );
	}

	function removeHtmlMarginTop() {
		resetElementInlineStyle( document.documentElement, CSS_MARGIN_TOP );
	}

	function addSiteMarginTop( site, siteContentTop ) {
		if ( ! isSite( site ) || ! isNumber( siteContentTop ) ) {
			return;
		}

		updateElementInlineStyle( site, CSS_MARGIN_TOP, toPx( Math.abs( siteContentTop ) + getHeight( getAdminBar() ) ) );
	}

	function addSiteTop( site ) {
		if ( ! isSite( site ) ) {
			return;
		}

		updateElementInlineStyle( site, CSS_TOP, CSS_VALUE_ZERO );
	}

	function addSitePositionAbsolute( site ) {
		if ( ! isSite( site ) ) {
			return;
		}

		updateElementInlineStyle( site, CSS_POSITION, CSS_VALUE_ABSOLUTE );
	}

	function setBodyOverflowVisible() {
		updateElementInlineStyle( document.body, CSS_OVERFLOW, CSS_VALUE_VISIBLE );
	}

	function addHtmlMarginTop( siteContentTop ) {
		if ( ! isNumber( siteContentTop ) ) {
			return;
		}

		updateElementInlineStyle( document.documentElement, CSS_MARGIN_TOP, toPx( siteContentTop ), IS_PRIORITY );
	}

	function getSiteContentTop() {
		return getStylePropFloat( getSiteContent(), CSS_TOP );
	}

	function isAdminBarFixed() {
		return isFixedElement( getAdminBar() );
	}

	function adjustSiteHeaderPageTop() {
		if ( ! hasAdminBar() ) {
			return;
		}

		var SITE = getSite();

		if ( ! SITE ) {
			return;
		}

		if ( ! isAdminBarFixed() && isSiteHeaderSearchOpened() ) {
			var SITE_CONTENT_TOP = getSiteContentTop();

			addHtmlMarginTop( SITE_CONTENT_TOP );
			setBodyOverflowVisible();
			addSitePositionAbsolute( SITE );
			addSiteTop( SITE );
			addSiteMarginTop( SITE, SITE_CONTENT_TOP );

			return;
		}

		removeHtmlMarginTop();
		removeBodyOverflow();
		removeSitePosition( SITE );
		removeSiteTop( SITE );
		removeSiteMarginTop( SITE );
	}

	function addSiteHeaderSearchEvents() {
		if ( isMobile() ) {
			return;
		}

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

			if ( isSiteHeaderSearchButton( event.target ) || isSiteHeaderSearchBackground( event.target ) || isSiteHeaderSearchCloseButton( event.target ) ) {
				adjustSiteHeaderPageTop();
			}
		} );
		onDocumentKeydown( function( event ) {
			if ( ! isEvent( event ) ) {
				return;
			}

			if ( isEnterKey( event ) ) {
				if ( isSiteHeaderSearchButton( event.target ) || isSiteHeaderSearchCloseButton( event.target ) ) {
					event.preventDefault();
					adjustSiteHeaderPageTop();
				}

				return;
			}

			if ( isEscapeKey( event ) ) {
				if ( ! isSiteHeaderSearchOpened() ) {
					event.preventDefault();
					adjustSiteHeaderPageTop();
				}
			}
		} );
		onWindowResize( adjustSiteHeaderPageTop );
	}

	function addAdminBarShowingPostEvents() {
		addAdminBarAdjustmentEvents();
		addSiteHeaderSearchEvents();
	}

	addAdminBarShowingPostEvents();
}() );
