(function ( $ ) {
	'use strict';

	window.qodeBadgesForWooCommerce = {};

	qodeBadgesForWooCommerce.body         = $( 'body' );
	qodeBadgesForWooCommerce.html         = $( 'html' );
	qodeBadgesForWooCommerce.windowWidth  = $( window ).width();
	qodeBadgesForWooCommerce.windowHeight = $( window ).height();
	qodeBadgesForWooCommerce.scroll       = 0;

	$( document ).ready(
		function () {
			qodeBadgesForWooCommerce.scroll = $( window ).scrollTop();
		}
	);

	$( window ).resize(
		function () {
			qodeBadgesForWooCommerce.windowWidth  = $( window ).width();
			qodeBadgesForWooCommerce.windowHeight = $( window ).height();
		}
	);

	$( window ).scroll(
		function () {
			qodeBadgesForWooCommerce.scroll = $( window ).scrollTop();
		}
	);

	$( window ).on(
		'load',
		function () {
		}
	);

	/**
	 * Init animation on appear
	 */
	var qodeBadgesForWooCommerceAppear = {
		init: function () {
			this.holder = $( '.qbfw--has-appear:not(.qbfw--appeared)' );

			if ( this.holder.length ) {
				this.holder.each(
					function () {
						var $holder = $( this );

						qodeBadgesForWooCommerce.qodeBadgesForWooCommerceIsInViewport.check(
							$holder,
							function () {
								qodeBadgesForWooCommerce.qodeBadgesForWooCommerceWaitForImages.check(
									$holder,
									function () {
										$holder.addClass( 'qbfw--appeared' );
									}
								);
							}
						);
					}
				);
			}
		},
	};

	qodeBadgesForWooCommerce.qodeBadgesForWooCommerceAppear = qodeBadgesForWooCommerceAppear;

	var qodeBadgesForWooCommerceIsInViewport = {
		check: function ( $element, callback, onlyOnce, callbackOnExit ) {
			if ( $element.length ) {
				// When item is 15% in the viewport.
				var offset = typeof $element.data( 'viewport-offset' ) !== 'undefined' ? $element.data( 'viewport-offset' ) : 0.15;

				var observer = new IntersectionObserver(
					function ( entries ) {
						// isIntersecting is true when element and viewport are overlapping
						// isIntersecting is false when element and viewport don't overlap.
						if ( entries[0].isIntersecting === true ) {
							callback.call( $element );

							// Stop watching the element when it's initialize.
							if ( onlyOnce !== false ) {
								observer.disconnect();
							}
						} else if ( callbackOnExit && onlyOnce === false ) {
							callbackOnExit.call( $element );
						}
					},
					{ threshold: [offset] }
				);

				observer.observe( $element[0] );
			}
		},
	};

	qodeBadgesForWooCommerce.qodeBadgesForWooCommerceIsInViewport = qodeBadgesForWooCommerceIsInViewport;

	/**
	 * Check element images to loaded
	 */
	var qodeBadgesForWooCommerceWaitForImages = {
		check: function ( $element, callback ) {
			if ( $element.length ) {
				var images       = $element.find( 'img' );
				var images_count = images.length;

				if ( images_count ) {
					var counter = 0;

					for ( var index = 0; index < images_count; index++ ) {
						var img = images[index];

						if ( img.complete ) {
							counter++;

							if ( counter === images.length ) {
								callback.call( $element );
							}
						} else {
							var image = new Image();

							image.addEventListener(
								'load',
								function () {
									counter++;
									if ( counter === images.length ) {
										callback.call( $element );
										return false;
									}
								},
								false
							);
							image.src = img.src;
						}
					}
				} else {
					callback.call( $element );
				}
			}
		},
	};

	qodeBadgesForWooCommerce.qodeBadgesForWooCommerceWaitForImages = qodeBadgesForWooCommerceWaitForImages;

	var qodeBadgesForWooCommerceScroll = {
		disable: function () {
			if ( window.addEventListener ) {
				window.addEventListener(
					'wheel',
					qodeBadgesForWooCommerceScroll.preventDefaultValue,
					{ passive: false }
				);
			}

			// window.onmousewheel = document.onmousewheel = qodeBadgesForWooCommerceScroll.preventDefaultValue;.
			document.onkeydown = qodeBadgesForWooCommerceScroll.keyDown;
		},
		enable: function () {
			if ( window.removeEventListener ) {
				window.removeEventListener(
					'wheel',
					qodeBadgesForWooCommerceScroll.preventDefaultValue,
					{ passive: false }
				);
			}
			window.onmousewheel = document.onmousewheel = document.onkeydown = null;
		},
		preventDefaultValue: function ( e ) {
			e = e || window.event;
			if ( e.preventDefault ) {
				e.preventDefault();
			}
			e.returnValue = false;
		},
		keyDown: function ( e ) {
			var keys = [37, 38, 39, 40];
			for ( var i = keys.length; i--; ) {
				if ( e.keyCode === keys[i] ) {
					qodeBadgesForWooCommerceScroll.preventDefaultValue( e );
					return;
				}
			}
		}
	};

	qodeBadgesForWooCommerce.qodeBadgesForWooCommerceScroll = qodeBadgesForWooCommerceScroll;

})( jQuery );
