/**
 * @author dave.rodriguez
 */

//currentFilter def in dist master
//var currentFilter = null;

jQuery(document).ready(
	function() {
	    // Grab a list of all filterable lists
	    var filterables = jQuery('.filterable');

	    // Copy the "no results" found template to all filterable lists
	    // and then hide the "no results" unless they are needed
	    var noResultsTemplate = jQuery('#noResultsTemplate').html();
	    filterables.after("<div class='no-results'>" + noResultsTemplate + "</div>");
	    jQuery('#noResultsTemplate').remove(); 	// Don't need the original anymore, so get rid of it
	    checkForEmpties();

	    jQuery('input[@name=mapFilter]').click(
			function() {
			    var val = this.value;

			    if (val == 'all') {
			        currentFilter = null;
			        jQuery(".no-results").hide();
			        jQuery('li', filterables).show();
			    } else {
			        currentFilter = val;
			        jQuery('li', filterables).hide();
			        jQuery('li.' + val, filterables).show();

			        checkForEmpties();
			    }
			    
			    var anchor = jQuery('.country-selector').val();
			    if (anchor) {
			        tc(cleanUpAnchor(anchor));
			    }
			}
		);

	    /// This function checks each country listing to see if there are any
	    /// results visible. If not, it shows the "No Results" block.
	    function checkForEmpties() {
	        jQuery('.no-results').hide();

	        filterables.each(
				function() {
				    var numVisible = jQuery('li:visible', this).length;
				    if (numVisible <= 0) {
				        jQuery('.no-results', jQuery(this).parent()).show();
				    }
				}
			);
	    }
	}
);