// JS code for Google Maps plugin.


function mt_insert_map_code(divId, urlStr, textSpanId, showMapText, hideMapText)
{
  var mapDiv = document.getElementById(divId);
  var textSpan = document.getElementById(textSpanId);
  var currentDisp = mapDiv.style.display;
  if('block' == mapDiv.style.display) {
    mapDiv.style.display = 'none';
    textSpan.innerHTML = showMapText;
  }
  else
  {
    mapDiv.style.display = 'block';
    textSpan.innerHTML = hideMapText;
    if('' == mapDiv.innerHTML) {

      var map = new GMap2(mapDiv, {size:new GSize(650,650)});
      map.clearOverlays();
      map.addControl(new GLargeMapControl());
      map.addControl(new GMenuMapTypeControl());
      map.addMapType(G_PHYSICAL_MAP);

      LoadGPXFileIntoGoogleMap2(map, urlStr, G_PHYSICAL_MAP);

    }
  }
}

function LoadGPXFileIntoGoogleMap2(map, filename, maptype)
{
  var mapPainter = null;
  var tracks = [];
  var waypoints = [];
  var parser = null;

  var request = GXmlHttp.create();
  request.open("GET", filename, true);
  request.onreadystatechange = function()
  {
    if (request.readyState == 4)
    {
      var gpxDoc = request.responseXML;
      if( !gpxDoc ) {
        alert("Could not load GPX document " + filename);
      } else if( !gpxDoc.documentElement ) {
        alert("Document " + filename + "\nwas not recognized by the XML loader");
//      } else if( gpxDoc.documentElement.childNodes.length < 3 ) {
//        alert("The XML loader could not parse document " + filename);
      } else {
        parser = new GMAPGpx(request.responseXML, 5);
        tracks = parser.GetTracks();
        waypoints = parser.GetWaypoints();

        mapPainter = new GMAPPainter(map);


        var gi = null;
  
        gi = new GIcon(G_DEFAULT_ICON);
        gi.image = "http://www.windy.gen.nz/wp-content/plugins/miketest/icons/flag_icon.png";
        gi.iconSize = new GSize(30,29);
        gi.shadow = null;
        gi.shadowSize = null;
        gi.iconAnchor = new GPoint(10,29);
  //      gi.maxHeight = 0;
        mapPainter.SetSymbolIcon("Flag", gi);
  
        gi = new GIcon(G_DEFAULT_ICON);
        gi.image = "http://www.windy.gen.nz/wp-content/plugins/miketest/icons/hut_icon_2.png";
        gi.iconSize = new GSize(25,23);
        gi.shadow = null;
        gi.shadowSize = null;
        gi.iconAnchor = new GPoint(12,23);
  //      gi.maxHeight = 0;
        mapPainter.SetSymbolIcon("Hut", gi);
  
        gi = new GIcon(G_DEFAULT_ICON);
        gi.image = "http://www.windy.gen.nz/wp-content/plugins/miketest/icons/campsite_icon.png";
        gi.iconSize = new GSize(30,38);
        gi.shadow = null;
        gi.shadowSize = null;
        gi.iconAnchor = new GPoint(14,25);
  //      gi.maxHeight = 0;
        mapPainter.SetSymbolIcon("Campsite", gi);
  
        gi = new GIcon(G_DEFAULT_ICON);
        gi.image = "http://www.windy.gen.nz/wp-content/plugins/miketest/icons/car_icon_3.png";
        gi.iconSize = new GSize(36,18);
        gi.shadow = null;
        gi.shadowSize = null;
        gi.iconAnchor = new GPoint(18,18);
  //      gi.maxHeight = 0;
        mapPainter.SetSymbolIcon("Road end", gi);
  
        gi = new GIcon(G_DEFAULT_ICON);
        gi.image = "http://www.windy.gen.nz/wp-content/plugins/miketest/icons/peak_icon.png";
        gi.iconSize = new GSize(30,18);
        gi.shadow = null;
        gi.shadowSize = null;
        gi.iconAnchor = new GPoint(15,18);
  //      gi.maxHeight = 0;
        mapPainter.SetSymbolIcon("Peak", gi);

        var trackColours = ["#ff0000", "#008000"];
        for(var n = 0; n < tracks.length; n++)
        {
          mapPainter.PaintTrack(tracks[n], trackColours[n % 2], 5);
        }
        for(var o = 0; o < waypoints.length; o++)
        {
          mapPainter.PaintWaypoint(waypoints[o]);
        }
        mapPainter.CentreZoom(maptype);

        map.addControl(new GMAPDisplayControl(mapPainter, true, true, '50%'));
      }
    }
  }
  request.send(null);
}


function mt_insert_map_code_old(divId, urlStr, textSpanId, showMapText, hideMapText)
{
  var innerDiv = document.getElementById(divId);
  var textSpan = document.getElementById(textSpanId);
  var currentDisp = innerDiv.style.display;
  if('block' == innerDiv.style.display) {
    innerDiv.style.display = 'none';
    textSpan.innerHTML = showMapText;
  }
  else
  {
    innerDiv.style.display = 'block';
    textSpan.innerHTML = hideMapText;
    if('' == innerDiv.innerHTML) {

      var map = new GMap2(innerDiv, {size:new GSize(400,400)});
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.addMapType(G_PHYSICAL_MAP);
      LoadGPXFileIntoGoogleMap(map, urlStr, G_PHYSICAL_MAP);

    }
  }
}

		function LoadGPXFileIntoGoogleMap(map, filename, maptype)
		{
			// Remove any existing overlays from the map.
			map.clearOverlays();

			var request = GXmlHttp.create();
			request.open("GET", filename, true);
			request.onreadystatechange = function()
			{
				if (request.readyState == 4)
				{
					var gpxDoc = request.responseXML;
					if( !gpxDoc ) {
						alert("Could not load GPX document " + filename);
					} else if( !gpxDoc.documentElement ) {
						alert("Document " + filename + "\nwas not recognized by the XML loader");
					} else if( gpxDoc.documentElement.childNodes.length < 3 ) {
						alert("The XML loader could not parse document " + filename);
					} else {
						parser = new GPXParser(request.responseXML, map);
						parser.SetTrackColour("#ff0000");					// Set the track line colour
						parser.SetTrackWidth(5);							// Set the track line width
						parser.SetMinTrackPointDelta(0.0001);				// Set the minimum distance between track points
						parser.CenterAndZoom(request.responseXML, maptype); // Center and Zoom the map over all the points.
						parser.AddTrackpointsToMap();						// Add the trackpoints
						parser.AddWaypointsToMap();							// Add the waypoints
					}
				}
			}
			request.send(null);
		}

// Load the google maps api.
//google.load("maps", "2");

