// Alternative way of appending the select option
//$("#filter1Select").append(new Option(item, index));

function createFilterSelectionValues() {
	$.each(filter1Array, function(index, value) {
		$("<option/>").val(index).text(value).appendTo("#filter1Select");
	});
	$.each(filter2Array, function(index, value) {
		$("<option/>").val(index).text(value).appendTo("#filter2Select");
	});
	$.each(filter3Array, function(index, value) {
		$("<option/>").val(index).text(value).appendTo("#filter3Select");
	});

}

$(document).ready(function() {

	$("#filter1Select").change(function() {
		filterCheck();
	});

	$("#filter2Select").change(function() {
		filterCheck();
	});

	$("#filter3Select").change(function() {
		filterCheck();
	});

});

function filterCheck() {
	// Defines an infowindow object.
	var infowindow = new google.maps.InfoWindow({content:''});

    // Define an bounds object. When looping through the "Geo Loaction" children, the bounds object will bw extended (if necessary), to encompass the markers geo location.
    var bounds = new google.maps.LatLngBounds();

	
    if (mc == null) {
		// If clustering is NOT used, we have to do it the hard way - Go through all markers and set the map reference to null, then set the map reference back for all markers matching the filters.
		for (i in markers) {
			markers[i].setMap(null);
		}
		$.each(geoLocationArray, function(k,v) {
			// If the GeoLocation fulfills the filters, make a GoogleMaps marker and add i to the cluster.
			if (filter1(v) && filter2(v) && filter3(v)) {
				markers[k].setMap(v.map);
                // Extends the bounds object to encompass the markers geolocation.
                bounds.extend(markers[k].getPosition());
			}
		});
	} else {
		// If clustering is used, we can do it the easy way - Clear the markers of the cluster and add the ones matching the filters. 
		mc.clearMarkers();
		$.each(geoLocationArray, function(k,v) {
			// If the GeoLocation fulfills the filters, make a GoogleMaps marker and add i to the cluster.
			if (filter1(v) && filter2(v) && filter3(v)) {
				// Create a GoogleMaps marker object.
				var marker = new google.maps.Marker({
					position: v.position,
					title: v.title,
					map: v.map,
					icon: v.icon
				});
				// Adds an event listener to open an infowindow for each of the markes.
				google.maps.event.addListener(marker, 'click', function() {
					infowindow.setContent(v.info_window_content);
					infowindow.open(map, marker);
				});
				
				mc.addMarker(marker);
                
                // Extends the bounds object to encompass the markers geolocation.
                bounds.extend(marker.getPosition());

			}
		});
	}
    
    // Sets the map to fit the bounds object.
    map.fitBounds(bounds);
    
}

function filter1(marker) {
	if ($("#filter1Select").length > 0) {
		var filter_1_index = $("#filter1Select").attr("selectedIndex");
		if (filter_1_index == 0) {
			return true;
		} else {
			var filter_1_value =  $("#filter1Select").find("option:selected").attr("text");
			if (marker.filter_value_1 == filter_1_value) {
				return true;
			} else {
				return false;
			}
		}
	} else {
		return true;
	}
}

function filter2(marker) {
	if ($("#filter2Select").length > 0) {
		var filter_2_index = $("#filter2Select").attr("selectedIndex");
		if (filter_2_index == 0) {
			return true;
		} else {
			var filter_2_value =  $("#filter2Select").find("option:selected").attr("text");
			if (marker.filter_value_2 == filter_2_value) {
				return true;
			} else {
				return false;
			}
		}
	} else {
		return true;
	}
}

function filter3(marker) {
	if ($("#filter3Select").length > 0) {
		var filter_3_index = $("#filter3Select").attr("selectedIndex");
		if (filter_3_index == 0) {
			return true;
		} else {
			var filter_3_value =  $("#filter3Select").find("option:selected").attr("text");
			if (marker.filter_value_3 == filter_3_value) {
				return true;
			} else {
				return false;
			}
		}
	} else {
		return true;
	}
}

