var WaypointType = { Parking:0, Trail:1, Area:2, Boulder:3, Camping:4 };
var parkingIcon;
var campingIcon;
var destIcon;
var foodIcon;
var star0;
var star1;
var star2;
var star3;
var star4;
var star5;
var constructIcon;
var limitedIcon;
var externalIcon;
var internetIcon;
var mainMap;
var zoomBounds;
var markerLabels = new Array();
var labelIndex = 0;

function initSymbols()
{
	destIcon = new GIcon();
	destIcon.image = "resource/gps/destination.png";
	destIcon.iconSize = new GSize(20, 20);
	destIcon.iconAnchor = new GPoint(10, 10);
	destIcon.infoWindowAnchor = new GPoint(10, 10);

	star0 = new GIcon();
	star0.image = "resource/gps/0star.png";
	star0.iconSize = new GSize(10, 10);
	star0.iconAnchor = new GPoint(5, 5);
	star0.infoWindowAnchor = new GPoint(5, 5);

	star1 = new GIcon();
	star1.image = "resource/gps/1star.png";
	star1.iconSize = new GSize(10, 10);
	star1.iconAnchor = new GPoint(5, 5);
	star1.infoWindowAnchor = new GPoint(5, 5);

	star2 = new GIcon();
	star2.image = "resource/gps/2star.png";
	star2.iconSize = new GSize(14, 14);
	star2.iconAnchor = new GPoint(7, 7);
	star2.infoWindowAnchor = new GPoint(7, 7);

	star3 = new GIcon();
	star3.image = "resource/gps/3star.png";
	star3.iconSize = new GSize(18, 18);
	star3.iconAnchor = new GPoint(9, 9);
	star3.infoWindowAnchor = new GPoint(9, 9);

	star4 = new GIcon();
	star4.image = "resource/gps/4star.png";
	star4.iconSize = new GSize(22, 22);
	star4.iconAnchor = new GPoint(11, 11);
	star4.infoWindowAnchor = new GPoint(11, 11);

	star5 = new GIcon();
	star5.image = "resource/gps/5star.png";
	star5.iconSize = new GSize(26, 26);
	star5.iconAnchor = new GPoint(13, 13);
	star5.infoWindowAnchor = new GPoint(13, 13);

	constructIcon = new GIcon();
	constructIcon.image = "resource/gps/construction.png";
	constructIcon.iconSize = new GSize(20, 20);
	constructIcon.iconAnchor = new GPoint(10, 10);
	constructIcon.infoWindowAnchor = new GPoint(10, 10);

	foodIcon = new GIcon();
	foodIcon.image = "resource/gps/food.png";
	foodIcon.iconSize = new GSize(20, 20);
	foodIcon.iconAnchor = new GPoint(10, 10);
	foodIcon.infoWindowAnchor = new GPoint(10, 10);

	limitedIcon = new GIcon();
	limitedIcon.image = "resource/gps/limited.png";
	limitedIcon.iconSize = new GSize(20, 20);
	limitedIcon.iconAnchor = new GPoint(10, 10);
	limitedIcon.infoWindowAnchor = new GPoint(10, 10);

	externalIcon = new GIcon();
	externalIcon.image = "resource/gps/external.png";
	externalIcon.iconSize = new GSize(20, 20);
	externalIcon.iconAnchor = new GPoint(10, 10);
	externalIcon.infoWindowAnchor = new GPoint(10, 10);
	
	parkingIcon = new GIcon();
	parkingIcon.image = "resource/gps/parking.png";
	parkingIcon.iconSize = new GSize( 20, 20 );
	parkingIcon.iconAnchor 	 = new GPoint( 10, 10 );
	parkingIcon.infoWindowAnchor = new GPoint( 10, 5 );
	
	campingIcon = new GIcon();
	campingIcon.image = "resource/gps/camping.png";
	campingIcon.iconSize = new GSize( 20, 20 );
	campingIcon.iconAnchor 	 = new GPoint( 10, 10 );
	campingIcon.infoWindowAnchor = new GPoint( 10, 10 );

	internetIcon = new GIcon();
	internetIcon.image = "resource/gps/internet.png";
	internetIcon.iconSize = new GSize( 20, 20 );
	internetIcon.iconAnchor 	 = new GPoint( 10, 10 );
	internetIcon.infoWindowAnchor = new GPoint( 10, 10 );

	zoomBounds = new GLatLngBounds();
}

