jQuery(document).ready(function($) {
	var today = new Date()
	var dd = String(today.getDate()).padStart(2, '0')
	var mm = String(today.getMonth() + 1).padStart(2, '0') // January is 0
	var yyyy = today.getFullYear()
	today = yyyy + '-' + mm + '-' + dd

	$('.bt-filter, .bt-closeFilter').click(function(e) {
		e.preventDefault()
		$('.breadcrumb__filter').toggleClass('open')
	})

	// SEARCH FILTERS
	if ($('.js-search-filters').length > 0) {
		var newCat = '',
			newSubCat = '',
			newTerm = '',
			newTaxonomy = '',
			newTaxQuery = {},
			hasCat = false,
			hasSubCat = false,
			newType = ['post', 'video'],
			filterType = '',
			searchName = $('.js-search-term'),
			originalSearchTerm = searchName.html(),
			newSearchTerm = originalSearchTerm

		// SEARCH INPUT
		$('.js-submit-search').submit(function() {
			newSearchTerm = $(this)
				.find('input')
				.val()
			current_page = 0
			data = {
				action: 'filter_results',
				query: posts,
				page: current_page
			}

			if (!newSearchTerm) newSearchTerm = originalSearchTerm

			var originalURL = window.location.href
			originalURL = originalURL.split('?s=')
			var newURL = originalURL[0] + '?s=' + newSearchTerm
			history.pushState(null, '', newURL)
			document.title = 'Search for "' + newSearchTerm + '" – Buzz'

			searchName.html(newSearchTerm)

			search_ajax()
			return false
		})

		$('.js-filter-select').change(function() {
			current_page = 0
			filterType = $(this).data('type')
			data = {
				action: 'filter_results',
				query: posts,
				page: current_page
			}

			if (filterType == 'cat' && $(this).val()) {
				hasCat = true
				newCat = $(this).val()

				if (hasSubCat == true) {
					newTaxQuery = {
						relation: 'AND',
						0: { taxonomy: 'category', field: 'slug', terms: newCat },
						1: { taxonomy: 'sub-category', field: 'slug', terms: newSubCat }
					}
					newTaxonomy = ''
					newTerm = ''
				} else {
					newTaxonomy = 'category'
					newTerm = newCat
					newTaxQuery = {}
				}
			} else if (filterType == 'cat' && !$(this).val()) {
				newCat = ''
				hasCat = false

				if (hasSubCat == true) {
					newTaxonomy = 'sub-category'
					newTerm = newSubCat
					newTaxQuery = {}
				} else {
					newTaxonomy = ''
					newTerm = ''
					newTaxQuery = {}
				}
			}

			if (filterType == 'subcat' && $(this).val()) {
				hasSubCat = true
				newSubCat = $(this).val()

				if (hasCat == true) {
					newTaxQuery = {
						relation: 'AND',
						0: { taxonomy: 'category', field: 'slug', terms: newCat },
						1: { taxonomy: 'sub-category', field: 'slug', terms: newSubCat }
					}
					newTaxonomy = ''
					newTerm = ''
				} else {
					newTaxonomy = 'sub-category'
					newTerm = newSubCat
					newTaxQuery = {}
				}
			} else if (filterType == 'subcat' && !$(this).val()) {
				newSubCat = ''
				hasSubCat = false

				if (hasCat == true) {
					newTaxonomy = 'category'
					newTerm = newCat
					newTaxQuery = {}
				} else {
					newTaxonomy = ''
					newTerm = ''
					newTaxQuery = {}
				}
			}

			if (filterType == 'type') {
				if ($(this).val()) {
					newType = $(this).val()
				} else {
					newType = ['post', 'video']
				}
			}

			search_ajax()
		})

		function search_ajax() {
			newQuery = JSON.parse(posts)
			newQuery.taxonomy = newTaxonomy
			newQuery.term = newTerm
			newQuery.tax_query = newTaxQuery
			newQuery.post_type = newType
			newQuery.s = newSearchTerm
			posts = JSON.stringify(newQuery)
			data.query = posts

			$.ajax({
				url: filter_results_params.ajaxurl,
				data: data,
				type: 'POST',
				beforeSend: function(xhr) {
					$('.js-posts-list').addClass('loading')
				},
				success: function(data) {
					$('.js-posts-list').removeClass('loading')
					if (data) {
						$('.js-posts-list').html(data)

						if (max_page > 1) {
							$('.js-loadmore').removeClass('hidden')
						} else {
							$('.js-loadmore').addClass('hidden')
						}
					} else {
						$('.js-posts-list').html('Nothing to show.')
					}
				}
			})
		}
	} // search filters

	// OFFERS FILTERS
	if ($('.js-offer-filters').length > 0) {
		var newCat = '',
			newLocation = '',
			newTerm = '',
			filterType = '',
			newOrder = '',
			newOrderby = '',
			newSearchTerm = '',
			newTaxonomy = '',
			newTaxQuery = {},
			hasCat = false,
			hasOrderby = false

		// SEARCH INPUT
		$('.js-submit-search').submit(function() {
			newSearchTerm = $(this)
				.find('input')
				.val()
			current_page = 0
			data = {
				action: 'filter_results',
				query: posts,
				page: current_page
			}

			offer_ajax()
			return false
		})

		var confirmed = getUrlParameter('c')

		if ($('body').hasClass('page-template-offers') && confirmed) {
			$('.selectCategory')
				.val(confirmed)
				.trigger('change')
			filterSelect($('.selectCategory'))
		}

		$('.js-filter-select').change(function() {
			$this = $(this)
			if ($('body').hasClass('page-template-offers')) {
				filterSelect($this)
			} else {
				catIntern = $(this).val()
				jsurl = $('.js-url').attr('href')
				goUrl = jsurl + '?c=' + catIntern
				console.log(goUrl)
				window.location.href = goUrl
			}
		})

		$('.btFilterOk').click(function(e) {
			e.preventDefault()
			offer_ajax()
		})
		$('.btReset').click(function(e) {
			e.preventDefault()
			hasCat = false
			hasOrderby = false
			newCat = ''
			newTaxonomy = ''
			newTerm = ''
			newTaxQuery = {}
			newOrderby = ''
			newOrder = ''

			$('.selectOrderby').prop('selectedIndex', 0)
			$('.selectCategory').prop('selectedIndex', 0)
		})

		function filterSelect($this) {
			current_page = 0
			filterType = $this.data('type')
			data = {
				action: 'filter_results',
				query: posts,
				page: current_page
			}
			if (filterType == 'cat' && $this.val()) {
				hasCat = true
				newCat = $this.val()
				newTaxonomy = 'category_offer'
				newTerm = newCat

				if (hasOrderby == true) {
					newTaxQuery = {
						relation: 'AND',
						0: { taxonomy: 'category_offer', field: 'slug', terms: newCat }
					}
					newOrderby = $('.selectOrderby').val()
					newOrder = $('.selectOrderby')
						.find(':selected')
						.data('order')
				} else {
					newTerm = newCat
					newTaxQuery = {}
				}
			} else if (filterType == 'cat' && !$this.val()) {
				newCat = ''
				hasCat = false
				newTerm = ''
				newTaxQuery = {}
				newTaxonomy = ''

				if (hasOrderby == true) {
					newOrderby = $('.selectOrderby').val()
					newOrder = $('.selectOrderby')
						.find(':selected')
						.data('order')
				}
			}

			if (filterType == 'orderby' && $this.val()) {
				hasOrderby = true
				newOrderby = $this.val()
				newOrder = $this.find(':selected').data('order')
				newTaxQuery = {}
				if (hasCat == true) {
					newCat = $('.selectCategory').val()
					newTaxonomy = 'category_offer'
					newTerm = newCat
					newTaxQuery = {
						relation: 'AND',
						0: { taxonomy: 'category_offer', field: 'slug', terms: newCat }
					}
				}
			} else if (filterType == 'orderby' && !$this.val()) {
				hasOrderby = false
				newTaxonomy = ''
				newTerm = ''
				newTaxQuery = {}
				if (hasCat == true) {
					newCat = $('.selectCategory').val()
					newTaxonomy = 'category_offer'
					newTerm = newCat
					newTaxQuery = {
						relation: 'AND',
						0: { taxonomy: 'category_offer', field: 'slug', terms: newCat }
					}
				}
			}

			if ($(window).width() >= 1024) {
				offer_ajax()
			}
		}

		function offer_ajax() {
			newQuery = JSON.parse(posts)
			newQuery.taxonomy = newTaxonomy
			newQuery.term = newTerm
			newQuery.s = newSearchTerm
			newQuery.orderby = newOrderby
			newQuery.order = newOrder
			// newQuery.tax_query = newTaxQuery
			// newQuery.meta_key = 'offer_expiration'
			// newQuery.meta_value = today
			// newQuery.meta_compare = '>='
			posts = JSON.stringify(newQuery)
			data.query = posts
			$.ajax({
				url: filter_results_params.ajaxurl,
				data: data,
				type: 'POST',
				beforeSend: function(xhr) {
					$('.listOffers').addClass('loading')
				},
				success: function(data) {
					$('.listOffers').removeClass('loading')
					if (data) {
						$('.listOffers').html(data)
						$('.breadcrumb__filter').removeClass('open')

						if (max_page > 1) {
							$('.js-loadmore').removeClass('hidden')
						} else {
							$('.js-loadmore').addClass('hidden')
						}
					} else {
						$('.listOffers').html('Nothing to show.')
					}
				}
			})
		}
	} // offers filters

	// JOB FILTERS
	if ($('.js-job-filters').length > 0) {
		var filterType = '',
			newMetaKey = '',
			newMetaValue = '',
			newCat = '',
			newOrder = '',
			newOrderby = '',
			searchTerm = '',
			metaQuery = {},
			metaQueryMetaKey = '',
			metaQueryMetaValue = '',
			hasIndustry = false,
			hasType = false

		// SEARCH INPUT
		$('.js-submit-search').submit(function() {
			searchTerm = $(this)
				.parent()
				.find('input')
				.val()
			current_page = 0
			data = {
				action: 'filter_results',
				query: posts,
				page: current_page
			}

			job_ajax()
			return false
		})

		// SELECTS
		$('.js-filter-select').change(function() {
			filterType = $(this).data('type')
			current_page = 0
			data = {
				action: 'filter_results',
				query: posts,
				page: current_page
			}

			//if location
			if (filterType == 'location' && $(this).val()) {
				newCat = $(this).val()
			} else if (filterType == 'location' && !$(this).val()) {
				newCat = ''
			}

			//if industry
			if (filterType == 'industry' && $(this).val()) {
				metaQueryMetaValueIndustry = $(this).val()
				newMetaValue = ''
				hasIndustry = true

				newMetaKey, (orderMetaKey = 'job_industry')
				metaQueryMetaKey = 'job_industry'

				if (hasType == true) {
					metaQuery = {
						relation: 'AND',
						query_type: {
							0: [
								{
									key: 'job_expiration',
									value: today,
									compare: '>='
								}
							],
							1: [
								{
									key: 'job_type',
									value: metaQueryMetaValue
								}
							],
							2: [
								{
									key: 'job_industry',
									value: metaQueryMetaValueIndustry
								}
							]
						}
					}

					newOrderby = 'query_order'
					newMetaKey = ''
					newMetaValue = ''
				} else if (hasType != true) {
					metaQuery = {
						relation: 'AND',
						query_type: {
							0: [
								{
									key: 'job_expiration',
									value: today,
									compare: '>='
								}
							],
							1: [
								{
									key: 'job_industry',
									value: metaQueryMetaValueIndustry
								}
							]
						}
					}

					newOrderby = 'query_order'
					newMetaKey = ''
					newMetaValue = ''
				}
			} else if (filterType == 'industry' && !$(this).val()) {
				hasIndustry = false

				if (hasType == true) {
					newOrder = 'DESC'
					newOrderby = 'date'

					metaQuery = {
						relation: 'AND',
						query_type: {
							0: [
								{
									key: 'job_expiration',
									value: today,
									compare: '>='
								}
							],
							1: [
								{
									key: 'job_type',
									value: metaQueryMetaValue
								}
							]
						}
					}
				} else if (hasType != true) {
					metaQuery = {
						query_type: {
							key: 'job_expiration',
							value: today,
							compare: '>='
						}
					}

					newOrder = 'DESC'
					newOrderby = 'date'
					newMetaKey = ''
					newMetaValue = ''
				}
			}

			// if type
			if (filterType == 'type' && $(this).val()) {
				hasType = true
				newMetaValue = $(this).val()
				metaQueryMetaValue = $(this).val()

				if (hasIndustry == true) {
					metaQuery = {
						relation: 'AND',
						query_type: {
							0: [
								{
									key: 'job_expiration',
									value: today,
									compare: '>='
								}
							],
							1: [
								{
									key: 'job_type',
									value: metaQueryMetaValue
								}
							],
							2: [
								{
									key: 'job_industry',
									value: metaQueryMetaValueIndustry
								}
							]
						},
						query_order: { key: metaQueryMetaKey }
					}

					newOrderby = 'query_order'
					newMetaKey = ''
					newMetaValue = ''
				} else if (hasIndustry != true) {
					newOrder = 'DESC'
					newOrderby = 'date'

					metaQuery = {
						relation: 'AND',
						query_type: {
							0: [
								{
									key: 'job_expiration',
									value: today,
									compare: '>='
								}
							],
							1: [
								{
									key: 'job_type',
									value: metaQueryMetaValue
								}
							]
						}
					}
				}
			} else if (filterType == 'type' && !$(this).val()) {
				hasType = false

				if (hasIndustry == true) {
					newMetaValue = ''
					newMetaKey = orderMetaKey
					newOrderby = 'meta_value'
					metaQuery = {
						query_type: {
							0: [
								{
									key: 'job_expiration',
									value: today,
									compare: '>='
								}
							],
							1: [
								{
									key: 'job_industry',
									value: metaQueryMetaValueIndustry
								}
							]
						}
					}
				} else if (hasIndustry != true) {
					metaQuery = {
						query_type: {
							key: 'job_expiration',
							value: today,
							compare: '>='
						}
					}

					newOrder = 'DESC'
					newOrderby = 'date'
					newMetaKey = ''
					newMetaValue = ''
				}
			}

			job_ajax()
		})

		function job_ajax() {
			newOrderby = 'query_order'

			newQuery = JSON.parse(posts)
			newQuery.taxonomy = 'location'
			newQuery.term = newCat
			newQuery.orderby = newOrderby
			newQuery.order = newOrder

			/*newQuery.meta_key = newMetaKey
			newQuery.meta_value = newMetaValue*/

			newQuery.meta_query = metaQuery
			newQuery.s = searchTerm
			posts = JSON.stringify(newQuery)
			data.query = posts

			$.ajax({
				url: filter_results_params.ajaxurl,
				data: data,
				type: 'POST',
				beforeSend: function(xhr) {
					$('.js-posts-list').addClass('loading')
				},
				success: function(data) {
					$('.js-posts-list').removeClass('loading')
					if (data) {
						$('.js-posts-list').html(data)

						if (max_page > 1) {
							$('.js-loadmore').removeClass('hidden')
						} else {
							$('.js-loadmore').addClass('hidden')
						}
					} else {
						$('.js-posts-list').html('Nothing to show.')
					}
				}
			})
		}
	} // job filters

	// OUT FILTERS
	if ($('.js-event-filters').length > 0) {
		var newCat = '',
			newLocation = '',
			newTerm = '',
			filterType = '',
			newOrder = '',
			newOrderby = '',
			newSearchTerm = '',
			metaQuery = {},
			newTaxQuery = {},
			metaQueryMetaKey = '',
			metaQueryMetaValue = '',
			newStartDate = '',
			newEndDate = ''

		window.runDateRange = function(startDate, endDate) {
			current_page = 0
			data = {
				action: 'filter_results',
				query: posts,
				page: current_page
			}
			newStartDate = startDate
			newEndDate = endDate

			metaQuery = { query_type: { key: 'event_date', value: { newStartDate, newEndDate }, compare: 'BETWEEN', type: 'DATE' } }

			event_ajax()
		}

		window.clearDateRange = function() {
			current_page = 0
			data = {
				action: 'filter_results',
				query: posts,
				page: current_page
			}
			metaQuery = {}

			event_ajax()
		}

		// SEARCH INPUT
		$('.js-submit-search').submit(function() {
			newSearchTerm = $(this)
				.find('input')
				.val()
			current_page = 0
			data = {
				action: 'filter_results',
				query: posts,
				page: current_page
			}

			event_ajax()
			return false
		})

		$('.js-filter-select').change(function() {
			current_page = 0
			filterType = $(this).data('type')
			data = {
				action: 'filter_results',
				query: posts,
				page: current_page
			}

			if (filterType == 'cat' && $(this).val()) {
				hasCat = true
				newCat = $(this).val()

				if (hasOrderby == true) {
					newTaxQuery = {
						relation: 'AND',
						0: { taxonomy: 'event-category', field: 'slug', terms: newCat },
						1: { taxonomy: 'location', field: 'slug', terms: newLocation }
					}
					newTaxonomy = ''
					newTerm = ''
				} else {
					newTaxonomy = 'event-category'
					newTerm = newCat
					newTaxQuery = {}
				}
			} else if (filterType == 'cat' && !$(this).val()) {
				newCat = ''
				hasCat = false

				if (hasOrderby == true) {
					newTaxonomy = 'location'
					newTerm = newLocation
					newTaxQuery = {}
				} else {
					newTaxonomy = ''
					newTerm = ''
					newTaxQuery = {}
				}
			}

			if (filterType == 'location' && $(this).val()) {
				hasOrderby = true
				newLocation = $(this).val()

				if (hasCat == true) {
					newTaxQuery = {
						relation: 'AND',
						0: { taxonomy: 'event-category', field: 'slug', terms: newCat },
						1: { taxonomy: 'location', field: 'slug', terms: newLocation }
					}
					newTaxonomy = ''
					newTerm = ''
				} else {
					newTaxonomy = 'location'
					newTerm = newLocation
					newTaxQuery = {}
				}
			} else if (filterType == 'location' && !$(this).val()) {
				newLocation = ''
				hasOrderby = false

				if (hasCat == true) {
					newTaxonomy = 'event-category'
					newTerm = newCat
					newTaxQuery = {}
				} else {
					newTaxonomy = ''
					newTerm = ''
					newTaxQuery = {}
				}
			}

			event_ajax()
		})

		function event_ajax() {
			newMetaKey = 'event_date'
			newOrderby = 'meta_value'
			newOrder = 'ASC'

			newQuery = JSON.parse(posts)
			newQuery.taxonomy = newTaxonomy
			newQuery.term = newTerm
			newQuery.s = newSearchTerm
			newQuery.orderby = newOrderby
			newQuery.order = newOrder
			newQuery.meta_key = newMetaKey
			newQuery.meta_query = metaQuery
			newQuery.tax_query = newTaxQuery
			posts = JSON.stringify(newQuery)
			data.query = posts

			$.ajax({
				url: filter_results_params.ajaxurl,
				data: data,
				type: 'POST',
				beforeSend: function(xhr) {
					$('.js-posts-list').addClass('loading')
				},
				success: function(data) {
					$('.js-posts-list').removeClass('loading')
					if (data) {
						$('.js-posts-list').html(data)

						if (max_page > 1) {
							$('.js-loadmore').removeClass('hidden')
						} else {
							$('.js-loadmore').addClass('hidden')
						}
					} else {
						$('.js-posts-list').html('Nothing to show.')
					}
				}
			})
		}
	} // outs filters
})
var getUrlParameter = function getUrlParameter(sParam) {
	var sPageURL = window.location.search.substring(1),
		sURLVariables = sPageURL.split('&'),
		sParameterName,
		i

	for (i = 0; i < sURLVariables.length; i++) {
		sParameterName = sURLVariables[i].split('=')

		if (sParameterName[0] === sParam) {
			return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1])
		}
	}
}
