// creates a Google Map with MGDS bathymetry and LDEO core locations
// allows users to see a core description by clicking at a core location
		//<![CDATA[
		// initiate variables
		var num_markers = 0;
		var marker = new Array();
		var tmp;
		marker[1] = null;
		marker[2] = null;
		var ptLatArr = new Array();
		ptLatArr[1] = 90;
		ptLatArr[2] = -90;
		var ptLongArr = new Array();
		ptLongArr[1] = -180;
		ptLongArr[2] = -180;
		var focusSiteZoom = false;
		var icon = new GIcon();
		icon.image = "http://www.marine-geo.org/services/google/icons/cross_icon.png";
		icon.iconAnchor = new GPoint(7,7);
		ptLat = 0;
		ptLong = 0;
		env = '';
		var tminx, tmaxx;
		var tminy, tmaxy;
		var cnt_x, cnt_y;
		var core_href;
		var core_list;
		
		// create map
		var map = new GMap2(document.getElementById("map"));
		
		// create tile layer for 1 to 10 zoom levels
		var tileDoq= new GTileLayer(new GCopyrightCollection(""),2,10);
		tileDoq.myLayers='topo';
		tileDoq.myFormat='image/jpeg';
		tileDoq.myBaseURL='http://www.marine-geo.org/services/wms?';
		tileDoq.getTileUrl=CustomGetTileUrl;
		tileDoq.getOpacity = function() {return 1.0;}
	
		var gmrt_layer=[tileDoq];
		var gmrt_hybrid_layer=[tileDoq,G_HYBRID_MAP.getTileLayers()[1]];
	
		var bathymetry = new GMapType(gmrt_layer, G_SATELLITE_MAP.getProjection(), "Cores", G_SATELLITE_MAP);
		var bathyhybrid = new GMapType(gmrt_hybrid_layer, G_SATELLITE_MAP.getProjection(), "Labels", G_SATELLITE_MAP);
	
	
		map.getMapTypes().length = 0;
		
		map.addMapType(G_HYBRID_MAP);
		map.addMapType(G_SATELLITE_MAP);
		map.addMapType(bathymetry);
		//map.addMapType(bathyhybrid);
		
		// open map for first time cdentered on Palisades Ny	
		map.setCenter(new GLatLng(41.0,-73.9), 2);
		
		// add zoom and pan controls to map window
		map.addControl(new GSmallMapControl());
		map.addControl(new GScaleControl());
		map.addControl(new GMapTypeControl());
	
		// enable mouse clicking for controls 
		//map.enableDoubleClickZoom();
		map.enableContinuousZoom();
		map.enableScrollWheelZoom();
		new GKeyboardHandler(map, window);
	  
	  
		//var bounds = map.getBounds();
		var center = map.getCenter();
		var bounds = map.getBounds();

		var current_zoom_level = 2;
		var last_zoom_level = 1;
		
		
		//Create the tile layer object
		var detailLayer = new GTileLayer(new GCopyrightCollection(''));

		//Method to retrieve the URL of the tile
		detailLayer.getTileUrl = function(tile, zoom){
		
		//pass the x and y position as well as the zoom
			var tileURL = "http://www.marine-geo.org/services/google/ldeo_cores/server.php?x="+tile.x+"&y="+tile.y+"&zoom="+zoom;
			return tileURL;
		};
		
		detailLayer.isPng = function() {
			//The example uses GIF's
			return false;
		}
		
		
		
		//add your tiles to the normal map projection
		//detailMapLayers = G_NORMAL_MAP.getTileLayers();
		detailMapLayers = G_HYBRID_MAP.getTileLayers();
		detailMapLayers.push(detailLayer);
		
		//add your tiles to the satellite map projection
		detailMapLayers = G_SATELLITE_MAP.getTileLayers();
		detailMapLayers.push(detailLayer);
		
		//add your tiles to the map projection		
		detailMapLayers = bathymetry.getTileLayers();
		detailMapLayers.push(detailLayer);


		
		//document.getElementById("message").innerHTML = bounds.toString();
		
		
		
		GEvent.addListener(map, 'click', select_corner);
		GEvent.addListener(map, 'zoomend', detect_zoom);
		GEvent.addListener(map, 'drag', detect_drag);
		
		// 'singlerightclick' can be used instead of 'click' for left mouse button
		

		map.setMapType(bathymetry);

		function detect_zoom(oldLevel, newLevel) {
			// clear bounding box when zooming in, keep when zoomiing out
			if ( focusSiteZoom ) {
				focusSiteZoom = false;
				return;
			}
			if(newLevel > oldLevel)
			{	num_markers = 0;		
				map.clearOverlays();
				document.getElementById('lat').value = '';
				document.getElementById('long').value = '';
				document.getElementById('core_ref_num').value = '';
				document.getElementById('dist').value = '';
				document.getElementById('url').value = '';

				document.getElementById('north').value = '';
				document.getElementById('south').value = '';
				document.getElementById('west').value = '';
				document.getElementById('east').value = '';
			}
		}
		function detect_drag() {
			// clear bounding box after dragging
			num_markers = 0;
			map.clearOverlays();
			document.getElementById('lat').value = '';
			document.getElementById('long').value = '';
			document.getElementById('core_ref_num').value = '';
			document.getElementById('dist').value = '';
			document.getElementById('url').value = '';

			document.getElementById('north').value = '';
			document.getElementById('south').value = '';
			document.getElementById('west').value = '';
			document.getElementById('east').value = '';
		}

		function select_corner(overlay, point)
		{
			if (overlay)
			{
				num_markers = 0;
				map.clearOverlays();
				document.getElementById('lat').value = '';
				document.getElementById('long').value = '';
				document.getElementById('core_ref_num').value = '';
				document.getElementById('dist').value = '';
				document.getElementById('url').value = '';
						
				document.getElementById('north').value = '';
				document.getElementById('south').value = '';
				document.getElementById('west').value = '';
				document.getElementById('east').value = '';
			}
			else if (point)
			{
				if (num_markers == 2) return;
				num_markers++;

				marker[num_markers] = new GMarker(point, icon);
				//marker[num_markers] = new GMarker(point);
				map.addOverlay(marker[num_markers]);	

				point.x = point.x*1000;
				point.y = point.y*1000;
				point.x = Math.round(point.x);
				point.y = Math.round(point.y);
				point.x = point.x/1000;
				point.y = point.y/1000;


				ptLatArr[num_markers] = point.y;
				ptLongArr[num_markers] = point.x;	

				if ( num_markers == 1 ) {
					document.getElementById('north').value = point.y;
					document.getElementById('west').value = point.x;
				}
				else if ( num_markers == 2 ) {
					document.getElementById('south').value = point.y;
					document.getElementById('east').value = point.x;			
				}			
				if (num_markers == 2) {

					drawPolylines();
					
					if ( ptLatArr[2] > ptLatArr[1] )
					{
						document.getElementById('north').value = ptLatArr[2];
						document.getElementById('south').value = ptLatArr[1];
						tmp = ptLatArr[1];
						ptLatArr[1] = ptLatArr[2];
						ptLatArr[2] = tmp;
					}

					if ( ( ( ptLongArr[1] < 0 && ptLongArr[2] > 0 ) || ( ptLongArr[2] < 0 && ptLongArr[1] > 0 ) ) && ( ( Math.abs(ptLongArr[1]) + Math.abs(ptLongArr[2]) ) > 180.0 ) ) {
						if (ptLongArr[2] < ptLongArr[1] ) {
							document.getElementById('west').value = ptLongArr[1];
							document.getElementById('east').value = ptLongArr[2];
						}
						else if (ptLongArr[1] < ptLongArr[2] ) {
							tmp = ptLongArr[1];
							ptLongArr[1] = ptLongArr[2];
							ptLongArr[2] = tmp;
							document.getElementById('west').value = ptLongArr[2];
							document.getElementById('east').value = ptLongArr[1];
						}
					}
					else if (ptLongArr[2] < ptLongArr[1] ) {
						document.getElementById('west').value = ptLongArr[2];
						document.getElementById('east').value = ptLongArr[1];
						tmp = ptLongArr[1];
						ptLongArr[1] = ptLongArr[2];
						ptLongArr[2] = tmp;
					}
					else if (ptLongArr[1] < ptLongArr[2] ) {
						document.getElementById('west').value = ptLongArr[1];
						document.getElementById('east').value = ptLongArr[2];
					}
					else if (ptLongArr[2] == ptLongArr[1] || ptLatArr[2] == ptLatArr[1]) {
						document.getElementById('north').value = '';
						document.getElementById('south').value = '';
						document.getElementById('west').value = '';
						document.getElementById('east').value = '';
					}
					
					var getVars = '?westLong='+ptLongArr[1] + "&eastLong=" + ptLongArr[2] + "&northLat=" + ptLatArr[1] + "&southLat=" + ptLatArr[2];
					var request = new XMLHttpRequest();
								
					request.onreadystatechange = function() {
						if(request.readyState == 4) {
							var reply = request.responseText;
							core_list = reply;							
							var values = reply.split(":",5);
							distance = values[3];
							var core_url = 'http://www.marine-geo.org/database/cores/descriptions/'+ values[0] +'.html';
							core_href = core_url;
							// if distance less than 1 degree from, click point and nearest core
							if (distance < 1) {
								document.getElementById('core_ref_num').value = values[0];
								document.getElementById('lat').value = values[2];
								document.getElementById('long').value = values[1];
								document.getElementById('dist').value = values[3];
								document.getElementById('url').value = core_href;						
							}
							else
							{
								document.getElementById('core_ref_num').value = '';
								document.getElementById('lat').value = '';
								document.getElementById('long').value = '';	
								document.getElementById('dist').value = '';
								document.getElementById('url').value = '';	
							}
						}
					}
					request.open('GET','http://www.marine-geo.org/services/google/ldeo_cores/akm_find_core_from_lat_long_server.php'+getVars);
					request.send(null);
					
				}
			}
		}

		function f_SiteSelect() {
			fnClear();
			switch (document.getElementById("site").value) {
				case '0':	// reset
					document.getElementById('west').value = -180;
        			        document.getElementById('east').value = 180;
					document.getElementById('south').value= -78;
					document.getElementById('north').value= 81;
					break;
				case 'Antarctic':	// everything south of 60S
					document.getElementById('west').value = -180;
					document.getElementById('east').value = 180;
					document.getElementById('south').value = -78;
					document.getElementById('north').value= -60;
					break;
				case 'Arctic':	// everything north of 72N
					document.getElementById('west').value = -180;
					document.getElementById('east').value = 180;
					document.getElementById('south').value= 72;
					document.getElementById('north').value = 81;
					break;
				case 'MARGINS':	// entire world
					document.getElementById('west').value = -180;
					document.getElementById('east').value = 180;
					document.getElementById('south').value = -78;
					document.getElementById('north').value = 81;
					break;
				case 'NJT':
					document.getElementById('west').value = 130;
					document.getElementById('east').value = 138;
					document.getElementById('south').value = 30;
					document.getElementById('north').value =35;
					break;
				case 'CA':
					document.getElementById('west').value = -95;
					document.getElementById('east').value = -80;
					document.getElementById('south').value = 5;
					document.getElementById('north').value = 17;
					break;
				case 'RS':
					document.getElementById('west').value = 30;
					document.getElementById('east').value = 51;
					document.getElementById('south').value = 9;
					document.getElementById('north').value = 33.5;
					break;
				case 'GoC':
					document.getElementById('west').value = -117;
					document.getElementById('east').value = -108;
					document.getElementById('south').value = 22;
					document.getElementById('north').value = 32;
					break;
				case 'PNG':
					document.getElementById('west').value = 134;
					document.getElementById('east').value = 153;
					document.getElementById('south').value = -12;
					document.getElementById('north').value = 0;
					break;
				case 'NZ':
					document.getElementById('west').value = 165;
					document.getElementById('east').value = 190;
					document.getElementById('south').value = -50;
					document.getElementById('north').value = -35;
					break;
				case 'IBM':
					document.getElementById('west').value = 138;
					document.getElementById('east').value = 150;
					document.getElementById('south').value = 10;
					document.getElementById('north').value = 36;
					break;
				case 'Ridge2000':	// entire world
					document.getElementById('west').value = -180;
					document.getElementById('east').value = 180;
					document.getElementById('south').value = -78;
					document.getElementById('north').value = 81;
					break;
				case 'EPR_8-11_N':
					document.getElementById('west').value = -106;
					document.getElementById('east').value = -102;
					document.getElementById('south').value = 8;
					document.getElementById('north').value = 11;
					break;
				case 'JdF-Endeavour':
					document.getElementById('west').value = -129.5;
					document.getElementById('east').value = -128.5;
					document.getElementById('south').value = 47.5;
					document.getElementById('north').value = 48.5;
					break;
				case 'Lau_Basin':
					document.getElementById('west').value = -178;
					document.getElementById('east').value = -173.5;
					document.getElementById('south').value = -23;
					document.getElementById('north').value = -19;
					break;
			}
			var centerInputLat = (parseFloat(document.getElementById('north').value) + parseFloat(document.getElementById('south').value)) / 2.0;
			var centerInputLon = (parseFloat(document.getElementById('east').value) + parseFloat(document.getElementById('west').value)) / 2.0;
			var zoomLevel = map.getBoundsZoomLevel( new GLatLngBounds( new GLatLng( document.getElementById('south').value, document.getElementById('west').value ), new GLatLng( document.getElementById('north').value, document.getElementById('east').value )));
			focusSiteZoom = true;
			map.setCenter( new GLatLng( centerInputLat, centerInputLon ), zoomLevel );
			GEvent.trigger(map, 'click', null, new GPoint(document.getElementById('west').value, document.getElementById('north').value));
			GEvent.trigger(map, 'click', null, new GPoint(document.getElementById('east').value, document.getElementById('south').value));
		}	

		function drawPolylines()
		{
			var mid1_lat = -1;
			var mid1_long = -1;
			var mid2_lat = -1;
			var mid2_long = -1;
			mid1_lat = ptLatArr[1];
			mid1_long = ptLongArr[2];
			mid2_lat = ptLatArr[2];
			mid2_long = ptLongArr[1];
	
			var points = [];
			points.push(new GPoint(ptLongArr[1], ptLatArr[1]));
			points.push(new GPoint(mid1_long, mid1_lat));
			points.push(new GPoint(ptLongArr[2], ptLatArr[2]));
			points.push(new GPoint(mid2_long, mid2_lat));
			points.push(new GPoint(ptLongArr[1], ptLatArr[1]));
			thisEnv = new GPolyline(points, "#000000");
			map.addOverlay(thisEnv);
		}
	
		// clear the form text boxes
		function fnClear()
		{
			num_markers = 0;
			
			map.clearOverlays();
			document.getElementById('lat').value = '';
			document.getElementById('long').value = '';
			document.getElementById('core_ref_num').value = '';
			document.getElementById('dist').value = '';
			document.getElementById('url').value = '';

			document.getElementById('north').value = '';
			document.getElementById('south').value = '';
			document.getElementById('west').value = '';
			document.getElementById('east').value = '';
		}

		function fnRemovePolyline()
		{
			map.removeOverlay(thisEnv);
		}
		
		// submit request to open a new window with clickable url for core description
		function fnSubmit()
		{
			var w = window.open();
			var d = w.document;
			d.open();
			description_url = document.getElementById('url').value;
			d.write('<html>');
			d.write('<head>');
//			d.write('<meta http-equiv=\"refresh\" content=\"0; URL=' + description_url + '\"');
//			d.write(description_url);
			var core_array = core_list.split("\n");
			d.write("<table>");
			d.write("<tr><td>Core_ID&nbsp;&nbsp;</td><td>Longitude&nbsp;&nbsp;</td><td>Latitude&nbsp;&nbsp;</td><td>Depth(m)&nbsp;&nbsp;</td><td>Length(cm)&nbsp;&nbsp;</td><td>Description&nbsp;&nbsp;</td></tr>");
			for ( var i = 0; i < core_array.length - 1; i++ ) {
				
				var values = core_array[i].split(":");
				d.write("<tr><td>" + values[1] + "</td><td>" + values[2] + "</td><td>" + values[3] + "</td><td>" + values[4] + "</td><td>" + values[5]);
				d.write("</td><td><a href=\"" + "http://www.marine-geo.org/database/cores/descriptions/" + values[0] + ".html\" target=\"_blank\">");
				d.write("http://www.marine-geo.org/database/cores/descriptions/" + values[0] + "</a></td></tr>");
//				d.write("<br />");			
			}
			d.write("</table>");
			d.write('</head>');
			d.write('</html>');
			d.close();
		}		
	   //]]>
