/**
 * 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 CLASS_ACCESSIBILITY_AUTOFOCUSED = 'autofocused';
	var SELECTOR_ACCESSIBILITY_AUTO_FOCUSED_ELEMENT = '.'.concat( CLASS_ACCESSIBILITY_AUTOFOCUSED );
	var NULL_ELEMENT = null;

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

	var INDEX_FIRST = 0;

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

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

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

	var EMPTY_STRING = '';

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

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

	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 isNodeList( element ) {
		return element instanceof NodeList;
	}

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

		return isNodeList( ELEMENTS ) ? ELEMENTS : NULL_ELEMENT;
	}

	function getAutoFocusedElements() {
		return querySelectorAll( SELECTOR_ACCESSIBILITY_AUTO_FOCUSED_ELEMENT );
	}

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

	var EVENT_CLICK = 'click';

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

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

	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 onDocumentClick( callback, options ) {
		onDocumentEvent( EVENT_CLICK, callback, options );
	}

	var EVENT_FOCUSIN = 'focusin';

	function onDocumentFocusin( callback, options ) {
		onDocumentEvent( EVENT_FOCUSIN, callback, options );
	}

	var EVENT_FOCUSOUT = 'focusout';

	function onDocumentFocusout( callback, options ) {
		onDocumentEvent( EVENT_FOCUSOUT, callback, options );
	}

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

	function createPreviousEventObject() {
		var wasClick = false;
		var wasMouseDown = false;

		return {
			get wasClick() {
				var WAS_CLICK = wasClick;

				wasClick = false;

				return WAS_CLICK;
			},
			setWasClick: function setWasClick() {
				return wasClick = true;
			},
			get wasMouseDown() {
				var WAS_MOUSE_DOWN = wasMouseDown;

				wasMouseDown = false;

				return WAS_MOUSE_DOWN;
			},
			setWasMouseDown: function setWasMouseDown() {
				return wasMouseDown = true;
			},
		};
	}

	var PREVIOUS_EVENT = createPreviousEventObject();

	function clearAutoFocusedElement( element ) {
		removeClass( element, CLASS_ACCESSIBILITY_AUTOFOCUSED );
	}

	function clearAutoFocusedElements() {
		forEach( getAutoFocusedElements(), clearAutoFocusedElement );
	}

	function addAutoFocusElementEvents() {
		onDocumentClick( function() {
			return PREVIOUS_EVENT.setWasClick();
		} );
		onDocumentFocusin( function( event ) {
			if ( ! isEvent( event ) ) {
				return;
			}

			clearAutoFocusedElements();

			if ( PREVIOUS_EVENT.wasClick ) {
				addClass( event.target, CLASS_ACCESSIBILITY_AUTOFOCUSED );
			}
		} );
		onDocumentFocusout( clearAutoFocusedElements );
	}

	function addAccessibilityEvents() {
		addAutoFocusElementEvents();
	}

	var CSS_BOTTOM = 'bottom';
	var CSS_LEFT = 'left';
	var CSS_MARGIN_TOP = 'margin-top';
	var CSS_POSITION = 'position';
	var CSS_RIGHT = 'right';
	var CSS_VALUE_FIXED = 'fixed';
	var CSS_VALUE_ZERO = 0;
	var CLASS_SECTION_SITE_CONTENT = 'site-content';
	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 getSiteContent() {
		return getElementByClassName( CLASS_SECTION_SITE_CONTENT );
	}

	var CLASS_SECTION_SITE_FOOTER = 'site-footer';

	function getSiteFooter() {
		return getElementByClassName( CLASS_SECTION_SITE_FOOTER );
	}

	var CLASS_SECTION_SITE_HEADER = 'site-header';

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

	var RECT_HEIGHT = 'height';
	var PIXEL_ZERO = 0;

	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 getHeight( element ) {
		return getRectProp( element, RECT_HEIGHT );
	}

	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 getStylePropFloat( element, property ) {
		var STYLE_PROP = parseFloat( getStyleProp( element, property ) );

		return isNumber( STYLE_PROP ) ? STYLE_PROP : 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 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 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 );
	}

	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 getSiteFooterHeight() {
		var SITE_FOOTER = getSiteFooter();

		return !! SITE_FOOTER ? getHeight( SITE_FOOTER ) + getStylePropFloat( SITE_FOOTER, CSS_MARGIN_TOP ) : CSS_VALUE_ZERO;
	}

	function getSiteContentHeight() {
		var SITE_CONTENT = getSiteContent();

		return !! SITE_CONTENT ? getHeight( SITE_CONTENT ) + getStylePropFloat( SITE_CONTENT, CSS_MARGIN_TOP ) : CSS_VALUE_ZERO;
	}

	function getSiteHeaderHeight() {
		var SITE_HEADER = getSiteHeader();

		return !! SITE_HEADER && ! isFixedElement( SITE_HEADER ) ? getHeight( SITE_HEADER ) : CSS_VALUE_ZERO;
	}

	function ensureFooterAtPageBottom() {
		var SITE_FOOTER = getSiteFooter();

		if ( ! SITE_FOOTER ) {
			return;
		}

		var SITE_HEIGHT = getSiteHeaderHeight() + getSiteContentHeight() + getSiteFooterHeight();

		if ( SITE_HEIGHT < getViewportHeight() ) {
			updateElementInlineStyle( SITE_FOOTER, CSS_POSITION, CSS_VALUE_FIXED );
			updateElementInlineStyle( SITE_FOOTER, CSS_BOTTOM, CSS_VALUE_ZERO );
			updateElementInlineStyle( SITE_FOOTER, CSS_LEFT, CSS_VALUE_ZERO );
			updateElementInlineStyle( SITE_FOOTER, CSS_RIGHT, CSS_VALUE_ZERO );

			return;
		}

		resetElementInlineStyle( SITE_FOOTER, CSS_POSITION, CSS_BOTTOM, CSS_LEFT, CSS_RIGHT );
	}

	function addSiteFooterEvents() {
		onWindowLoad( ensureFooterAtPageBottom );
		onWindowResize( ensureFooterAtPageBottom );
	}

	function addIeEvents() {
		addSiteFooterEvents();
	}

	addAccessibilityEvents();
	addIeEvents();
}() );
