# Source: modern-store-locator.js

    /**
     * @namespace WPGMZA
     * @module ModernStoreLocator
     * @requires WPGMZA
     */
    (function($) {

    	/**
    	 * The new modern look store locator. It takes the elements
    	 * from the default look and moves them into the map, wrapping
    	 * in a new element so we can apply new styles.
    	 * @return Object
    	 */
    	WPGMZA.ModernStoreLocator = function(map_id)
    	{
    		var self = this;
    		var original;

    		WPGMZA.assertInstanceOf(this, "ModernStoreLocator");

    		if(WPGMZA.isProVersion())
    			 original = $(".wpgmza_sl_search_button[mid='" + map_id + "']").closest(".wpgmza_sl_main_div");
    		else
    			original = $(".wpgmza_sl_search_button").closest(".wpgmza_sl_main_div");

    		if(!original.length)
    			return;

    		// Build / re-arrange elements
    		this.element = $("<div class='wpgmza-modern-store-locator'><div class='wpgmza-inner wpgmza-modern-hover-opaque'/></div>")[0];

    		var inner = $(this.element).find(".wpgmza-inner");

    		var titleSearch = $(original).find("[id='nameInput_" + map_id + "']");
    		if(titleSearch.length)
    		{
    			var placeholder = wpgmaps_localize[map_id].other_settings.store_locator_name_string;
    			if(placeholder && placeholder.length)
    				titleSearch.attr("placeholder", placeholder);
    			inner.append(titleSearch);
    		}

    		var addressInput;
    		if(WPGMZA.isProVersion())
    			addressInput = $(original).find(".addressInput");
    		else
    			addressInput = $(original).find("#addressInput");

    		if(wpgmaps_localize[map_id].other_settings.store_locator_query_string && wpgmaps_localize[map_id].other_settings.store_locator_query_string.length)
    			addressInput.attr("placeholder", wpgmaps_localize[map_id].other_settings.store_locator_query_string);

    		inner.append(addressInput);

    		inner.append($(original).find("select.wpgmza_sl_radius_select"));
    		// inner.append($(original).find(".wpgmza_filter_select_" + map_id));

    		// Buttons
    		this.searchButton = $(original).find( ".wpgmza_sl_search_button" );
    		inner.append(this.searchButton);

    		this.resetButton = $(original).find( ".wpgmza_sl_reset_button_div" );
    		inner.append(this.resetButton);

    		this.resetButton.hide();

    		if(WPGMZA.isProVersion())
    		{
    			this.searchButton.on("click", function(event) {
    				if($("addressInput_" + map_id).val() == 0)
    					return;

    				self.searchButton.hide();
    				self.resetButton.show();
    			});
    			this.resetButton.on("click", function(event) {
    				self.resetButton.hide();
    				self.searchButton.show();
    			});
    		}

    		// Distance type
    		inner.append($("#wpgmza_distance_type_" + map_id));

    		// Categories
    		var container = $(original).find(".wpgmza_cat_checkbox_holder");
    		var ul = $(container).children("ul");
    		var items = $(container).find("li");
    		var numCategories = 0;

    		//$(items).find("ul").remove();
    		//$(ul).append(items);

    		var icons = [];

    		items.each(function(index, el) {
    			var id = $(el).attr("class").match(/\d+/);

    			for(var category_id in wpgmza_category_data) {

    				if(id == category_id) {
    					var src = wpgmza_category_data[category_id].image;
    					var icon = $('<div class="wpgmza-chip-icon"/>');

    					icon.css({
    						"background-image": "url('" + src + "')",
    						"width": $("#wpgmza_cat_checkbox_" + category_id + " + label").height() + "px"
    					});
    					icons.push(icon);

                        if(src != null && src != ""){
    					   //$(el).find("label").prepend(icon);
                           $("#wpgmza_cat_checkbox_" + category_id + " + label").prepend(icon);
                        }

    					numCategories++;

    					break;
    				}

    			}
    		});

            $(this.element).append(container);

    		if(numCategories) {
    			this.optionsButton = $('<span class="wpgmza_store_locator_options_button"><i class="fas fa-list"></i></span>');
    			$(this.searchButton).before(this.optionsButton);
    		}

    		setInterval(function() {

    			icons.forEach(function(icon) {
    				var height = $(icon).height();
    				$(icon).css({"width": height + "px"});
    				$(icon).closest("label").css({"padding-left": height + 8 + "px"});
    			});

    			$(container).css("width", $(self.element).find(".wpgmza-inner").outerWidth() + "px");

    		}, 1000);

    		$(this.element).find(".wpgmza_store_locator_options_button").on("click", function(event) {

    			if(container.hasClass("wpgmza-open"))
    				container.removeClass("wpgmza-open");
    			else
    				container.addClass("wpgmza-open");

    		});

    		// Remove original element
    		$(original).remove();

    		// Event listeners
    		$(this.element).find("input, select").on("focus", function() {
    			$(inner).addClass("active");
    		});

    		$(this.element).find("input, select").on("blur", function() {
    			$(inner).removeClass("active");
    		});
    	}

    	WPGMZA.ModernStoreLocator.createInstance = function(map_id)
    	{
    		if(WPGMZA.settings.engine == "google-maps")
    			return new WPGMZA.GoogleModernStoreLocator(map_id);
    		else
    			return new WPGMZA.OLModernStoreLocator(map_id);
    	}

    })(jQuery);