function getWaypointType( symbolName )
{
	var result = WaypointType.Boulder;

	if( symbolName == "Parking Area" ) { result = WaypointType.Parking; }
	else if( symbolName == "Trail Head" ) { result = WaypointType.Trail; }
	else if( symbolName == "Summit" ) { result = WaypointType.Area; }
	else if( symbolName == "Campground" ) { result = WaypointType.Camping; }
	else if( symbolName == "Telephone" ) { result = WaypointType.Internet; }
	
	return result;
}

function showWaypoint( inDetail, inWaypointType )
{
	var result = 0;

	// If we are showing details (approach map), we only want to show the parking and area markers.
//	if( inDetail == 1 )
//	{
//		if( inWaypointType == WaypointType.Parking || inWaypointType == WaypointType.Area )
//		{
//			result = 1;
//		}
//	}
	// If we are showing the location map, we want to show everything by the area markers.
//	else if( inWaypointType != WaypointType.Area )
//	{
		result = 1;
//	}
	
	return result;
}

function chooseIcon( inWaypointType )
{
	var result = destIcon;

	if( inWaypointType == WaypointType.Parking ) { result = parkingIcon; }
	else if( inWaypointType == WaypointType.Camping ) { result = campingIcon; }
	else if( inWaypointType == WaypointType.Internet ) { result = internetIcon; }

	return result;
}

function chooseLineStyle( inName )
{
	var result = 0;

	if( inName.indexOf( "Approach" ) == 0 )
	{
		result = new Array( "#4012FE", 3, .5 ); // Color, Width, Opacity
	}
	else
	{
		result = new Array( "#4012FE", 1.5, .5 );
	}
	
	return result;
}

// *****************************************************
// *****************************************************
// Callback function for the markers to set their z-index
//
function markerOrder( marker )
{
	return 0; // returning 0 will just put them in the order they are added,
	          // which is controlled by the SQL query on the index page.
}

// *****************************************************
// *****************************************************
// Internal function to create a marker with pop-up text
//
function createMarker(point, icon, label, inHtml, inNodeId)
{
  if( label )
  {
		var testLabel = new ELabel( new GLatLng( point.y, point.x ), label, "label", new GSize( 12, 10 ), 100, true );
		markerLabels[labelIndex] = testLabel;
		mainMap.addOverlay( testLabel );
		zoomBounds.extend( new GLatLng( point.y, point.x ) );
	}

	var markerOptions = { 
		icon: icon,
		zIndexProcess: markerOrder
	};

  var marker = new GMarker(point, markerOptions);
  var currentIndex = labelIndex;
  if( inHtml )
	{
		marker.bindInfoWindowHtml( inHtml );
	}
  else if( inNodeId )
  {
		marker.bindInfoWindow( document.getElementById( inNodeId ) );
	}
//  GEvent.addListener( marker, "click", function()
//  {
//		marker.openInfoWindowHtml( document.getElementById( inNodeId ).innerHTML );
//	} );

  GEvent.addListener( marker, "mouseover", function() { markerLabels[currentIndex].show(); } );
  GEvent.addListener( marker, "mouseout", function() { markerLabels[currentIndex].hide(); } );
  mainMap.addOverlay(marker);
 
 if( label )
 {
		labelIndex++;
	}
  return marker;
}

