var entryImages = [];
var bounds = new GLatLngBounds();
var markerCount = 0;
        
function load(xmlPath)
{
	if (GBrowserIsCompatible()) 
	{
		var map = new GMap2(document.getElementById("map1"));
		var point = new GLatLng(10,10);
		
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		//map.setCenter(point, 16);
		map.setCenter(new GLatLng(0,0),0);
		
		var myEStyle;
		if (IsIE())
			myEStyle = new EStyle("images/maps/stem7.png", new GSize(24,24),  "estyle2", new GPoint(-10,23));
		else
			myEStyle = new EStyle("images/maps/stem7.png", new GSize(24,24),  "estyle2", new GPoint(-10,18));
		
		ew = new EWindow(map, myEStyle);      
		map.addOverlay(ew);

		var baseIcon = new GIcon();
		baseIcon.shadow = "images/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);
	
		function createMarker(point, name, note, url, image, index, imageWidth, imageHeight, trip)
		{	
			var icon = new GIcon();
			
			if (trip == false)
				icon.image = "images/maps/smallerblue.png";
			else
				icon.image = "images/maps/smallerred.png";
			
			icon.shadow = "images/mm_20_shadow.png";
			icon.iconSize = new GSize(12, 12);
			icon.shadowSize = new GSize(18, 16);
			icon.iconAnchor = new GPoint(6, 8);
			icon.infoWindowAnchor = new GPoint(5, 1);
		
			/*var marker = new GMarker(point, icon);
	      
			GEvent.addListener(marker, "click", function()
			{
				marker.openInfoWindowHtml(createWindowHtml(name, note, url, image, imageWidth, imageHeight));
			});
	      
			return marker;*/
			
			var marker = new GMarker(point, icon);
			GEvent.addListener(marker, "click", function() {
			ew.openOnMap(point, createWindowHtml(name, note, url, image, imageWidth, imageHeight), new GPoint(10, 30));
			});
			return marker;
		}
	
		var request = GXmlHttp.create();
		request.open("GET", xmlPath, true);
		
		request.onreadystatechange = function()
		{
			if (request.readyState == 4)
  			{
  				var xmlDoc = request.responseXML;
  				var routes = xmlDoc.getElementsByTagName("route");
	  		
  				for (var i = 0; i < routes.length; i++)
  				{
  					var currentRoute = routes[i];
	  				
  					var floatLatitude = 0;
  					var floatLongitude = 0;  				
  					var name = null;
  					var note = null;
  					var url = null;
  					var image = null;
  					var iWidth = 0;
					var iHeight = 0;
  					var trip = false;
	  				
  					if (currentRoute.attributes['trip'] != null && currentRoute.attributes['trip'].value == "true")
  						trip = true;
  					else
  						trip = false;
	  				
  					for (var j = 0; j < currentRoute.childNodes.length; j++)
  					{
  						if (currentRoute.childNodes[j].tagName == "latitude")
  						{
  							var strLat = currentRoute.childNodes[j].childNodes[0].nodeValue;
  							var latitude = strLat.substring(0, strLat.length - 1);
  							var lastChar = strLat.substring(strLat.length - 1, strLat.length);
	  						
  							if (lastChar == "N")
  							{
  								// greater than zero
  								floatLatitude = parseFloat(latitude);
  							}
  							else if (lastChar == "S")
  							{
  								// less than zero
  								floatLatitude = parseFloat(latitude);
  								floatLatitude = floatLatitude - (floatLatitude * 2);
  							}
  						}
	  					
  						if (currentRoute.childNodes[j].tagName == "longitude")
  						{
  							var strLon = currentRoute.childNodes[j].childNodes[0].nodeValue;
  							var longitude = strLon.substring(0, strLon.length - 1);
  							var lastChar = strLon.substring(strLon.length - 1, strLon.length);
	  						
  							if (lastChar == "E")
  							{
  								// if there is E on the end its a positive number
  								floatLongitude = parseFloat(longitude);
  							}
  							else if (lastChar == "W")
  							{
  								// if there is W on the end it needs to be converted to a negative number
  								floatLongitude = parseFloat(longitude);
  								floatLongitude = floatLongitude - (floatLongitude * 2);
  							}
  						}
	  					
  						if (currentRoute.childNodes[j].tagName == "locations")
  						{
  							var locationsNode = currentRoute.childNodes[j];

  							var locationNode = null;

							for (var k = 0; k < locationsNode.childNodes.length; k++)
							{
								if (locationsNode.childNodes[k].tagName == "location")
								{
									locationNode = locationsNode.childNodes[k];
									break;
								}
							}

							if (locationNode != null)
							{
  								for (var l = 0; l < locationNode.childNodes.length; l++)
  								{
  									if (locationNode.childNodes[l].tagName == "name")
  										name = locationNode.childNodes[l].childNodes[0].nodeValue;
		  									
  									if (locationNode.childNodes[l].tagName == "note")
  										note = locationNode.childNodes[l].childNodes[0].nodeValue;
		  							
  									if (locationNode.childNodes[l].tagName == "url")
  										url = locationNode.childNodes[l].childNodes[0].nodeValue;
	  									
  									if (locationNode.childNodes[l].tagName == "image" && locationNode.childNodes[l].childNodes.length != 0)
  									{
  										image = locationNode.childNodes[l].childNodes[0].nodeValue;
	  									
  										if (locationNode.childNodes[l].attributes['width'] != null && locationNode.childNodes[l].attributes['height'] != null)
  										{
  											iWidth = locationNode.childNodes[l].attributes['width'].value;
  											iHeight = locationNode.childNodes[l].attributes['height'].value;
  										}
	  									
  										var imgPreLoadObj = new Image();
  										imgPreLoadObj.src = image;
  										entryImages.push(imgPreLoadObj);
  									}
  								}
							}
  						}
  					}
	  				
  					if (floatLatitude != 0 && floatLongitude != 0)
  					{
  						var point = new GLatLng(floatLatitude, floatLongitude);
	  					
						map.addOverlay(createMarker(point, name, note, url, image, markerCount, iWidth, iHeight, trip));
						bounds.extend(point);
						markerCount++;
  					}
    			}
  			}
	  		
  			if (bounds != null)
  			{
  				map.setCenter(bounds.getCenter());
	  			
  				if (markerCount > 1)
  					map.setZoom(map.getBoundsZoomLevel(bounds));
  				else
  					map.setZoom(3);
  			}
		}
		
		request.send(null);
	}
}

