(function ($) {
	'use strict';

	var ecomus = ecomus || {};

	ecomus.init = function () {
		if (ecomusAjaxSearch.header_ajax_search != 'yes') {
			return;
		}

        this.instanceSearch();
		this.focusSearch();
		this.activeTabs();
    }

	ecomus.focusSearch = function() {
		$(document.body).on('ecomus_modal_opened', function(event, $target) {
			if( $target.length && $target.hasClass('search-modal') ) {
				$target.find('.search-modal__field').trigger( 'focus' );
			}
		});
	}

	ecomus.activeTabs = function() {
		$(document.body).on('ecomus_ajax_search_request_success', function(event, $results ) {
			var $tabHeader = $results.find('.results-tab-header'),
				$tabContent = $results.find('.results-tab-content'),
				$modalContent = $results.closest('.modal__content');

			$tabHeader.find('.results-tab-button:first-child').addClass('active');
			$tabContent.find('.result-tab-item:first-child').addClass('active');
			$modalContent.addClass('active');

			$tabHeader .on('click', '.results-tab-button', function(e) {
				e.preventDefault();
				$tabHeader.find('.results-tab-button').removeClass('active');
				$(this).addClass('active');

				var target = $(this).data('target');

				$tabContent.find('.result-tab-item').removeClass('active');
				$tabContent.find('.em-col-' + target).addClass('active');
				$tabContent.find('.em-col-' + target).closest('.em-col').addClass('active');
				$modalContent.removeClass('active');
			});
		});
	}

	ecomus.instanceSearch = function () {
		var $modal = $('#search-modal'),
			$results = $modal.find('.modal__content-results'),
			$suggestion = $modal.find('.modal__content-suggestion');

		var xhr = null,
			searchCache = {};

		$modal.on('keyup', '.search-modal__field', function (e) {
			var valid = false,
			$search = $(this);

			if (typeof e.which == 'undefined') {
				valid = true;
			} else if (typeof e.which == 'number' && e.which > 0) {
				valid = !e.ctrlKey && !e.metaKey && !e.altKey;
			}

			if (!valid) {
				return;
			}

			if (xhr) {
				xhr.abort();
			}

			$modal.find('.modal__content-results').html('');

			var $currentForm = $search.closest('.search-modal__form'),
				$currentContent = $search.closest('.modal__container');

			if ($search.val().length < 2) {
				$currentContent.removeClass('searching searched actived found-products found-no-product invalid-length');
				$results.hide();
				$suggestion.show();
			}

			search($currentForm);
		}).on('focusout', '.search-modal__field', function () {
			var $search = $(this),
				$currentContent = $search.closest('.modal__container');

			if ($search.val().length < 2) {
				$currentContent.removeClass('searching searched actived found-products found-no-product invalid-length');
			}
		}).on('click', '.close-search-modal__results', function (e) {
			e.preventDefault();
			$modal.find('.search-modal__field').val('');
			$modal.find('.modal__container').removeClass('searching searched actived found-products found-no-product invalid-length');

			$modal.find('.modal__content-results').html('');
			$results.hide();
			$suggestion.show();
		});

		/**
		 * Private function for search
		 */
		function search($currentForm) {
			var $search = $currentForm.find('input.search-modal__field'),
				keyword = $search.val(),
				$results = $currentForm.closest('.modal__container').find('.modal__content-results'),
				$suggestion = $currentForm.closest('.modal__container').find('.modal__content-suggestion'),
				$currentContent = $currentForm.closest('.modal__container');

			if (keyword.trim().length < 2) {
				$currentContent.removeClass('searching found-products found-no-product').addClass('invalid-length');
				return;
			}

			$currentContent.removeClass('found-products found-no-product').addClass('searching');

			var keycat = keyword;

			if (keycat in searchCache) {
				var result = searchCache[keycat];

				$currentContent.removeClass('searching');
				$currentContent.addClass('found-products');
				$suggestion.hide();
				$results.html(result.products).show();


				$(document.body).trigger('ecomus_ajax_search_request_success', [$results]);

				$currentContent.removeClass('invalid-length');
				$currentContent.addClass('searched actived');
			} else {
				var data = {
						'term': keyword,
						'ajax_search_number': ecomusAjaxSearch.header_search_number,
					},
					ajax_url = ecomusAjaxSearch.ajax_url.toString().replace('%%endpoint%%', 'ecomus_instance_search_form');

				xhr = $.ajax({
					url: ajax_url,
					method: 'post',
					data: data,
					success: function (response) {
						var $products = response.data;
						$suggestion.hide();
						$currentContent.removeClass('searching');
						$currentContent.addClass('found-products');
						$results.html($products).show();
						$currentContent.removeClass('invalid-length');

						$(document.body).trigger('ecomus_ajax_search_request_success', [$results]);

						// Cache
						searchCache[keycat] = {
							found: true,
							products: $products
						};

						$currentContent.addClass('searched actived');

					}
				});
			}
		}
	}

	/**
	 * Document ready
	 */
	$(function () {
		ecomus.init();
	});

})(jQuery);