/**********************************************************************
 * HH400 Google Maps API Project
 * @File: hh400.map.locations.js
 * Displays locations and events as markers on the map
 * Version 1.0 | 05-04-2009
 * Developed by Cartosoft, LLC
 * http://www.cartosoft.com | info@cartosoft.com
 * Copyright 2009 Google, Inc. | http://www.google.com
**********************************************************************/

/***
Toggle Locations on and Off
***/

function toggleLocations() {
	
	//Close initial infowindow
	chkWindow();

	var location_check = document.getElementById('locations-toggle');

	if (location_check.checked) {
	
		if (mst_overlay != 'locations-toggle') { 
			mst_overlay = 'locations-toggle';
			checkNumOverlays();
			overlays_tracker = overlays_tracker + 1;
		}
	
		if (events_cluster) {
			events_cluster.addMarkers(locations_overlay);
			events_cluster.refresh();
		}
		else {
		
			showLoadingStatus('Events');
			
			var bounds = new GLatLngBounds();
			
			GDownloadUrl("utilities/hh400.point.data.php?idx=4", function(data, responseCode) {
			
				//Set a hash table and sort out the multiple listings
				var keycounts = [];
			
				 if (responseCode == 200) {
					 var json_points = eval('(' + data + ')') ;
					 
						//Create the icon object
						var icon = new GIcon();
						icon.image = 'images/library/mapicons/icon-events.png';
						icon.iconSize = new GSize(25,25);
						icon.iconAnchor = new GPoint(12, 25);
						icon.infoWindowAnchor = new GPoint(12, 12);
						
						//Populate the hash table
						for (var i = 0; i < json_points.markers.length; i++) {
							var lat = json_points.markers[i].lat;
							var lng = json_points.markers[i].lng;
							var key = hash(lng,lat);
								keycounts[key] = 0;
						}
						for (var i = 0; i < json_points.markers.length; i++)	{
							var lat = json_points.markers[i].lat;
							var lng = json_points.markers[i].lng;
							var key = hash(lng,lat);
							if (keycounts[key]) keycounts[key]++;
							else (keycounts[key]) = 1;
						}

	
					 for (var i=0; i<json_points.markers.length; i++) {
						var id = json_points.markers[i].id;
						var title = json_points.markers[i].title;
						var description = json_points.markers[i].description;
						var lat = json_points.markers[i].lat;
						var lng = json_points.markers[i].lng;
						var json_icon = json_points.markers[i].icon;
						var num_images = json_points.markers[i].num_images;
						var num_videos = json_points.markers[i].num_videos;
						var num_links = json_points.markers[i].num_links;
						var num_threed_images = json_points.markers[i].num_threed_images;
						
						var key  = hash(lng, lat);
						var nodes = [];
						var m_html = "";
						
						var c = 1;
						
						if (keycounts[key] != 1) {
							for (var w=0; w<json_points.markers.length; w++) {
								var m_id = json_points.markers[w].id;
								var m_title = json_points.markers[w].title;
								var m_lat = json_points.markers[w].lat;
								var m_lng = json_points.markers[w].lng;
								var m_key  = hash(m_lng, m_lat);
								if (key == m_key && m_id != id) {
																
									//If there are multiple listings at the same location then add the key to the nodes array and the html string to the nodehtmls array.
									if (keycounts[m_key] != 1 && !nodes[m_key]) {
										nodes.push(m_key);
										m_html += '<li><a href="javascript:void(0);" class="cluster-link" onClick="javascript:clickMarker(locations_overlay_ids['+m_id+'])">'+c+'. '+m_title+'</a></li>';
										c = c + 1;
									}
								
								}
							}
						}
						
						if (m_html.length > 0) {
							var vrb = 'are'; var txt_pts = 'points';
							if (nodes.length == 1) { vrb = 'is'; txt_pts = 'point'; }
							m_html = 'There '+vrb+' '+nodes.length+' additional '+txt_pts+' in this same location:<br/><ul>' + m_html + '</ul>';
						}
						
						var location_point = new GLatLng(lat,lng);
						var marker = createMarker(location_point, icon, title, description, id, 4, num_images, num_videos, num_links, num_threed_images, m_html);
						
						//map.addOverlay(marker);
						locations_overlay.push(marker);
						locations_overlay_ids[id] = marker;
						
						//Add point to bounds object
						bounds.extend(location_point);
					}
					
					//Create the cluster icon object
						var cl_icon = new GIcon();
						cl_icon.image = 'images/library/mapicons/icon-events-cluster.png';
						cl_icon.iconSize = new GSize(27,27);
						cl_icon.iconAnchor = new GPoint(13, 27);
						cl_icon.infoWindowAnchor = new GPoint(13, 13);
					
					events_cluster = new ClusterMarker(map, {markers:locations_overlay, clusterMarkerIcon:cl_icon, clusterMarkerClick:function(a){sel_cluster = 'events_cluster'; clusterInfo(a, 'Events', 'events related to Henry Hudson\'s 400th celebration');}, clusterMarkerTitle:'There are %count points of interest here'});
					
					zoomTo(bounds);
					
					hideLoadingStatus();
					
        		}
				 
			});
			
		}
	}
	else {
		if (overlays_tracker > 0) { overlays_tracker = overlays_tracker - 1; }
		if (events_cluster) {
			events_cluster.removeMarkers();
			events_cluster.refresh();
		}
	}

}


/***
Helper Link: Better UX to Toggle Locations on and Off
***/

function toggleLocationsLink() {

	var location_check = document.getElementById('locations-toggle');

	if (location_check.checked) {
		location_check.checked = false;
	}
	else {
		location_check.checked = true;
	}
	
	toggleLocations();
}