/**********************************************************************
 * HH400 Google Maps API Project
 * @File: hh400.map.water.js
 * Displays H209 forum 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 H209 On and Off
***/

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

	var water_check = document.getElementById('water-toggle');

	if (water_check.checked) {
	
		if (mst_overlay != 'water-toggle') {
			mst_overlay = 'water-toggle';
			checkNumOverlays();
			overlays_tracker = overlays_tracker + 1;
		}
	
		if (water_cluster) {
			water_cluster.addMarkers(water_overlay);
			water_cluster.refresh();
		}
		else {
		
			showLoadingStatus('Origins');
			
			var bounds = new GLatLngBounds();
			
			GDownloadUrl("utilities/hh400.point.data.php?idx=6", function(data, responseCode) {
			
				var m_html = "";
			
				 if (responseCode == 200) {
					 var json_points = eval('(' + data + ')') ;
					 
						//Create the icon object
						var icon = new GIcon();
						icon.image = 'images/library/mapicons/icon-h209.png';
						icon.iconSize = new GSize(25,25);
						icon.iconAnchor = new GPoint(12, 25);
						icon.infoWindowAnchor = new GPoint(12, 12);
	
					 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 water_point = new GLatLng(lat,lng);
						var marker = createMarker(water_point, icon, title, description, id, 6, num_images, num_videos, num_links, 0, m_html);
						
						//map.addOverlay(marker);
						water_overlay.push(marker);
						
						//Add point to bounds object
						bounds.extend(water_point);
					}
					
					//Create the cluster icon object
						var cl_icon = new GIcon();
						cl_icon.image = 'images/library/mapicons/icon-h209-cluster.png';
						cl_icon.iconSize = new GSize(27,27);
						cl_icon.iconAnchor = new GPoint(13, 27);
						cl_icon.infoWindowAnchor = new GPoint(13, 13);
					
					water_cluster = new ClusterMarker(map, {markers:water_overlay, clusterMarkerIcon:cl_icon, clusterMarkerClick:function(a){sel_cluster = 'water_cluster'; clusterInfo(a, 'Water Challenges', 'water challenges facing coastal cities in the 21st century');}, clusterMarkerTitle:'There are %count points of interest here'});
					
					zoomTo(bounds);
					
					hideLoadingStatus();
					
        		}
				 
			});
			
		}
	}
	else {
	
		if (overlays_tracker > 0) { overlays_tracker = overlays_tracker - 1; }
	
		if (water_cluster) {
			water_cluster.removeMarkers();
			water_cluster.refresh();
		}
	}

}


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

function toggleWaterLink() {

	var water_check = document.getElementById('water-toggle');

	if (water_check.checked) {
		water_check.checked = false;
	}
	else {
		water_check.checked = true;
	}
	
	toggleWater();
}