var url_to_mapfeed_module = null;
var id = null;
var node_id = null;
var map =  null;
var geoLocationArray = new Array();		// Define an array to hold the Geo Location objects.
var markers = new Array();				// Define an array to hold the GoogleMaps Marker objects.
var mc = null;
var filter1Array = new Array();
var filter2Array = new Array();
var filter3Array = new Array();

function initmwgeolocation(p_url_to_mapfeed_module, p_id, p_node_id) {
	// Save the received parameters in global variables.
	url_to_mapfeed_module = p_url_to_mapfeed_module;
	id = p_id;
	node_id = p_node_id;
	
	//	Initialize an options array with default value for some options.
	var myOptions = {
		disableDoubleClickZoom: false,
		keyboardShortcuts: true,
		maxWidth: 500
	};

	// Initialize a Google Maps map object, connect it to the map-canvas, and set the maps defalut options array. 
	map = new google.maps.Map(document.getElementById(id), myOptions);

	// Call function to load the markers, and place the markes on the map.
	getMarkers();
}

function getMarkers() {
	// Call the "mapfeed" module to get the json data containing the geo location data.
	$.getJSON(url_to_mapfeed_module, function(data) {				

		// Set the dynamic Map Options according to what is specified in the "Geo Location Folder" object. These settings is contained in the "data.settings" part of the json data.
		map.setOptions({
				draggable: parseInt(data.settings.draggable),
				zoomControl: parseInt(data.settings.zoom_control), 
				streetViewControl: parseInt(data.settings.street_view_control),
				panControl: parseInt(data.settings.pan_control),
				mapTypeControl: parseInt(data.settings.map_type_control),
				scaleControl: parseInt(data.settings.scale_info)
			});
		
		// Set the MapType according to what is specified in the "Geo Location Folder" object.
		switch(data.settings.maptype)
		{
			case "ROADMAP": 	map.setMapTypeId(google.maps.MapTypeId.ROADMAP); break;
			case "HYBRID": 		map.setMapTypeId(google.maps.MapTypeId.HYBRID); break;
			case "SATELLITE": 	map.setMapTypeId(google.maps.MapTypeId.SATELLITE); break;
			case "TERRAIN": 	map.setMapTypeId(google.maps.MapTypeId.TERRAIN); break;
			default: 			map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
		}

		// 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();
		
		// Loop through the "Geo Location" children, create a GeoLocation object and a marker for each of them, and connects the marker to the map. 
		$.each(data.locations, function(i,v) {
			// Get the default icon from the "Geo Location Folder" to use if the "Geo Location" marker itself dosn't specify an icon_image.			
			if (data.locations[i].icon_image=="" && data.locations[i].icon_image.length <= 0) {
				var icon = data.settings.icon;
			} else {
				var icon = data.locations[i].icon_image;
				
			}

			// Construct the content for the info window.
			var info_window_content = '<div class="mwgeolocation_infowindow">';
			info_window_content +=       '<h3>' + data.locations[i].name + '</h3>';
            if (data.locations[i].infowindow_image.length > 0) {
                info_window_content +=       '<img src="' + data.locations[i].infowindow_image + '">';
            }
			info_window_content +=       '<p>' + data.locations[i].infowindow_description + '</p>';
			if (jQuery.isEmptyObject(data.locations[i].related_objects)) {
			} else {
				info_window_content +=    '<ul>';
				$.each(data.locations[i].related_objects, function(p,n) {
					info_window_content +=       '<li>';
					info_window_content +=           '<a href=' + data.locations[i].related_objects[p].url + '>' + data.locations[i].related_objects[p].name + '</a>';
					info_window_content +=       '</li>';
				});
				info_window_content +=    '</ul>';
			}
			if (data.settings.read_more_about_text != null) {
				info_window_content +=    '<ul>';
				info_window_content +=       '<li>';
				info_window_content +=           '<a href=' + data.locations[i].url_alias + '>' + data.settings.read_more_about_text + ' ' + data.locations[i].name + '</a>';
				info_window_content +=       '</li>';
				info_window_content +=    '</ul>';
			}
			info_window_content +=    '</div>';

			// Create a GeoLocation object.
			var latlng = new google.maps.LatLng(data.locations[i].location.latitude, data.locations[i].location.longitude);
			var title = data.locations[i].name;
			var filter_value_1 = data.locations[i].filter_value_1;
			var filter_value_2 = data.locations[i].filter_value_2;
			var filter_value_3 = data.locations[i].filter_value_3;
			var geolocation = new GeoLocation(latlng, title, map, icon, info_window_content, filter_value_1, filter_value_2, filter_value_3);
			
			// Create a GoogleMaps marker object.
			var marker = new google.maps.Marker({
				position: geolocation.position,
				title: geolocation.title,
				map: geolocation.map,
				icon: geolocation.icon
			});
			
			// Add marker to array of markers
			markers.push(marker);
			
			// Adds an event listener to open an infowindow for each of the markes.
			google.maps.event.addListener(marker, 'click', function() {
				infowindow.setContent(info_window_content);
				infowindow.open(map, marker);
			});
			
			// Extends the bounds object to encompass the markers geolocation.
			bounds.extend(marker.getPosition());
			
			// Add the GeoLocation object to the geoLocationArray array for later use in filtering.
			geoLocationArray.push(geolocation);
		});
		
		// Loops through the filter arrays
		$.each(data.filter1array, function(i,v) {
			filter1Array.push(v);
		});
		$.each(data.filter2array, function(i,v) {
			filter2Array.push(v);
		});
		$.each(data.filter3array, function(i,v) {
			filter3Array.push(v);
		});
		
		// Sets the map to fit the bounds object.
		map.fitBounds(bounds);

		// Make cluster
		if (data.settings.cluster_style != 'Do NOT use clusters') {
			var style = -1;
			switch(data.settings.cluster_style)
			{
				case "Use standard cluster icons": 	style = -1; break;
				case "Use custom cluster icons": 	style = 0; break;
				default: 							style = -1;
			}
			var maxZoom = null;
			switch(data.settings.cluster_max_zoom)
			{
				case "Clustering enabled at all zoom levels": 		maxZoom = null; break;
				default: maxZoom = data.settings.cluster_max_zoom;
			}
			var cluster_zoom_on_click = true;
			var title = '';
			switch(data.settings.cluster_zoom_on_click)
			{
				case "0": 		cluster_zoom_on_click = false; title = ''; break;
				case "1": 		cluster_zoom_on_click = true; title = 'click to zoom'; break;
			}
			var clusterOptions = {
						gridSize: 60,
						maxZoom: maxZoom,
						styles: styles[style],
						zoomOnClick: cluster_zoom_on_click,
						title: title
					};
			mc = new MarkerClusterer(map, markers, clusterOptions);
		}
		
		// Fill the filter selection dropdowns with the proper filter values.
		createFilterSelectionValues();
		
	});
}

// "Definition" of a javascript "class" to hold the GeoLocation objects.
function GeoLocation(position, title, map, icon, info_window_content, filter_value_1, filter_value_2, filter_value_3) {
	this.position = position;
	this.title = title;
	this.map = map;
	this.icon = icon;
	this.info_window_content = info_window_content;
	this.filter_value_1 = filter_value_1;
	this.filter_value_2 = filter_value_2;
	this.filter_value_3 = filter_value_3;
}