function createWindowHtml(name, note, url, image, imageWidth, imageHeight)
{
	var html = "<table cellpadding=\"5\" cellspacing=\"0\">";
		
	html += "<tr><td width=\"100%\" class=\"EWTitle\" colspan=2 nowrap>";
	html += name;
	html += "<a href=\"javascript:ew.hide()\"><img width=\"14\" height=\"13\" title=\"Close the EWindow\" src=\"images/maps/eclose.gif\" border=\"0\" style=\"position:absolute;right:4px;top:4px\"></a>";
	html += "</td></tr>";
	
	html += "<tr>";
	
	if (image != null)
	{
		html += "<td>";
		if (imageWidth > 0 && imageHeight > 0)
			html += "<a href=\"" + url + "\"><img src=\"" + image + "\" width=\"" + imageWidth + "\" height=\"" + imageHeight + "\" /></a>";		
		else
			html += "<a href=\"" + url + "\"><img src=\"" + image + "\" /></a>";		
		html += "</td>";
	}
	
	html += "<td valign=\"top\" width=\"250px;\" nowrap>";
	html += "Title: " + note + "";
	html += "<br/>Location: " + name + "";
	html += "<br/><a href=\"" + url + "\">View Entry</a>";
	html += "</td>";
	html += "</tr>";
	html += "</table>";
	
	return html;
}  

function IsIE()
{
	var agent = navigator.userAgent.toLowerCase();
	return (agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1);
}