// JavaScript Document
window.onload = function initialize() {
      if (GBrowserIsCompatible()) {
var domain =  document.getElementById("doName").value;
if (domain=="localhost") {
	domain ="localhost/mrtrail";
}
//
// Get map center latitude & longitude
// ** Requires src="http://j.maxmind.com/app/geoip.js in main.php
//
	var lat = geoip_latitude();
	var lng = geoip_longitude();
	var zoomLevel = 7;
              createMap(lat,lng,zoomLevel);
   } else { alert('Your browser is not compatible with Google Maps!');}
}
function createMap(lat,lng,zoomLevel){
var map = new GMap2(document.getElementById("map_canvas"));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.addControl(new GOverviewMapControl()); 
	map.setCenter(new GLatLng(lat, lng), zoomLevel);
//	
// create icon showing latitude and longitude after double click
//
GEvent.addListener(map, "dblclick", function(marker, point) {
  if (marker) {
  } else {
    map.addOverlay(new GMarker(point));
var marker = new GMarker(point);
  map.openInfoWindowHtml(marker.getPoint(),
                  document.createTextNode("Latitude: " + point.lat()  + " Longitude: " + point.lng()));
  }
});
//
// Create a base icon for all of our markers that specifies the
// shadow, icon dimensions, etc.
//
var baseIcon = new GIcon();
baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);
baseIcon.infoShadowAnchor = new GPoint(18, 25);
//
// Creates a marker whose info window displays the letter corresponding
// to the given index.
//
function createMarker(point, index, tnum, tnam, ttype, camp) {
// 1=Hike
// 2=Snow Shoe
// 3=Backpack
// 4=climb
//
var domTyp =  document.getElementById("doType").value;
// var domNam = dom.split(".");
  var icon = new GIcon(baseIcon);
if (ttype == "1") {  icon.image = "img/hikemarker.png"; }
if (ttype == "2") {  icon.image = "img/Smarker.png"; }
if (ttype == "3") {   icon.image = "img/Bmarker.png"; }
if (ttype == "4") {  icon.image = "img/Cmarker.png"; }
if (domTyp == "3" ) {
	if (camp == "Y") {  icon.image = "img/Bmarker.png"; } 
	}
  var marker = new GMarker(point, icon);
  GEvent.addListener(marker, "mouseover", function() {
marker.openInfoWindowHtml("<b>" + tnam + "</b>"  + "<br />" + "<a href=index.php?mod=viewInfo&amp;n=" + tnum + ">View Details</a>");
  });
return marker;
}
//
// Download trail information and load the markers on the map.
//
var domain =  document.getElementById("doName").value;
var typ =  document.getElementById("doType").value;
//alert (typ);
var ptyp = "trail";
//var ptyp = "climb";
if(typ == "4") {
	ptyp = "climb";
}
if (domain=="localhost") {
	domain ="localhost/mrtrail";
}
	var url="http://" +domain +"/admin/" +ptyp +"_info_post.php";

// GDownloadUrl("http://www.mrtrail.com/admin/trail_info_post.php", function(data, responseCode)
 GDownloadUrl(url, function(data, responseCode) 
{
  var xml = GXml.parse(data);
  var markers = xml.documentElement.getElementsByTagName("marker");
var bounds = map.getBounds();
var southWest = bounds.getSouthWest();
var northEast = bounds.getNorthEast();
var lngSpan = northEast.lng() - southWest.lng();
var latSpan = northEast.lat() - southWest.lat(); 
  for (var i = 0; i < markers.length; i++) {
    var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                            parseFloat(markers[i].getAttribute("lng")));
	var tnam = (markers[i].getAttribute("nam"));
	var tnum = (markers[i].getAttribute("num"));
	var camp = (markers[i].getAttribute("camp"));
	var ttype = (markers[i].getAttribute("type"));
var domTyp =  document.getElementById("doType").value;
//var domNam = dom.split(".");
var good ="1";
// 1=Hike
// 2=Snow Shoe
// 3=Backpack
// 4=climb
if (domTyp == "1" ) 
{ if (ttype != "1") {good = null;}
}
if (domTyp == "2") 
{ if (ttype != "2") {  good = null; }
}
if (domTyp == "3" ) 
{ if (ttype != "3") {  good = null; }
}
if (domTyp == "4" ) 
{ if (ttype != "4") {  good = null; }
}
if (domTyp == "3" ) 
{ if (camp == "Y") {  good = "1"; }
}
if (domTyp == null ) 
{ good = "1";
}
if (good != null)
{
	 map.addOverlay(createMarker(point, i, tnum, tnam, ttype, camp));	
}
  }
});
}
