(function ( $ ) {
	'use strict';

	$( document ).ready( function () {
		qodefCustomCursor.init();
	} );

	var qodefCustomCursor = {
		init: function () {
			var cursorHolder     = $( '#qodef-custom-cursor' ),
				elementHolder    = $( '.qodef-portfolio-list.qodef-custom-view--cursor.swiper-container-initialized' ),
				elementHolderTwo = $( '.qodef-image-with-text.qodef-custom-view--cursor' ),
				cursor           = cursorHolder.find( '.qodef-cursor-dot' ),
				cursorText       = cursorHolder.find( '.qodef-cursor-text-holder' ),
				notouch          = $( 'html.no-touchevents' );

			if ( elementHolder.length ) {

				$( elementHolder ).on(
					'mouseenter',
					function () {
						elementHolder.addClass( 'disable-default-cursor' );
						cursorHolder.css(
							'visibility',
							'visible'
						);
					}
				);

				$( elementHolder ).on(
					'mouseleave',
					function () {
						elementHolder.removeClass( 'disable-default-cursor' );
						cursorHolder.css(
							'visibility',
							'hidden'
						);
					}
				);

				$( elementHolder ).on(
					'mousemove',
					function () {
						elementHolder.css(
							'cursor',
							'none'
						);
					}
				);
			}

			if ( elementHolderTwo.length ) {

				$( elementHolderTwo ).on(
					'mouseenter',
					function () {
						elementHolderTwo.addClass( 'disable-default-cursor' );
						cursorHolder.css(
							'visibility',
							'visible'
						);
					}
				);

				$( elementHolderTwo ).on(
					'mouseleave',
					function () {
						elementHolderTwo.removeClass( 'disable-default-cursor' );
						cursorHolder.css(
							'visibility',
							'hidden'
						);
					}
				);

				$( elementHolderTwo ).on(
					'mousemove',
					function () {
						elementHolderTwo.css(
							'cursor',
							'none'
						);
					}
				);
			}

			if ( notouch && cursorHolder.length ) {
				//init

				cursor.css( {
					transform: 'matrix(1, 0, 0, 1, ' + qodef.windowWidth / 2 + ', ' + qodef.windowHeight / 2 + ')'
				} );

				cursorText.css( {
					transform: 'matrix(1, 0, 0, 1, ' + qodef.windowWidth / 2 + ', ' + qodef.windowHeight / 2 + ')'
				} );


				//move
				$( document )
				.on(
					'mousemove',
					function ( e ) {
						var x = e.clientX,
							y = e.clientY;

						TweenMax.to( cursor,
							0,
							{
								x: x,
								y: y,
								delay: 0
							}
						);

						TweenMax.to( cursorText,
							0,
							{
								x: x,
								y: y,
								delay: 0
							}
						);

					}
				);

				var cursorTransitionDruration = .3;

				//hover links
				var linkSelectors = 'a, .swiper-button-next, .swiper-button-prev, button';
				$( document ).on(
					'mouseenter',
					linkSelectors,
					function () {
						cursorHolder.addClass( 'qodef--hovering' );
						qodefCustomCursor.linkCursor(
							cursor,
							cursorTransitionDruration
						);
					}
				);
				$( document ).on(
					'mouseleave',
					linkSelectors,
					function () {
						cursorHolder.removeClass( 'qodef--hovering' );
						qodefCustomCursor.resetSize(
							cursor,
							cursorTransitionDruration
						);
					}
				);

				//view links
				var viewSelectors = '.qodef-portfolio-list.qodef-custom-view--cursor .qodef-e-media-image img, .qodef-portfolio-list.qodef-custom-view--cursor .qodef-e-inner a,' + '.qodef-portfolio-list.qodef-custom-view--cursor .swiper-wrapper .qodef-e.swiper-slide .qodef-e-media-image a img,' + '.qodef-image-with-text.qodef-custom-view--cursor .qodef-m-image a img, .qodef-image-with-text.qodef-custom-view--cursor .qodef-m-content a';

				$( document ).on(
					'mousemove',
					viewSelectors,
					function () {
						if ( ! cursorHolder.hasClass( 'qodef--view' ) ) {
							cursorHolder.removeClass( 'qodef--hovering' );
							cursorHolder.addClass( 'qodef--view' );
							qodefCustomCursor.viewCursor(
								cursor,
								cursorTransitionDruration
							);
						}
					}
				);
				$( document ).on(
					'mouseleave',
					viewSelectors,
					function () {
						cursorHolder.removeClass( 'qodef--view' );
						qodefCustomCursor.resetSize(
							cursor,
							cursorTransitionDruration
						);
					}
				);

				//dragging
				var dragging = '.qodef-swiper--initialized:not(.qodef-testimonials-list) .swiper-wrapper,' + '.swiper-container-horizontal';
				$( document ).on(
					'mouseenter',
					dragging,
					function () {
						if ( ! (cursorHolder.hasClass( 'qodef--hovering' )) ) {
							cursorHolder.addClass( 'qodef--dragging qodef--draging-reserved' );
							qodefCustomCursor.dragCursor(
								cursor,
								cursorTransitionDruration
							);

							$( document ).on(
								'mouseenter',
								linkSelectors,
								function () {
									cursorHolder.removeClass( 'qodef--dragging' );
									qodefCustomCursor.linkCursor(
										cursor,
										cursorTransitionDruration
									);
								}
							);

							$( document ).on(
								'mouseleave',
								linkSelectors,
								function () {
									if ( cursorHolder.hasClass( 'qodef--draging-reserved' ) ) {
										cursorHolder.addClass( 'qodef--dragging' );
										qodefCustomCursor.dragCursor(
											cursor,
											cursorTransitionDruration
										);
									}
								}
							);

							$( document ).on(
								'mouseleave',
								viewSelectors,
								function () {
									if ( cursorHolder.hasClass( 'qodef--draging-reserved' ) ) {
										cursorHolder.addClass( 'qodef--dragging' );
										qodefCustomCursor.dragCursor(
											cursor,
											cursorTransitionDruration
										);
									}
								}
							);
						}
					}
				);
				$( document ).on(
					'mouseleave',
					dragging,
					function () {
						cursorHolder.removeClass( 'qodef--dragging qodef--draging-reserved' );
						qodefCustomCursor.resetSize(
							cursor,
							cursorTransitionDruration
						);
					}
				);
			}
		},
		resetSize: function ( $cursor, $duration ) {
			TweenMax.to(
				$cursor,
				$duration,
				{
					scaleX: 1,
					scaleY: 1,
					transformStyle: 'preserve-3d',
					delay: 0,
					ease: Power3.easeOut
				}
			);
		},
		linkCursor: function ( $cursor, $duration ) {
			TweenMax.to(
				$cursor,
				$duration,
				{
					scaleX: 2,
					scaleY: 2,
					transformStyle: 'preserve-3d',
					delay: 0,
					ease: Power3.easeOut
				}
			);
		},
		viewCursor: function ( $cursor, $duration ) {
			TweenMax.to(
				$cursor,
				$duration,
				{
					scaleX: 13,
					scaleY: 13,
					transformStyle: 'preserve-3d',
					delay: 0,
					ease: Power3.easeOut
				}
			);
		},
		dragCursor: function ( $cursor, $duration ) {
			TweenMax.to(
				$cursor,
				$duration,
				{
					scaleX: 13,
					scaleY: 13,
					transformStyle: 'preserve-3d',
					delay: 0,
					ease: Power3.easeOut
				}
			);
		}
	};
})( jQuery );