/**********************************************************************
 * HH400 Google Maps API Project
 * @File: hh400.map.voyages.maps.js
 * Displays Henry Hudson's Voyages as markers/polylins on the map
 * Note that this script utilizes several arrays to keep track of voyages
 * which are treated as individual layers, yet which can behave as a 
 * master voyages layer (sort of)
 * 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 Voyages on and Off
***/

var arrow_vis = new Array();

function toggleVoyage(vid, color, mst) {

	chkWindow();
	
	var active_vid;
	var voyage_check = document.getElementById('voyage-toggle-'+vid);

	if (voyage_check.checked) {
	
		if (mst_overlay != 'voyage-toggle-'+vid) { 
			mst_overlay = 'voyage-toggle-'+vid;
			checkNumOverlays();
			overlays_tracker = overlays_tracker + 1;
		}
	
		if (voyages_overlays[vid+'-0']) {
		
			showLoadingStatus('Voyages');
			
			voyages_overlays[vid+'-0'].show();
			voyages_overlays[vid+'-1'].show();
			if (arrow_vis[vid] == 'hidden') { arrow_vis[vid] = 'show'; }
			map.addOverlay(voyages_arrows_overlays[vid]);
			var bnds = voyages_overlays[vid+'-0'].getBounds();
			zoomTo(bnds);
			
			if(voyages_cluster[vid]) {
				voyages_cluster[vid].addMarkers(voyages_milestones[vid]);
				voyages_cluster[vid].refresh();
			}
			//hideLoadingStatus();
		}
		else {
		
			showLoadingStatus('Voyages');
			
			active_vid = vid;
			
			function getVoyageLines() {
				GDownloadUrl("utilities/voyages.polyline.php?idx=" + active_vid+"&encoded=true", function(data, responseCode) {
					 var poly_coords = eval('(' + data + ')') ;
					 
					for (var i=0; i < poly_coords.lines.length; i++) {
						voyages_overlays[active_vid+'-'+i] = poly_coords.lines[i];
						map.addOverlay(voyages_overlays[active_vid+'-'+i]);
					}
				});
			}
			
			
			function getArrows() {
				GDownloadUrl("utilities/voyages.polyline.php?idx=" + active_vid, function(data, responseCode) {
					 var poly_coords = eval('(' + data + ')') ;
	
					 var arrows_coords = poly_coords.markers;
					 zoomToBounds(arrows_coords,true,'0');
					 
					polylineEncoder = new PolylineEncoder();
	
					 voyages_arrows_overlays[active_vid] = new BDCCArrowedPolyline(arrows_coords,"#"+color,4,0,null,30,9,"#"+color,2,0.9,active_vid);
					 map.addOverlay(voyages_arrows_overlays[active_vid]);
					 arrow_vis[active_vid] = 'show';
				});
			}
			
			//Display arrows
			setTimeout(function(){getArrows()}, 1000);

			function getMilestones() {
				GDownloadUrl("utilities/hh400.point.data.php?idx=5&mi=" + vid, function(data, responseCode) {
				
					//Set a hash table and sort out the multiple listings
					var keycounts = [];
				
					 if (responseCode == 200) {
						 var json_points = eval('(' + data + ')') ;
							
						voyages_milestones[active_vid] = new Array();
						voyages_overlays_ids[active_vid] = new Array();
						
						//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;
							
							//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);
							icon.image = 'images/library/mapicons/' + json_icon;
							
							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(voyages_overlays_ids['+active_vid+']['+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 voyage_point = new GLatLng(lat,lng);
							var marker = createMarker(voyage_point, icon, title, description, id, 5, num_images, num_videos, num_links, num_threed_images, m_html);
							//map.addOverlay(marker);
							voyages_milestones[active_vid].push(marker);
							voyages_overlays_ids[active_vid][id] = marker;
						}
						
						//Create the cluster icon object
							var cl_icon = new GIcon();
							cl_icon.image = 'images/library/mapicons/icon-voyages-'+active_vid+'-cluster.png';
							cl_icon.iconSize = new GSize(27,27);
							cl_icon.iconAnchor = new GPoint(13, 27);
							cl_icon.infoWindowAnchor = new GPoint(13, 13);
						
						voyages_cluster[active_vid] = new ClusterMarker(map, {markers:voyages_milestones[active_vid], clusterMarkerIcon:cl_icon, clusterMarkerClick:function(a){sel_cluster = 'voyages_cluster['+active_vid+']'; clusterInfo(a, 'Henry Hudson\'s Voyages', 'milestones for this voyage');}, clusterMarkerTitle:'There are %count points of interest here'});
						
						voyages_cluster[active_vid].refresh();
					}
					 
				});
			}
			
			//Display the milestones
			getMilestones();
			
			//Display Voyage Lines
			getVoyageLines();
			
		}
	}
	else {
	
		if (overlays_tracker > 0) { overlays_tracker = overlays_tracker - 1; }
	
		if (voyages_overlays[vid+'-0']) {
		
			voyages_overlays[vid+'-0'].hide();
			voyages_overlays[vid+'-1'].hide();
			if (arrow_vis[vid] == 'show') { arrow_vis[vid] = 'hidden'; }
			map.removeOverlay(voyages_arrows_overlays[vid]);
			if(voyages_milestones[vid]) {
				voyages_cluster[vid].removeMarkers();
				voyages_cluster[vid].refresh();
			}
		}
	}

}


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

function toggleVoyageLink(vid, color, tt) {

	var voyage_check = document.getElementById('voyage-toggle-'+vid);

	if (voyage_check.checked && !tt) {
		voyage_check.checked = false;
	}
	else {
		voyage_check.checked = true;
	}
	
	toggleVoyage(vid, color);
}


/***
Helper Toggle: Turn All Voyages on/off
***/

function toggleAllVoyages() {
	
	var voyage_check_all = document.getElementById('voyage-toggle-all');

	for (var i=0; i<voyage_ids.length; i++) {
		
		var voyage_check = document.getElementById('voyage-toggle-'+voyage_ids[i]);
		
		if (voyage_check_all.checked) {
			
			if (!voyage_check.checked) {
				voyage_check.checked = true;
				toggleVoyage(voyage_ids[i], voyage_colors[i]);
			}
			
		}
		else if (!voyage_check_all.checked) {
			
			if (voyage_check.checked) {
				voyage_check.checked = false;
				toggleVoyage(voyage_ids[i], voyage_colors[i]);
			}
			
		}
	}

}


/***
Helper Link for All Voyages: Better UX to Toggle All Voyages on and Off
***/

function toggleVoyageLinkAll() {

	var voyage_check = document.getElementById('voyage-toggle-all');

	if (voyage_check.checked) {
		voyage_check.checked = false;
	}
	else {
		voyage_check.checked = true;
	}
	
	toggleAllVoyages();
}