/**********************************************************************
 * HH400 Google Maps API Project
 * @File: hh400.map.illustrations.maps.js
 * Displays the historical map illustrations 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 illustrations on and Off
***/

function toggleIllustrations(active_marker) {

	//Close initial infowindow
	chkWindow();

	var illustration_check = document.getElementById('illustrations-toggle');

	if (illustration_check.checked) {
		
		if (mst_overlay != 'illustrations-toggle') {
			mst_overlay = 'illustrations-toggle';
			checkNumOverlays();
			overlays_tracker = overlays_tracker + 1;
		}
	
		if (illustrations_cluster) {
		
			for (var i=0; i < illustrations_topten.length; i++) {
				map.removeOverlay(illustrations_topten[i]);
			}
			
			illustrations_topten = new Array();
		
			if (!illustrations_cluster_added) {
				illustrations_cluster.addMarkers(illustrations_overlay);
				illustrations_cluster.refresh();
				illustrations_cluster_added = true;
			}

			if (active_marker) {
				addTopMarker('illustrations', active_marker);
			}
		}
		else {
		
			showLoadingStatus('Illustrations');
		
			var bounds = new GLatLngBounds();
			
			GDownloadUrl("utilities/hh400.point.data.php?idx=31", function(data, responseCode) {
			
				var m_html = "";
			
				 if (responseCode == 200) {
					 var json_points = eval('(' + data + ')') ;
					 
						//Create the icon object
						var icon = new GIcon();
						icon.iconSize = new GSize(25,25);
						icon.iconAnchor = new GPoint(12, 25);
						icon.infoWindowAnchor = new GPoint(12, 12);
	
					if (json_points.markers[0]) {
					
						 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;
							
							icon.image = 'images/library/mapicons/' + json_icon;
							
							var illustration_point = new GLatLng(lat,lng);
							var marker = createMarker(illustration_point, icon, title, description, id, 4, num_images, num_videos, num_links,0, m_html);
							
							//Add an event listener to maximize the marker by default
							GEvent.addListener(marker, "infowindowopen", function() {
								var iw = map.getInfoWindow();
								iw.maximize();
								_theme_id = 3;
								setTimeout(function(){iw.disableMaximize()},1500);
							});
							
							//map.addOverlay(marker);
							illustrations_overlay.push(marker);
							illustrations_overlay_ids[id] = marker;
							
							//Add point to bounds object
							bounds.extend(illustration_point);
						}
						
						//Create the cluster icon object
						var cl_icon = new GIcon();
						cl_icon.image = 'images/library/mapicons/icon-illustrations-cluster.png';
						cl_icon.iconSize = new GSize(27,27);
						cl_icon.iconAnchor = new GPoint(13, 27);
						cl_icon.infoWindowAnchor = new GPoint(13, 13);
					
						illustrations_cluster = new ClusterMarker(map, {markers:illustrations_overlay, clusterMarkerIcon:cl_icon, clusterMarkerClick:function(a){sel_cluster = 'illustrations_cluster'; clusterInfo(a, 'Historical Illustrations', 'historical illustration maps');}, clusterMarkerTitle:'There are %count points of interest here'});
						
						illustrations_cluster_added = true;
						
						zoomTo(bounds);
						
						hideLoadingStatus();
						
						if (active_marker) {
							addTopMarker('illustrations', active_marker);
						}
						
					}
					
        		}
				 
			});
			
		}
	}
	else {
		
		if (overlays_tracker > 0) { overlays_tracker = overlays_tracker - 1; }
		
		if (illustrations_cluster) {
			illustrations_cluster.removeMarkers();
			illustrations_cluster.refresh();
			
			for (var i=0; i < illustrations_topten.length; i++) {
				map.removeOverlay(illustrations_topten[i]);
			}
			illustrations_topten = new Array();
			illustrations_cluster_added = false;
		}
	}

}


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

function toggleIllustrationsLink(active_marker) {

	var illustration_check = document.getElementById('illustrations-toggle');

	if (illustration_check.checked && !active_marker) {
		illustration_check.checked = false;
	}
	else {
		illustration_check.checked = true;
	}
	
	toggleIllustrations(active_marker);
}