/**********************************************************************
 * HH400 Google Maps API Project
 * @File: hh400.dual.maps.js
 * Sets up a side-by-side display of a
 * Historical Map Overlay and a modern day Google 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
**********************************************************************/

if (GBrowserIsCompatible()) {

//Set up some global variables
var fullscreen = 0;

var map;
var historical_map;
var sidebar_status = 'max';
var ini_point;
var hm_ov_control = new GOverviewMapControl();
var gm_ov_control = new GOverviewMapControl();
var ini_window;
var activeLayer;
var activeOverlay;

	/***
	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]);
		}
	}

	//loadmap function sets up the side by side maps, based on JS vars set up in the HEAD of the doc
	function loadmap() {
	
		var active_mid;
		var activeLayer;
		var activeOverlay;
		
		//Historical Map
		historical_map = new GMap2(document.getElementById('historical-map'), {backgroundColor:'#99b3cc'});
		
		//Hack for IE6 and under; If NOT IE6, then use the "new" map controls
		//otherwise load the other controls
		//This is related to a bug with the "new" Google Maps Control and the GE Plugin
		var b_check = detectIeSix();
		if (!b_check) {
			historical_map.setUIToDefault();
		}
		else {
			historical_map.addControl(new GLargeMapControl());
			historical_map.addControl(new GMapTypeControl());
			historical_map.addControl(new GScaleControl());
		}
		
		//Add and minimize the overview control
		historical_map.addControl(hm_ov_control);
		hm_ov_control.hide(true);
		
		//The initial point for the maps; map center and zoom updated later
		//with map overlay extent
		ini_point = new GLatLng(40.7043266,-74.0142059);
		historical_map.setCenter (ini_point, 3);
		
		//Zoom to the bounds of the map
		if (bounds) {
			var m_bounds = bounds;
			historical_map.setZoom(historical_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;
			historical_map.setCenter(new GLatLng(clat,clng));
		}
		
		//Set up event listeners to control the "slave" map (Google Map)
		//Whenever the master (historical map) is moved or zoomed
		GEvent.addListener(historical_map, 'moveend', function() {
			var zoom = historical_map.getZoom();
			var center = historical_map.getCenter();
			map.setCenter(center, zoom);
		});
		
		GEvent.addListener(historical_map, 'movestart', function() {
			var zoom = historical_map.getZoom();
			var center = historical_map.getCenter();
			map.setCenter(center, zoom);
		});
		
		GEvent.addListener(historical_map, 'drag', function() {
			var zoom = historical_map.getZoom();
			var center = historical_map.getCenter();
			map.setCenter(center, zoom);
		});
		
		//Tile overlay call
		function getTcTiles(a,b) {
			var img_tile = map_href+'/'+TileToQuadKey(a.x,a.y,b)+'.png';
			return img_tile;
		}
		
		//Tile layer object sets up the initial layer transparency
		var tilelayer = new GTileLayer(null, null, null, {
			isPng:true,
			opacity: 0.7
		});
		
		tilelayer.getOpacity = function() {
			return this.opacity;
		}
		
		//Update the map with the tile overlay
		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);
		historical_map.addOverlay(activeOverlay);
		
		/*************************************************************************************/
		
		
		//Standard Map
		map = new GMap2(document.getElementById('map'), {backgroundColor:'#99b3cc'});
		map.disableDragging();
		
		map.addControl(new GMapTypeControl());
		
		map.addControl(gm_ov_control);
		gm_ov_control.hide(true);
		
		ini_point = new GLatLng(40.7043266,-74.0142059);
		map.setCenter (ini_point, 3);
		
		//Zoom to the bounds of the map
		if (bounds) {
			var m_bounds = bounds;
			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));
		}
		
	}
	


} //end GBrowserIsCompatible
else {
	alert("Sorry.  It appears that your browser is not compatible with the Google Maps API");
}