//	Load and parse GPX file
//	Builds .gpx filename based on html file
function loadGPS( gpsName, inDetail )
{
//	initSymbols();

	// Create map object
//	mainMap = new GMap2( document.getElementById( "map" ) );
	//mainMap.addControl(new GLargeMapControl());
	//mainMap.addControl(new GMapTypeControl());
	//mainMap.setCenter(new GLatLng(37.4419, -122.1419), 13);
//	mainMap.setUIToDefault();

	var request = GXmlHttp.create();
	var URL = gpsName;
//	URL = URL.replace(/\.html$/,"");
//	URL = URL.concat(".xml");
	request.open( "GET", URL, true );
	request.onreadystatechange = function()
	{
		if( request.readyState == 4 )
		{
			var gpxDoc = null;
			var textLength = parseFloat( request.responseText.length );
			if( textLength > 0 )
			{
				gpxDoc = GXml.parse(request.responseText);
			}
			else
			{
				gpxDoc = request.responseXML;
			}

			if( !gpxDoc )
			{
				// alert("Could not load GPX document " + URL);
			}
			else if( !gpxDoc.documentElement )
			{
				alert("Document " + URL + "\nwas not recognized by the XML loader");
			}
			else if( gpxDoc.documentElement.childNodes.length < 3 )
			{
				// alert("The XML loader could not parse document " + URL);
			}
			else
			{
				//	Parse waypoints in <wpt></wpt> tags
				var tname = "";
				var wpt;
				wpt = gpxDoc.documentElement.getElementsByTagName("wpt")
				if( wpt )
				{
					for( var i = 0; i < wpt.length; i++ )
					{
						var waypointType = WaypointType.Boulder;
						var symElement = wpt[i].getElementsByTagName( "sym" );
						if( symElement.length )
						{
							waypointType = getWaypointType( symElement[0].firstChild.nodeValue );
						}

						if( showWaypoint( inDetail, waypointType ) )
						{
							point = new GPoint( parseFloat(wpt[i].getAttribute("lon")), parseFloat(wpt[i].getAttribute("lat")));

							// Extract <name></name> tag
							var tlabel = "";
							element = wpt[i].getElementsByTagName( "name" );
							if( element.length )
							{
								tname = element[0].firstChild.nodeValue;
								tlabel += "<p><b>" + element[0].firstChild.nodeValue + "</b><p>";
							}

							tlabel += "<p>Latitude, longitude<br/> " + wpt[i].getAttribute("lat") + ", " + wpt[i].getAttribute("lon") + "</p>";
							tlabel += "<p>";

							element = wpt[i].getElementsByTagName("cmt");
							if( element.length )
							{
								tlabel += element[0].firstChild.nodeValue;
								tlabel += "<br/>";
							}

							if( waypointType != WaypointType.Area )
							{
								tlabel += "<a target='new' href='http://maps.google.com/maps?f=d&source=s_d&saddr=&daddr=" +
									wpt[i].getAttribute("lat") + ",+" + wpt[i].getAttribute("lon") + "'>Get Directions</a>";

								element = wpt[i].getElementsByTagName("link");
								if( element.length )
								{
									tlabel += " • <a href=\"" + element[0].getAttribute("href") + "\" target=\"_blank\">Website</a>";
								}
							}

							tlabel += "</p>";

							var mapIcon = chooseIcon( waypointType );
							createMarker( point, mapIcon, tname, tlabel, "" );
						}
					}
				}
				wpt = [];

				//	Parse routes in <rte></rte>
//				if( inDetail == 1 )
				{
					var rte = gpxDoc.documentElement.getElementsByTagName("trk");
					if( rte )
					{
						for( var i = 0; i < rte.length; i++ )
						{
							tcolor="#4012FE";
							twidth=3;
							topacity=.5;

							// Extract <name></name> tag
							element = rte[i].getElementsByTagName("name");
							if( element.length )
							{
								if( element[0].firstChild )
								{
									array = chooseLineStyle( element[0].firstChild.nodeValue );
									tcolor = array[0];
									twidth = array[1];
									topacity = array[2];
								}
							}

							// Process each point of each route, string them into a single polyline
							points = [];
							var rtept = rte[i].getElementsByTagName("trkpt");
							for (var k = 0; k < rtept.length; k++)
							{
								points.push( new GLatLng( parseFloat( rtept[k].getAttribute( "lat" ) ),
									parseFloat( rtept[k].getAttribute( "lon" ) ) ) );
							}
							mainMap.addOverlay(new GPolyline(points, tcolor, twidth, topacity));
							points = [];
						}
						rte = []
					}
				}
			} // gpxDoc

			var zoomLevel = mainMap.getBoundsZoomLevel( zoomBounds ) - 1;
			if( zoomLevel > 15 )
			{
				zoomLevel = 15;
			}
			// We need to do this here, because it's the last thing that gets processed due to the HTTP callback.
			mainMap.setCenter( zoomBounds.getCenter(), zoomLevel );
		} // readyState
	} // function

	request.send(null);
}

