/**********************************************************************
 * HH400 Google Maps API Project
 * @File: hh400.map.historical.maps.js
 * Displays the historical map overlays on the map
 * Includes functions to control map opacity via sliders
 * Also "automagically" moves overlay rows back and forth between
 * Currently visible legend table, and non-active legend table
 * 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
**********************************************************************/

/***
Show/Hide Individual Map Opacity Slider
***/

function toggleMapOpacity(idx) {
	$('map-track-'+idx).toggle();
	scrollMaps('carousel-content-wrapper', 'carousel-content', 'down', 60, 'map-track-'+idx);
}

/***
Set Layer Opacity
***/

function layerOpacity(op,idx) {
	if (historical_maps_overlays[idx]) {
		historical_maps_overlays[idx].getTileLayer().opacity = op;
		map.removeOverlay(historical_maps_overlays[idx]);
		map.addOverlay(historical_maps_overlays[idx]);
  	}
}

/***
Toggle Maps on and Off
***/

function toggleMap(map_id, get_bnds) {

	//Close initial infowindow
	chkWindow();

	var active_mid;
	var activeLayer;
	var activeOverlay;
	
	var map_check = document.getElementById('historical-map-toggle-'+map_id);

	if (map_check.checked) {
		
		activateMap(map_id);
		checkActiveMaps(map_id);
	
		if (historical_maps_overlays[map_id]) {
			map.addOverlay(historical_maps_overlays[map_id])

			//Zoom to the bounds of the map
			if (get_bnds == 1) {
				var m_bounds = historical_maps_bnds[map_id];
				map.setZoom(map.getBoundsZoomLevel(m_bounds));
				var clat = (m_bounds.getNorthEast().lat() + m_bounds.getSouthWest().lat()) /2;
				var clng = (m_bounds.getNorthEast().lng() + m_bounds.getSouthWest().lng()) /2;
				map.setCenter(new GLatLng(clat,clng));
			}
			
		}
		else {
		
			function getTcTiles(a,b) {
				var img_tile = historical_maps_tiles[map_id]+'/'+TileToQuadKey(a.x,a.y,b)+'.png';
				return img_tile;
			}
			
			var tilelayer = new GTileLayer(null, null, null, {
				isPng:true,
				opacity:parseInt(document.getElementById('opacity-label-'+map_id).innerHTML)/100
			});
			
			tilelayer.getOpacity = function() {
				if (!ini_opacity[map_id]) {
					return parseInt(document.getElementById('opacity-label-'+map_id).innerHTML)/100;
					ini_opacity.push(map_id);
				}
				else {
					return this.opacity;
				}
			}
			
			tilelayer.getTileUrl = getTcTiles;
			
			function TileToQuadKey ( x, y, zoom){
			var quad = "";
				for (var i = zoom; i > 0; i--){
					var mask = 1 << (i - 1);
					var cell = 0;
					if ((x & mask) != 0)
						cell++;
					if ((y & mask) != 0)
						cell += 2;
					quad += cell;
				}
				return quad;
			}
				
			activeOverlay = new GTileLayerOverlay(tilelayer);
			map.addOverlay(activeOverlay);
		
			historical_maps_overlays[map_id] = activeOverlay;
			
			var bounds = new GLatLngBounds();
			
			GDownloadUrl("utilities/hh400.point.data.php?idx=3&map_id="+map_id, function(data, responseCode) {
			
				 if (responseCode == 200) {
					 var json_points = eval('(' + data + ')') ;
	
					 for (var i=0; i<json_points.markers.length; i++) {
						var id = json_points.markers[i].id;
						var lat = json_points.markers[i].lat;
						var lng = json_points.markers[i].lng;
						
						var location_point = new GLatLng(lat,lng);
						
						//Add point to bounds object
						bounds.extend(location_point);
					}
					
					historical_maps_bnds[map_id] = bounds;
					
					//Zoom to the bounds of the map
					//Set the map bounds based on the min_x, min_y, max_x, max_y
					map.setZoom(map.getBoundsZoomLevel(bounds)+1);
					var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
					var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
					map.setCenter(new GLatLng(clat,clng));
					
					//Open up the sidebar info window and show the user some reference info about the map
					if (map_id != 8) {
						showTip('maps','maps-tooltip-icon-'+map_id, map_id);
					}
					
        		}
				 
			});
			
		}
	}
	else {
		if (historical_maps_overlays[map_id]) {
			map.removeOverlay(historical_maps_overlays[map_id]);
			deActivateMap(map_id);
			checkActiveMaps();
		}
	}

}


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

function toggleMapLink(map_id, get_bnds) {

	var map_check = document.getElementById('historical-map-toggle-'+map_id);

	if (map_check.checked) {
		map_check.checked = false;
	}
	else {
		map_check.checked = true;
	}
	
	toggleMap(map_id, get_bnds);
}


/***
Activate a map and show it as visible in the legend
(moves the DOM node from one table to another)
***/

function activateMap(map_id) {

	$('currently-active-maps').childElements()[0].insert({bottom:$('historical-maps-tr-'+map_id)});
	$('currently-active-maps').childElements()[0].insert({bottom:$('historical-track-tr-'+map_id)});
	scrollMaps('carousel-content-wrapper', 'carousel-content', 'up', 300);
	new Effect.Highlight('historical-maps-tr-'+map_id, { startcolor: '#01BBE7', endcolor: '#ffffff' });
}

			
/***
De-Activate a map and remove it from the visible legend
(moves the DOM node from one table to another)
***/

function deActivateMap(map_id) {

	var element_matched = false;

	var tab_elements = $('inactive-maps').childElements()[0].childElements();
	var sel_number = map_id - 1;
	
	for (var i=0; i < tab_elements.length; i++) {
		var sel_element = tab_elements[i];
		var sel_id = sel_element.identify();
		var sel_id_chunks = sel_id.split('-');
		var sel_id_number = sel_id_chunks.pop();
		if (sel_id_chunks.length == 1) { continue; } //Not a named element
		if (sel_id_number == sel_number) {
			$('historical-track-tr-'+sel_number).insert({after:$('historical-track-tr-'+map_id)});
			$('historical-track-tr-'+sel_number).insert({after:$('historical-maps-tr-'+map_id)});
			element_matched =true;
		}
		
	}
	
	if (!element_matched) {
		$('inactive-maps').childElements()[0].insert({top:$('historical-track-tr-'+map_id)});
		$('inactive-maps').childElements()[0].insert({top:$('historical-maps-tr-'+map_id)});
	}
	
}

/***
Toggle the visibility of the Currently Visible Maps table, based on number of elements
***/

function checkActiveMaps(map_id) {
	var tab_elements = $('currently-active-maps').childElements()[0].childElements();
	if (tab_elements.length > 1) {
		$('currently-active-maps').setStyle({display:'block'});
		$('map-separator').setStyle({display:'block'});
	}
	else {
		$('currently-active-maps').setStyle({display:'none'});
		$('map-separator').setStyle({display:'none'});
	}
}