<?php
// don't load directly
if ( ! defined( 'ABSPATH' ) ) {
	die( '-1' );
}


add_action('widgets_init', 'nfpfilter_widget_func');

function nfpfilter_widget_func(){
	register_widget('nfp_filter_plus');
}

class nfp_filter_plus extends WP_Widget {

	function nfp_filter_plus()
	{
		$widget_ops = array('classname' => 'filter_plus', 'description' => 'All in one widget for WooCommerce Filter Plus');
		$control_ops = array('id_base' => 'filter-plus');
		parent::__construct('filter-plus', '&nbsp;Filter Plus', $widget_ops, $control_ops);
		$this->exclude_empty_filter = true;
	}

	function widget($args, $instance)
	{
		if ( is_single() ) {return;} // should not run in single posts as it has nothing to do

		extract($args);
		$prefix = 'nfp';
		$title = apply_filters('widget_title', $instance['title']);
		$filter = $instance['filter'];
		$type = $instance['type'];
		$filter_name = $instance['filter_name'];
		$filter_options = $instance['filter_options'];
		$fold_options_after = isset($instance['fold_options_after']) ? $instance['fold_options_after'] : '';
		$price_ranges = $instance['price_ranges'];
		$hide_empty_options = $instance['hide_empty_options'];
		$cat_availability = $instance['cat_availability'];
		$additional_class = isset($instance['additional_class']) ? $instance['additional_class'] : "";

		$get = $_GET;

		global $wp_query;

		if(!isset($current_cat)){
			if(isset($get['nfpproduct_cat'])){
				$current_cat = $get['nfpproduct_cat'];
			}else if(isset($_POST['nfp-queried-cat'])){
				$current_cat = $_POST['nfp-queried-cat'];
			}else{
				$current_cat = '';
			}
		}

		if ( is_tax( 'product_cat' ) ) {
			$this->current_cat   = $wp_query->queried_object;
			$current_cat = $this->current_cat->slug;
		}
		if(!empty($cat_availability)){
			if(isset($current_cat) && !in_array($current_cat,$cat_availability)) {
				return;
			}
		}

		$enable_ajax_loading = get_option('nfp_ajax_loading') == true ? 'nfp-ajax-widget' : '';

		echo '<div class="woocommerce nfp-woocommerce-widget '.esc_attr($enable_ajax_loading).'" data-parent-sidebar-id="'.esc_attr($args['id']).'">';

		echo $before_widget;

		if($title) {
			echo $before_title.esc_html($title).$after_title;
		}

		echo '<div class="nfpfilter-inside-widget widget-'.esc_attr($this->id).'" data-id="widget-'.esc_attr($this->id).'">';
		// clear filter button
		if(isset($instance['clear_filter']) && $instance['clear_filter'] == 'true' && !empty($get[$prefix.$filter])){
			echo '<p style="margin-bottom: 10px"><a href="'.esc_attr(self::build_link_from_url($get , array($filter=>''),'or',false)).'"><span style="font-size: inherit; vertical-align: middle; height: auto;" class="dashicons dashicons-arrow-left-alt2"></span>'.esc_html__('Clear ','nfpfilter').esc_html($filter_name).'</a></p>';
		}elseif(((isset($get[$prefix.'min_price']) && !empty($get[$prefix.'min_price'])) || (isset($get[$prefix.'max_price'])) && !empty($get[$prefix.'max_price'])) && $instance['clear_filter'] == 'true' && $instance['filter'] == 'price'){
			echo '<p style="margin-bottom: 10px"><a href="'.esc_attr(self::build_link_from_url($get , array('min_price'=>'','max_price'=>''),'or',false)).'"><span style="font-size: inherit; vertical-align: middle; height: auto;" class="dashicons dashicons-arrow-left-alt2"></span>'.esc_html__('Clear ','nfpfilter').esc_attr($filter_name).'</a></p>';
		}elseif(isset($get[$prefix.'_s']) && $instance['clear_filter'] == 'true' && $instance['filter'] == 'search'){
			echo '<p style="margin-bottom: 10px"><a href="'.esc_attr(self::build_link_from_url($get , array('_s'=>''),'or',false)).'"><span style="font-size: inherit; vertical-align: middle; height: auto;" class="dashicons dashicons-arrow-left-alt2"></span>'.esc_html__('Clear ','nfpfilter').esc_attr($filter_name).'</a></p>';
		}

		$nargs=array(
			'object_type' => array('product')
		);

		$taxonmies = get_taxonomies($nargs);

		if($filter == 'product_cat' ){ // filter type product categories
			if(!empty($filter_options)){
				$all_ids = array();
				$product_cats = get_terms(array(
						'taxonomy' => 'product_cat',
						'hide_empty'=>false)
				);

				foreach($product_cats as $product_cat){
					if(is_object($product_cat)){$all_ids[] = $product_cat->term_id;}
				}

				$exclude_cats = $all_ids;

				foreach($filter_options as $option){

					$value = explode(':nfp:', $option);

					$term = get_term_by('slug',$value[0],'product_cat');
					$cat_id_key = array_search($term->term_id, $exclude_cats);
					unset($exclude_cats[$cat_id_key]);
				}
			}


			$count              = isset( $instance['cat_count'] ) ? $instance['cat_count'] : '';
			$hierarchical       = isset( $instance['cat_hierarchy'] ) ? $instance['cat_hierarchy'] : '';
			$show_children_only = isset( $instance['cat_show_child'] ) ? $instance['cat_show_child'] : '';
			$dropdown           = isset( $instance['dropdown'] ) ? $instance['dropdown'] : '';
			$orderby            = isset( $instance['orderby'] ) ? $instance['orderby'] : '';
			$hide_empty         = isset( $instance['cat_hide_empty'] ) ? $instance['cat_hide_empty'] : '';
			$dropdown_args      = array( 'hide_empty' => $hide_empty );
			$list_args          = array( 'show_count' => $count, 'hierarchical' => $hierarchical, 'taxonomy' => 'product_cat', 'hide_empty' => $hide_empty );

			// Menu Order
			$list_args['menu_order'] = false;
			if ( $orderby == 'order' ) {
				$list_args['menu_order'] = 'asc';
			} else {
				$list_args['orderby']    = 'title';
			}

			// Setup Current Category

			$this->current_cat = isset($get[$prefix.'product_cat']) ? get_term_by('slug',$get[$prefix.'product_cat'],'product_cat') : false;
			$this->cat_ancestors = is_object($this->current_cat) ? get_ancestors( $this->current_cat->term_id, 'product_cat' ) : array();


			if ( is_tax( 'product_cat' ) ) {
				$this->current_cat   = $wp_query->queried_object;
				$this->cat_ancestors = get_ancestors( $this->current_cat->term_id, 'product_cat' );
			} elseif ( is_singular( 'product' ) ) {

			}


			// Show Siblings and Children Only
			if ( $show_children_only && $this->current_cat ) {

				// Top level is needed
				$top_level = get_terms(
					'product_cat',
					array(
						'fields'       => 'ids',
						'parent'       => 0,
						'hierarchical' => true,
						'hide_empty'   => false
					)
				);

				// Direct children are wanted
				$direct_children = get_terms(
					'product_cat',
					array(
						'fields'       => 'ids',
						'parent'       => $this->current_cat->term_id,
						'hierarchical' => true,
						'hide_empty'   => false
					)
				);

				// Gather siblings of ancestors
				$siblings  = array();
				if ( $this->cat_ancestors ) {
					foreach ( $this->cat_ancestors as $ancestor ) {
						$ancestor_siblings = get_terms(
							'product_cat',
							array(
								'fields'       => 'ids',
								'parent'       => $ancestor,
								'hierarchical' => false,
								'hide_empty'   => false
							)
						);
						$siblings = array_merge( $siblings, $ancestor_siblings );
					}
				}

				if ( $hierarchical ) {
					$include = array_merge( $top_level, $this->cat_ancestors, $siblings, $direct_children, array( $this->current_cat->term_id ) );
				} else {
					$include = array_merge( $direct_children );
				}

				if(isset($exclude_cats) && $exclude_cats && is_array($exclude_cats)){
					foreach($exclude_cats as $exclude_cat_key => $exclude_cat_value){
						if(in_array($exclude_cat_value,$include)){
							unset($include[array_search($exclude_cat_value,$include)]);
						}
					}
				}

				$dropdown_args['include'] = implode( ',', $include );
				$list_args['include']     = implode( ',', $include );

				if ( empty( $include ) ) {
					return;
				}

			} elseif ( $show_children_only ) {
				$dropdown_args['depth']        = 1;
				$dropdown_args['child_of']     = 0;
				$dropdown_args['hierarchical'] = 1;
				$list_args['depth']            = 1;
				$list_args['child_of']         = 0;
				$list_args['hierarchical']     = 1;
			}

			// Dropdown
			if ( $dropdown ) {
				$dropdown_defaults = array(
					'show_count'         => $count,
					'hierarchical'       => $hierarchical,
					'show_uncategorized' => 0,
					'orderby'            => $orderby,
					'selected'           => $this->current_cat ? $this->current_cat->slug : ''
				);
				$dropdown_args = wp_parse_args( $dropdown_args, $dropdown_defaults );

				// Stuck with this until a fix for https://core.trac.wordpress.org/ticket/13258
				wc_product_dropdown_categories( apply_filters( 'woocommerce_product_categories_widget_dropdown_args', $dropdown_args ) );

				wc_enqueue_js( "
				jQuery( '.dropdown_product_cat' ).change( function() {
					if ( jQuery(this).val() != '' ) {
						var this_page = '';
						var home_url  = '" . esc_js( home_url( '/' ) ) . "';
						if ( home_url.indexOf( '?' ) > 0 ) {
							this_page = home_url + '&product_cat=' + jQuery(this).val();
						} else {
							this_page = home_url + '?product_cat=' + jQuery(this).val();
						}
						location.href = this_page;
					}
				});
			" );

				// List
			} else {



				if(is_tax('product_cat')){
					include_once( NFP_DIR.'/walkers/class-product-cat-list-walker.php' );
					$list_args['walker']                     = new WC_Product_Cat_List_Walker;
				}else{
					include_once( NFP_DIR.'/walkers/nfpfilter-class-product-cat-list-walker.php' );
					$list_args['walker']                     = new NFP_WC_Product_Cat_List_Walker;
				}

				$list_args['title_li']                   = '';
				$list_args['pad_counts']                 = 1;
				$list_args['show_option_none']           = esc_html__('No product categories exist.', 'woocommerce' );
				$list_args['current_category']           = $this->current_cat;
				$list_args['current_category_ancestors'] = $this->cat_ancestors;

				if(isset($exclude_cats) && !empty($exclude_cats)) $list_args['exclude'] = $exclude_cats;

				echo '<ul class="product-categories">';
				wp_list_categories(apply_filters( 'woocommerce_product_categories_widget_args', $list_args ));

				echo '</ul>';
			}



		}elseif($filter == 'price'){ // filter type price ranges
			$price_ranges = explode(',',$price_ranges);

			echo '<ul class="unstyled">';

			if(isset($get[$prefix.'min_price']) || isset($get[$prefix.'max_price'])){
				$get[$prefix.'min_price'] = isset($get[$prefix.'min_price']) ? $get[$prefix.'min_price']:'';
				$get[$prefix.'max_price'] = isset($get[$prefix.'max_price']) ? $get[$prefix.'max_price']:'';
				echo '<li><input style="width: 50px; display: inline;" type="text" class="nfp_min_price nfp_price_input" value="'.esc_attr($get[$prefix.'min_price']).'">'.esc_html__(' to ','nfpfilter').'<input style="width: 50px; display: inline;" type="text" class="nfp_max_price nfp_price_input" value="'.esc_attr($get[$prefix.'max_price']).'"> <a href="'.esc_attr(self::build_link_from_url($get , array('min_price'=>$get[$prefix.'min_price'],'max_price'=> $get[$prefix.'max_price']),'or', true, $cat_availability)).'" class="nfp-filter-btn nfp-price-go">'.esc_html__(' Go ','nfpfilter').'</a></li>';

			}else{
				$args = array();
				foreach ($price_ranges as $price_range){
					if(strpos($price_range,'-')>0){
						$range = explode('-',$price_range);
						$args =  array('min_price'=>$range[0],'max_price'=>$range[1]);
						echo '<li><a href="'.esc_attr(self::build_link_from_url($get , $args,'or', true, $cat_availability)).'">'.wc_price($range[0]).' - '.wc_price($range[1]).'</a></li>';
					}elseif(strpos($price_range,'+')>0){
						$range = explode('+',$price_range);
						if($range[0] > 0){
							$args =  array('min_price'=>$range[0]);
							echo '<li><a href="'.esc_attr(self::build_link_from_url($get , $args,'or', true, $cat_availability)).'">'.wc_price($range[0]).esc_html__(' &amp; above','nfpfilter').'</a></li>';
						}
					}
					$args = null;

				}
				echo '<li><input style="width: 50px;display: inline;" type="text" class="nfp_min_price nfp_price_input" value="">'.esc_html__(' to ','nfpfilter').'<input style="width: 50px;display: inline;" type="text" class="nfp_max_price nfp_price_input" value=""> <a  href="'.self::build_link_from_url($get , array(),'or', true, $cat_availability).'"  class="nfp-filter-btn nfp-price-go">'.esc_html__(' Go ','nfpfilter').'</a></li>';
			}
			echo '</ul>';

		}elseif($filter == 'min_discount'){ // filter type min discount
			echo '<ul class="unstyled nfp-discount">';
			$discounts = explode(',',str_replace(' ','',$instance['min_discount']));
			foreach ($discounts as $discount){
				$active = isset($get[$prefix.'min_discount']) && $get[$prefix.'min_discount'] == $discount ? 'active' : '';
				echo '<li><a class="'.esc_attr($active).'"  href="'.esc_attr(self::build_link_from_url($get , array('min_discount'=>$discount),'or', true, $cat_availability)).'"> '.esc_html($discount).esc_html__('% off or more','nfpfilter').'</a></li>';
			}
			echo '</ul>';
		}
		elseif($filter == 'search') { // filter type search
			$val = isset($get['nfp_s']) ? stripslashes(urldecode($get['nfp_s'])) : "";
			echo '<input type="text" class="nfpfilter-search-input '.esc_attr($additional_class).'" value="'.$val.'">';
		}
		else{
			// all other filters
			if($hide_empty_options == true && is_array($filter_options)){ // simply remove all options that does not belong to any post
				$slugs = array();
				foreach ($filter_options as $option){
					$value = explode(':nfp:',$option);
					$slugs[] = $value[0];
				}
				$args = array(
					'taxonomy' => $filter,
					'hide_empty' => true,
					'slug' => $slugs
				);

				$new_filter_options = get_terms($args);

				$filter_options = array();
				foreach ($new_filter_options as $new_filter_option){
					$filter_options[] = $new_filter_option->slug.':nfp:'.$new_filter_option->name;
				}
			}


			if(in_array($filter,$taxonmies)){
				switch ($type){
					case 'list' :
						echo '<ul class="unstyled">';
						$counter = $counter2 = 0;
						$fold = false;
						foreach ($filter_options as $option){

							if($hide_empty_options){ // hide empty options
								$link = self::build_link_from_url($get , array($filter=>$value[0]), 'or', true, $cat_availability);
								$link_get = apply_filters('nfpfilter_string_to_array',$link);
								$link_get['paged'] = 1;
								$product_args = apply_filters('nfpfilter_get_args',$link_get);
								$posts_array = get_posts( $product_args );
								if(empty($posts_array)){$empty_option = true;}
							}
							if($empty_option === false){ // show only if posts exists
								$value = explode(':nfp:',$option);

								$option_lang = apply_filters('wpml_translate_single_string', $value[0], 'Widgets', 'Filter option ('.$value[1].')' );
								if($option_lang && $option_lang!= $value[1] && $option_lang!= $value[0]) $value[1] = $option_lang;

								$active = array_key_exists($prefix.$filter,$get) && $this->check_string_into_array($value[0],explode(',',$get[$prefix.$filter])) == true ? 'active' : '';

								if( !empty($fold_options_after) && $counter2 === intval($fold_options_after)){
									echo '<li class="popup-holder"><span class="nfpfilter-show-more-toggle"> '.esc_html__("Show more","nfpfilter").'</span><div class="nfpfilter-option-fold" style="display: none"><ul>';
								}

								echo '<li class="'.esc_attr($active).'" ><a href="'.esc_attr(self::build_link_from_url($get , array($filter=>$value[0]),'or', true, $cat_availability)).'">'.esc_html($value[1]).'</a></li>';

								$counter2 ++;
							}


							if(!empty($fold_options_after) && $counter+1 === sizeof($filter_options)){
								echo '</ul></div></li>';
							}
							$counter ++;
						}

						echo '</ul>';
						break;

					case 'radio' :
						echo '<ul class="unstyled nfp-input-box nfp-radio-input">';
						$counter = $counter2 = 0;
						$fold = false;
						if(is_array($filter_options)){
							foreach ($filter_options as $option){
								$empty_option = false;
								$value = explode(':nfp:',$option);

								$option_lang = apply_filters('wpml_translate_single_string', $value[0], 'Widgets', 'Filter option ('.$value[1].')' );
								if($option_lang && $option_lang!= $value[1] && $option_lang!= $value[0]) $value[1] = $option_lang;

								if($hide_empty_options){ // hide empty options
									$link = self::build_link_from_url($get , array($filter=>$value[0]), 'or', true, $cat_availability);
									$link_get = apply_filters('nfpfilter_string_to_array',$link);
									$link_get['paged'] = 1;
									$product_args = apply_filters('nfpfilter_get_args',$link_get);
									$posts_array = get_posts( $product_args );
									if(empty($posts_array)){$empty_option = true;}
								}
								if($empty_option === false){
									$value = explode(':nfp:',$option);
									$active = array_key_exists($prefix.$filter,$get) && strpos($get[$prefix.$filter],$value[0]) > -1 ? 'active' : '';
									$checked = array_key_exists($prefix.$filter,$get) && $this->check_string_into_array($value[0],explode(',',$get[$prefix.$filter])) == true ? 'checked="checked"' : '';

									if( !empty($fold_options_after) && $counter2 === intval($fold_options_after)){
										echo '<li class="popup-holder"><span class="nfpfilter-show-more-toggle"> '.esc_html__("Show more","nfpfilter").'</span><div class="nfpfilter-option-fold" style="display: none"><ul>';
									}
									echo '<li class="'.esc_attr($active).'"><a href="'.esc_attr(self::build_link_from_url($get , array($filter=>$value[0]),'or', true, $cat_availability)).'"><input '.$checked.' type="radio" name="'.esc_attr($filter).'"><span class="input-span"> '.esc_html($value[1]).'</span></a></li>';
									$counter2 ++;
								}

								if(!empty($fold_options_after) && $counter+1 === sizeof($filter_options)){
									echo '</ul></div></li>';
								}
								$counter ++;
							}
						}


						echo '</ul>';
						break;

					case 'checkbox' :

						echo '<ul class="unstyled nfp-input-box nfp-checkbox-input">';
						if(is_array($filter_options)){
							$counter = $counter2 = 0;
							$fold = false;
							foreach ($filter_options as $option){

								$value = explode(':nfp:',$option);

								$option_lang = apply_filters('wpml_translate_single_string', $value[0], 'Widgets', 'Filter option ('.$value[1].')' );
								if($option_lang && $option_lang!= $value[1] && $option_lang!= $value[0]) $value[1] = $option_lang;

								$empty_option = false;
								if($hide_empty_options){ // hide empty options
									$link = self::build_link_from_url($get , array($filter=>$value[0]), 'or', true, $cat_availability);
									$link_get = apply_filters('nfpfilter_string_to_array',$link);
									$link_get['paged'] = 1;
									$product_args = apply_filters('nfpfilter_get_args',$link_get);
									$posts_array = get_posts( $product_args );
									if(empty($posts_array)){$empty_option = true;}
								}

								if($empty_option === false){
									$active = array_key_exists($prefix.$filter,$get) && $this->check_string_into_array($value[0],explode(',',$get[$prefix.$filter])) == true ? 'class="active"' : '';
									$checked = array_key_exists($prefix.$filter,$get) && $this->check_string_into_array($value[0],explode(',',$get[$prefix.$filter])) == true ? 'checked="checked"' : '';

									if( !empty($fold_options_after) && $counter2 == intval($fold_options_after)){
										$fold = true;
										echo '<li class="popup-holder"><span class="nfpfilter-show-more-toggle"> '.esc_html__("Show more","nfpfilter").'</span><div class="nfpfilter-option-fold" style="display: none"><ul>';
									}

									echo '<li '.$active.'><a href="'.esc_attr(self::build_link_from_url($get , array($filter=>$value[0]), 'and', true, $cat_availability)).'"><input '.$checked.' type="checkbox" name="'.esc_attr($filter).'"><span class="input-span"> '.esc_html($value[1]).'</span></a></li>';

									$counter2 ++;
								}
								if(!empty($fold_options_after) && $counter+1 == sizeof($filter_options) && $fold === true){
									echo '</ul></div></li>';
								}
								$counter ++;
							}
						}

						echo '</ul>';

						break;

					case 'bordered_inline' :
						echo '<ul class="unstyled nfp-bordered-inline">';
						if(is_array($filter_options)){
							$counter = $counter2 = 0;
							$fold = false;
							foreach ($filter_options as $option){
								$empty_option = false;
								$value = explode(':nfp:',$option);
								$option_lang = apply_filters('wpml_translate_single_string', $value[0], 'Widgets', 'Filter option ('.$value[1].')' );
								if($option_lang && $option_lang!= $value[1] && $option_lang!= $value[0]) $value[1] = $option_lang;
								if($hide_empty_options){ // hide empty options
									$link = self::build_link_from_url($get , array($filter=>$value[0]), 'or', true, $cat_availability);
									$link_get = apply_filters('nfpfilter_string_to_array',$link); // extract array from link
									$product_args = apply_filters('nfpfilter_get_args',$link_get);
									$posts_array = get_posts( $product_args );
									if(empty($posts_array)){$empty_option = true;}
								}

								if($empty_option === false){
									$active = array_key_exists($prefix.$filter,$get) && $this->check_string_into_array($value[0],explode(',',$get[$prefix.$filter])) == true ? 'class="active"' : '';
									$checked = array_key_exists($prefix.$filter,$get) && $this->check_string_into_array($value[0],explode(',',$get[$prefix.$filter])) == true ? 'checked="checked"' : '';

									if( !empty($fold_options_after) && $counter2 == intval($fold_options_after)){
										$fold = true;
										echo '<li class="popup-holder"><span class="nfpfilter-show-more-toggle"> '.esc_html__("Show more","nfpfilter").'</span><div class="nfpfilter-option-fold" style="display: none"><ul>';
									}

									echo '<li '.$active.'><a href="'.esc_attr(self::build_link_from_url($get , array($filter=>$value[0]), 'and', true, $cat_availability)).'"><span class="input-span"> '.esc_html($value[1]).'</span></a></li>';

									$counter2 ++;
								}
								if(!empty($fold_options_after) && $counter+1 == sizeof($filter_options) && $fold === true){
									echo '</ul></div></li>';
								}
								$counter ++;
							}
						}

						echo '</ul><div style="clear:both;"></div>';

						break;

					case 'color':

						$terms = get_terms( array(
							'taxonomy' => $instance['filter'],
							'hide_empty' => false,
						) );

						$option_type = 'and';


							echo '<div class="unstyled">';
							foreach ($filter_options as $option) {
								$value = explode(':nfp:',$option);
								$option_lang = apply_filters('wpml_translate_single_string', $value[0], 'Widgets', 'Filter option ('.$value[1].')' );
								if($option_lang && $option_lang!= $value[1] && $option_lang!= $value[0]) $value[1] = $option_lang;

								$empty_option = false;
								if($hide_empty_options){ // hide empty options


									$link = self::build_link_from_url($get , array($filter=>$value[0]), 'or', true, $cat_availability);
									$link_get = apply_filters('nfpfilter_string_to_array',$link);
									$product_args = apply_filters('nfpfilter_get_args',$link_get);

									$posts_array = get_posts( $product_args );

									if(empty($posts_array)){$empty_option = true;}
								}

								if($empty_option === false){
									$value = explode(':nfp:', $option);
									$option_lang = apply_filters('wpml_translate_single_string', $value[0], 'Widgets', 'Filter option ('.$value[1].')' );
									if($option_lang && $option_lang!= $value[1] && $option_lang!= $value[0]) $value[1] = $option_lang;
									$term = get_term_by('slug',$value[0], $filter);

									$color = get_term_meta($term->term_id,'pa_color_swatches_id_color',true); // should get the color from woocommerce color and swatch
									if(empty($color)){
										$color = get_term_meta($term->term_id,'color','true'); // compatible with variation swatch and color
									}

									if(!empty($color)){
										$active = array_key_exists($prefix.$filter, $get) && strpos($get[$prefix.$filter], $value[0]) > -1 ? 'active' : '';
										$active_icon = array_key_exists($prefix.$filter, $get) && strpos($get[$prefix.$filter], $value[0]) > -1 ? 'active' : '';
										$checked = array_key_exists($prefix.$filter, $get) && strpos($get[$prefix.$filter], $value[0]) > -1 ? 'checked' : '';
										echo '<span class="nfp-color '.esc_attr($active).'"><a title="'.esc_attr($value[1]).'" href="' . esc_attr(self::build_link_from_url($get , array($filter=>$value[0]), $option_type, true, $cat_availability)) . '"><input ' . esc_attr($checked) . ' type="hidden" name="' . esc_attr($filter) . '"> <span style="background-color: '.esc_attr($color).';"></span></a></span>';
									}
								}

							}
							echo '</div>';

						break;

					case 'image':

						$terms = get_terms( array(
							'taxonomy' => $instance['filter'],
							'hide_empty' => false,
						) );

						$option_type = 'and';

						echo '<div class="unstyled">';
						foreach ($filter_options as $option) {
							$value = explode(':nfp:', $option);
							$option_lang = apply_filters('wpml_translate_single_string', $value[0], 'Widgets', 'Filter option ('.$value[1].')' );
							if($option_lang && $option_lang!= $value[1] && $option_lang!= $value[0]) $value[1] = $option_lang;
							$term = get_term_by('slug',$value[0], $filter);

							$image = get_term_meta($term->term_id,'pa_color_swatches_id_photo',true); // should get the photo from woocommerce color and swatch

							if(empty($image)){
								$image = get_term_meta($term->term_id,'image','true'); // compatible with variation swatch and color
							}

							if(!empty($image)){
								$active = array_key_exists($prefix.$filter, $get) && strpos($get[$prefix.$filter], $value[0]) > -1 ? 'active' : '';
								$active_icon = array_key_exists($prefix.$filter, $get) && strpos($get[$prefix.$filter], $value[0]) > -1 ? 'active' : '';
								$checked = array_key_exists($prefix.$filter, $get) && strpos($get[$prefix.$filter], $value[0]) > -1 ? 'checked' : '';
								$img_src = wp_get_attachment_image_src($image,'thumbnail');
								echo '<span class="nfp-color '.esc_attr($active).'"><a title="'.esc_attr($value[1]).'" href="' . esc_attr(self::build_link_from_url($get , array($filter=>$value[0]), $option_type, true, $cat_availability)) . '"><input ' . esc_attr($checked) . ' type="hidden" name="' . esc_attr($filter) . '"> <span><img src="'.esc_attr($img_src[0]).'" alt=""></span></a></span>';
							}

						}
						echo '</div>';


						break;

					case 'label':

						$terms = get_terms( array(
							'taxonomy' => $instance['filter'],
							'hide_empty' => false,
						) );

						$option_type = 'and';

						echo '<div class="unstyled">';
						foreach ($filter_options as $option) {
							$value = explode(':nfp:', $option);
							$option_lang = apply_filters('wpml_translate_single_string', $value[0], 'Widgets', 'Filter option ('.$value[1].')' );
							if($option_lang && $option_lang!= $value[1] && $option_lang!= $value[0]) $value[1] = $option_lang;
							$term = get_term_by('slug',$value[0], $filter);

							$label = get_term_meta($term->term_id,'label','true'); // compatible with variation swatch and color


							if(!empty($label)){
								$active = array_key_exists($prefix.$filter, $get) && strpos($get[$prefix.$filter], $value[0]) > -1 ? 'active' : '';
								$active_icon = array_key_exists($prefix.$filter, $get) && strpos($get[$prefix.$filter], $value[0]) > -1 ? 'active' : '';
								$checked = array_key_exists($prefix.$filter, $get) && strpos($get[$prefix.$filter], $value[0]) > -1 ? 'checked' : '';
								echo '<span class="nfp-color '.esc_attr($active).'"><a title="'.esc_attr($value[1]).'" href="' . esc_attr(self::build_link_from_url($get , array($filter=>$value[0]), $option_type, true, $cat_availability)) . '"><input ' . esc_attr($checked) . ' type="hidden" name="' . esc_attr($filter) . '"> <span>'.$label.'</span></a></span>';
							}

						}
						echo '</div>';


						break;
				}
			}elseif($filter == 'min_rating'){
				?>
				<ul class="nfp-review">
					<li><a <?php echo isset($get[$prefix.'min_rating']) && $get[$prefix.'min_rating'] == '4' ? 'class="selected"': ''; ?> href="<?php echo esc_attr(self::build_link_from_url($get , array('min_rating'=>4),'or', true, $cat_availability)); ?>"> <div class="star-rating" title="Rated 4 and up"><span style="width:80%"><?php _e("4 out of 5","nfpfilter"); ?></span></div> <?php _e("&amp; up","nfpfilter"); ?></a></li>
					<li><a <?php echo isset($get[$prefix.'min_rating']) && $get[$prefix.'min_rating'] == '3' ? 'class="selected"': ''; ?> href="<?php echo esc_attr(self::build_link_from_url($get , array('min_rating'=>3),'or', true, $cat_availability)); ?>"> <div class="star-rating" title="Rated 3 and up"><span style="width:60%"><?php _e("3 out of 5","nfpfilter"); ?></span></div> <?php _e("&amp; up","nfpfilter"); ?> </a></li>
					<li><a <?php echo isset($get[$prefix.'min_rating']) && $get[$prefix.'min_rating'] == '2' ? 'class="selected"': ''; ?> href="<?php echo self::build_link_from_url($get , array('min_rating'=>2),'or', true, $cat_availability) ?>"> <div class="star-rating" title="Rated 2 and up"><span style="width:40%"><?php _e("2 out of 5","nfpfilter"); ?></span></div> <?php _e("&amp; up","nfpfilter"); ?></a></li>
					<li><a <?php echo isset($get[$prefix.'min_rating']) && $get[$prefix.'min_rating'] == '1' ? 'class="selected"': ''; ?> href="<?php echo self::build_link_from_url($get , array('min_rating'=>1),'or', true, $cat_availability) ?>"> <div class="star-rating" title="Rated 1 and up"><span style="width:20%"><?php _e("1 out of 5","nfpfilter"); ?></span></div> <?php _e("&amp; up","nfpfilter"); ?></a></li>
				</ul>
				<div style="clear: both"></div>
				<?php
			}
		}
		echo '</div>';/*inside div*/
		echo $after_widget;

		echo '</div>';
	}



	public static function check_string_into_array($string,$array){
		$return = false;
		foreach($array as $key){
			if($string===$key){
				$return = true;
			}
		}
		return $return;
	}


	public static function build_link_from_url($get, $filters=null, $type='or', $add = true, $available_cats = null ){

		global $nfpfilter_initial_url;
		if(isset($nfpfilter_initial_url) && !empty($nfpfilter_initial_url)){$url = $nfpfilter_initial_url;}
		$url =  empty($url) ? "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}" : $url ;


		if(strpos($url,'?') > 0){ $url = substr($url,0,strpos($url,'?'));}

		$prefix = 'nfp';

		if(!is_array($filters))return;

		foreach($filters as $key => $value){

			if(is_tax('product_cat') && $key == 'product_cat'){
				return false;
			}

			if($add == false){ // remove item if add is false
				unset($get[$prefix.$key]);
			}
			elseif(!array_key_exists($prefix.$key,$get)){ //new key does not exist in url
				//$url .= '&'.$key.'='.$value;
				$get[$prefix.$key] = $value;
			}elseif($type=='and'){
				$new_val = array();
				$val_array = explode(',',$get[$prefix.$key]);
				$new_val = $val_array;
				if(self::check_string_into_array($value,$val_array)=== true){
					// remove available cats
					if(!empty($available_cats) && !empty($get[$prefix.$key])){
						foreach($available_cats as $available_cat){
							if(strpos($get[$prefix.$key],'nfppcat'.$available_cat) == true ){
								$available_cat_key = array_search('nfppcat'.$available_cat, $new_val);
								unset($new_val[$available_cat_key]);
							}
						}
					}

					$this_key = array_search($value, $new_val);

					unset($new_val[$this_key]);

					$this_key = null;
				}else{
					$new_val[] = $value;
				}

				$get[$prefix.$key]  = '';
				$counter = 1;

				foreach($new_val as $value){
					$get[$prefix.$key] .= sizeof($new_val)>$counter && !empty($new_val)  ? $value .',' : $value;
					$counter++;
				}

			}elseif($type=='or'){
				$get[$prefix.$key] = $value;
			}


			if(!empty($available_cats) && !empty($get[$prefix.$key])){

				foreach($available_cats as $available_cat){
					if(strpos($get[$prefix.$key],'nfppcat'.$available_cat) == false ){
						$get[$prefix.$key] .= ',nfppcat'.$available_cat;
					}
				}
			}
		}
		foreach($get as $key=>$value){
			if(is_null($value) || $value == '' || $key == '_')unset($get[$key]);
			if($key == 'paged' || $value == 'paged')unset($get[$key]);
		}
		
		

		$url = $url . "?" . http_build_query($get);
		
		
		if(strpos($url,'/page/') > 0){
			$url = explode('/page/',$url);
			$url = $url[0] . substr($url[1],1);
		}
		
		return $url;
	}


	function update($new_instance, $old_instance)
	{
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['filter'] = $new_instance['filter'];
		$instance['type'] = $new_instance['type'];
		$instance['filter_options'] = $new_instance['filter_options'];
		$instance['fold_options_after'] = $new_instance['fold_options_after'];
		$instance['filter_name'] = $new_instance['filter_name'];
		$instance['price_ranges'] = $new_instance['price_ranges'];
		$instance['min_discount'] = $new_instance['min_discount'];
		$instance['cat_count'] = $new_instance['cat_count'];
		$instance['cat_hierarchy'] = $new_instance['cat_hierarchy'];
		$instance['cat_show_child'] = $new_instance['cat_show_child'];
		$instance['hide_empty_options'] = isset($new_instance['hide_empty_options']) ? $new_instance['hide_empty_options'] : '' ;
		$instance['cat_hide_empty'] = $new_instance['cat_hide_empty'];
		$instance['clear_filter'] = $new_instance['clear_filter'];
		$instance['cat_availability'] = $new_instance['cat_availability'];
		$instance['additional_class'] = $new_instance['additional_class'];


		// register strings for wpml
		if(has_action('wpml_register_single_string') && $instance['filter'] != 'product_cat') {
			do_action( 'wpml_register_single_string', 'Widgets', 'Filter name', $instance['filter'] );
			foreach($instance['filter_options'] as $option){
				$filter_option_l = explode(':nfp:',$option);
				do_action( 'wpml_register_single_string', 'Widgets', 'Filter option (' . $filter_option_l[1].')', $filter_option_l[1] );
			}
		}


		return $instance;
	}

	function form($instance)
	{

		$defaults = array('title' => '', 'filter' => '','type' => '','filter_options' => '','fold_options_after' => '','clear_filter' => '','filter_name' => '','price_ranges' => '0-200,201-300,301-400,401+','min_discount' => '','cat_show_count' => '','cat_hierarchy' => '','cat_show_child' => '','cat_hide_empty' => '','hide_empty_options'=>'','cat_availability'=>'','additional_class' => '');
		$instance = wp_parse_args((array) $instance, $defaults);

		$args=array(
			'object_type' => array('product')
		);

		$taxonomies = get_taxonomies($args,'objects');
		$filters = array();

		wp_deregister_script('select2');
		wp_register_script('select2',NFP_URL.'admin/js/select2.js',false,false,true);
		wp_enqueue_script('select2');
		wp_enqueue_style('select2',NFP_URL.'admin/css/select2.min.css');
		?>

		<p>
			<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title:','nfpfilter'); ?></label>
			<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr($instance['title']); ?>" />
		</p>

		<p><?php /* product category : all filters */ ?>

			<label for="<?php echo esc_attr($this->get_field_id('filter')); ?>"><?php esc_html_e('Select filter','nfpfilter'); ?></label>
			<?php

			foreach($taxonomies as $key => $value){
				$filters[$key] = $value->labels->name;
			}
			$filters['min_rating'] = 'Rating';
			$filters['price'] = 'Price range';
			$filters['min_discount'] = 'Discount';
			$filters['search'] = 'Search';
			$filter_name = '';
			?>
			<select id="<?php echo esc_attr($this->get_field_id('filter')); ?>" name="<?php echo esc_attr($this->get_field_name('filter')); ?>" class="widefat nfpfilter-update-options nfpfilter-select-filter">
				<?php

				echo '<option>'.esc_html__('Select your filter','nfpfilter').'</option>';
				if(is_array($filters)){
					foreach($filters as $filter => $value){
						$selected = $filter == $instance['filter'] ? 'selected' : '';
						if($filter == $instance['filter'])$filter_name = $value;
						echo '<option '.esc_attr($selected).' value="'.esc_attr($filter).'">'.esc_attr($value).'</option>';
					}
				}
				?>
			</select>
			<input class="widefat" type="hidden" id="<?php echo esc_attr($this->get_field_id('filter_name')); ?>" name="<?php echo esc_attr($this->get_field_name('filter_name')); ?>" value="<?php echo esc_attr($filter_name); ?>" >
		</p>

		<?php if( array_key_exists($instance['filter'],$filters) && !in_array($instance['filter'],array('price','min_discount','min_rating','search'))): ?>
		<div class="nfpfilter-select-btn-group">
			<p>

				<?php

				/**
				 * filter options for taxonomies
				 */

				$terms = get_terms( array(
					'taxonomy' => $instance['filter'],
				) );

				?>
				<label for="<?php echo esc_attr($this->get_field_id('filter_options')); ?>"><?php esc_html_e('Select filter options','nfpfilter'); ?></label>

				<select id="<?php echo esc_attr($this->get_field_id('filter_options')); ?>" name="<?php echo esc_attr($this->get_field_name('filter_options')); ?>[]" class="widefat nfpfilter-select2 select2 filter-plus-options" multiple>
					<?php
					if(is_array($terms)){
						foreach($terms as $term){
							$selected = in_array($term->slug.':nfp:'.$term->name,$instance['filter_options']) ? 'selected' : '';
							echo '<option '.esc_attr($selected).' value="'.esc_attr($term->slug).':nfp:'.esc_attr($term->name).'">'.esc_attr($term->name).'</option>';
						}
					}
					?>
				</select>
			</p>
			<div role="group">
				<a href="#" class="nfp-select2-select-all button-secondary"><?php esc_html_e('Select all','nfpfilter'); ?></a> &nbsp;
				<a href="#" type="button" class="nfp-select2-select-none button-secondary"><?php esc_html_e('Clear','nfpfilter'); ?></a>
			</div>
		</div>

		<?php if( $instance['filter'] !== 'product_cat') : ?>
			<p>
				<?php

				/**
				 * filter options : more options for popup
				 */

				?>
				<label for="<?php echo esc_attr($this->get_field_id('fold_options_after')); ?>"><?php esc_html_e('Fold over ','nfpfilter'); ?></label>

				<input style="width: 50px" type="number" id="<?php echo esc_attr($this->get_field_id('fold_options_after')); ?>" name="<?php echo esc_attr($this->get_field_name('fold_options_after')); ?>" value="<?php echo $instance['fold_options_after']; ?>"> <?php esc_html_e("options.","nfpfilter"); ?>
				<br><em> <?php esc_html_e("You may fold options after specified number of options or keep it blank","nfpfilter"); ?></em>

			</p>

		<?php endif; ?>
	<?php endif; ?>


		<?php if( $instance['filter'] == 'product_cat') : ?>
		<p><?php /* product category : show count */ ?>
			<input class="widefat" type="checkbox" value="true" id="<?php echo esc_attr($this->get_field_id('cat_count')); ?>" name="<?php echo esc_attr($this->get_field_name('cat_count')); ?>" <?php echo $instance['cat_count'] == 'true' ? 'checked' : ''; ?>/>
			<label for="<?php echo esc_attr($this->get_field_id('cat_count')); ?>"><?php esc_html_e('Show product counts','nfpfilter'); ?></label>
		</p>

		<p><?php /* product category : show hierarchy */ ?>
			<input class="widefat" type="checkbox" value="true" id="<?php echo esc_attr($this->get_field_id('cat_hierarchy')); ?>" name="<?php echo esc_attr($this->get_field_name('cat_hierarchy')); ?>" <?php echo $instance['cat_hierarchy'] == 'true' ? 'checked' : ''; ?>/>
			<label for="<?php echo esc_attr($this->get_field_id('cat_hierarchy')); ?>"><?php esc_html_e('Show hierarchy','nfpfilter'); ?></label>
		</p>

		<p><?php /* product category : show child category */ ?>
			<input class="widefat" type="checkbox" value="true" id="<?php echo esc_attr($this->get_field_id('cat_show_child')); ?>" name="<?php echo esc_attr($this->get_field_name('cat_show_child')); ?>" <?php echo $instance['cat_show_child'] == 'true' ? 'checked' : ''; ?>/>
			<label for="<?php echo esc_attr($this->get_field_id('cat_show_child')); ?>"><?php esc_html_e('Show child category only','nfpfilter'); ?></label>
		</p>

		<p><?php /* product category : hide empty category */ ?>
			<input class="widefat" type="checkbox" value="true" id="<?php echo esc_attr($this->get_field_id('cat_hide_empty')); ?>" name="<?php echo esc_attr($this->get_field_name('cat_hide_empty')); ?>" <?php echo $instance['cat_hide_empty'] == 'true' ? 'checked' : ''; ?>/>
			<label for="<?php echo esc_attr($this->get_field_id('cat_hide_empty')); ?>"><?php esc_html_e('Hide empty category','nfpfilter'); ?></label>
		</p>

	<?php endif; ?>

		<?php if( array_key_exists($instance['filter'],$filters) && !in_array($instance['filter'],array('product_cat','price','min_discount','min_rating','search'))): ?>
		<?php
		/**
		 * filter type
		 * eg. radio, checkbox, button, price, color
		 */
		?>
		<p>

			<label for="<?php echo esc_attr($this->get_field_id('type')); ?>"><?php esc_html_e('Select filter type','nfpfilter'); ?></label>
			<select id="<?php echo esc_attr($this->get_field_id('type')); ?>" name="<?php echo esc_attr($this->get_field_name('type')); ?>" class="widefat">
				<option><?php esc_html_e('Select type','nfpfilter') ?></option>
				<option value="radio" <?php echo $instance['type']=='radio' ? 'selected' : ''; ?>><?php esc_html_e('Radio (single)','nfpfilter'); ?></option>
				<option value="checkbox" <?php echo $instance['type']=='checkbox' ? 'selected' : ''; ?>><?php esc_html_e('Checkbox (multiple)','nfpfilter') ?></option>
				<option value="bordered_inline" <?php echo $instance['type']== 'bordered_inline' ? 'selected' : ''; ?>><?php esc_html_e('Bordered inline (multiple)','nfpfilter') ?></option>
				<option value="color" <?php echo $instance['type']== 'color' ? 'selected' : ''; ?>><?php esc_html_e('Color swatch (multiple)','nfpfilter') ?></option>
				<option value="image" <?php echo $instance['type']== 'image' ? 'selected' : ''; ?>><?php esc_html_e('Image (multiple)','nfpfilter') ?></option>
				<option value="label" <?php echo $instance['type']== 'label' ? 'selected' : ''; ?>><?php esc_html_e('Label (multiple)','nfpfilter') ?></option>
			</select>
		</p>
	<?php endif; ?>



		<?php if( array_key_exists($instance['filter'],$filters) && !in_array($instance['filter'],array('product_cat','price','min_discount','min_rating','search'))) : ?>

		<p><?php /* hide empty filter options */ ?>
			<input class="widefat" type="checkbox" value="true" id="<?php echo esc_attr($this->get_field_id('hide_empty_options')); ?>" name="<?php echo esc_attr($this->get_field_name('hide_empty_options')); ?>" <?php echo $instance['hide_empty_options'] == 'true' ? 'checked' : ''; ?>/>
			<label for="<?php echo esc_attr($this->get_field_id('hide_empty_options')); ?>"><?php esc_html_e('Hide empty options','nfpfilter'); ?></label>
		</p>
	<?php endif; ?>
		<p><?php /* availability based on categories */
			if(!empty($instance['filter']) && $instance['filter'] !== 'product_cat'):
			$cats = get_terms('product_cat');
			?>
			<label for="<?php echo esc_attr($this->get_field_id('cat_availability')); ?>"><?php esc_html_e('Category dependency','nfpfilter'); ?></label>

			<select multiple="multiple" class="select2 nfpfilter-select2 widefat" type="checkbox" id="<?php echo esc_attr($this->get_field_id('cat_availability')); ?>" name="<?php echo esc_attr($this->get_field_name('cat_availability')); ?>[]">
				<?php
				foreach ($cats as $cat){
					$selected = in_array($cat->slug,$instance['cat_availability']) ? 'selected' : '';
					echo '<option value="'.esc_attr($cat->slug).'" '.esc_attr($selected).'>'.esc_html($cat->name).'</option>';
				}
				?>
			</select>
		</p>
	<?php endif; ?>
		<?php
		/**
		 * review option
		 *
		 */

		if($instance['filter'] === 'min_rating'): ?>

		<?php endif; ?>


		<?php

		/**
		 * price filter option for form
		 */


		if( $instance['filter']=='price'): ?>
			<p>
				<label for="<?php echo esc_attr($this->get_field_id('price_ranges')); ?>"><?php esc_html_e('Enter price ranges seperated by comma "," . Use + for more than an amount. eg. 0-200,201-300,301-400,401+','nfpfilter'); ?></label>
				<input type="text" id="<?php echo esc_attr($this->get_field_id('price_ranges')); ?>" name="<?php echo esc_attr($this->get_field_name('price_ranges')); ?>" class="widefat" value="<?php echo $instance['price_ranges']; ?>" />
			</p>

		<?php endif; ?>

		<?php

		/**
		 * Additional class
		 */


		if( array_key_exists($instance['filter'],$filters) && in_array($instance['filter'],array('search'))): ?>
			<p>
				<label for="<?php echo esc_attr($this->get_field_id('additional_class')); ?>"><?php esc_html_e('Additional class','nfpfilter'); ?></label>
				<input type="text" id="<?php echo esc_attr($this->get_field_id('additional_class')); ?>" name="<?php echo esc_attr($this->get_field_name('additional_class')); ?>" class="widefat" value="<?php echo esc_attr($instance['additional_class']); ?>">
			</p>
		<?php endif; ?>
		<?php

		/**
		 * discount option
		 */


		if( $instance['filter']=='min_discount'): ?>
			<p>
				<label for="<?php echo esc_attr($this->get_field_id('min_discount')); ?>"><?php esc_html_e('Enter discount ranges seperated by comma "," . eg. 10,20,30,40','nfpfilter'); ?></label>
				<input type="text" id="<?php echo esc_attr($this->get_field_id('min_discount')); ?>" name="<?php echo esc_attr($this->get_field_name('min_discount')); ?>" class="widefat" value="<?php echo esc_attr($instance['min_discount']); ?>">
			</p>

		<?php endif; ?>

		<?php if(!empty($instance['filter'])): ?>
		<p><?php /* clear option */ ?>
			<input class="widefat" type="checkbox" value="true" id="<?php echo esc_attr($this->get_field_id('clear_filter')); ?>" name="<?php echo esc_attr($this->get_field_name('clear_filter')); ?>" <?php echo $instance['clear_filter'] == 'true' ? 'checked' : ''; ?>/>
			<label for="<?php echo esc_attr($this->get_field_id('clear_filter')); ?>"><?php esc_html_e('Insert clear filter button','nfpfilter'); ?></label>
		</p>
	<?php endif; ?>

		<style>
			.widget-inside span.select2-container{
				min-width: 100%;
			}
		</style>
		<script type="text/javascript">
			jQuery(document).ready(function($){
				"use strict";
				var $nfpselect = $('select.select2').select2();

				$('.nfp-select2-select-all').click(function (e) {
					var $thisSelect = $(this).parents('.nfpfilter-select-btn-group:first').find('.select2').select2();
					var select_ids = [];
					$(this).parents('.nfpfilter-select-btn-group:first').find('.filter-plus-options option').each(function(index, element) {
						select_ids.push($(this).val());
					})
					$thisSelect.val(select_ids).trigger("change");
					e.preventDefault();
				});

				$('.nfp-select2-select-none').click(function (e) {
					var $thisSelect = $(this).parents('.nfpfilter-select-btn-group:first').find('.select2').select2();
					$thisSelect.val(null).trigger("change");
					e.preventDefault();
				});

				$('.nfpfilter-update-options').change(function(){
					$(this).parents('form:first').find('input[type="submit"]').trigger('click');
				});

			});
		</script>

		<?php
	}
}
