var map;
var xml;
var geocoder;

function body_onload() {
	map = new GMap2(document.getElementById("map_display"));

	geocoder = new GClientGeocoder();
	GDownloadUrl("/framework/classes/map_points.php", function(data) {
		xml = GXml.parse(data);
		markers = xml.documentElement.getElementsByTagName("marker");
		for (var i = 0; i < markers.length; i++) {
			coords = markers[i].getAttribute("coords");
			shopname = markers[i].getAttribute("name");
			country = markers[i].getAttribute("country");
			address = markers[i].getAttribute("address");
			city = markers[i].getAttribute("city");
			prov = markers[i].getAttribute("province");
			postal = markers[i].getAttribute("postal");
			phone = markers[i].getAttribute("phone");
			email = markers[i].getAttribute("email");
			website = markers[i].getAttribute("website");

			CreateLocations(coords,shopname,country,address,city,prov,postal,phone,website);
		}
	});
	
	map.addMapType(G_SATELLITE_3D_MAP);

	map.addControl(new GHierarchicalMapTypeControl());
	map.addControl(new GLargeMapControl());

	map.setCenter(new GLatLng(44.665676, -96.57505500000001), 3);
}

function CreateLocations(Coords, LName, Country, Address, City, State, zip, phone, WebSite) {

	if (WebSite.length < 8){
		WebSite = '';
	}

	/*if(!Address){
		// Don't display unless they have an address
		alert(LName + " has no address located in " + Country);
	}else*/
	if(!Coords){
		var newLocation = Address + ', ' + City + ', ' + State + ', ' + Country;

		geocoder.getLatLng(newLocation,function(point) {
			if (!point) {
			  //alert(address + " not found");
			} else {
				var freshIcon = new GIcon(G_DEFAULT_ICON);
				freshIcon.image = "http://www.freshgear.com/images/map/marker_freshgear.png";
				/*freshIcon.iconAnchor = new GPoint(0, 0);*/
				freshIcon.infoWindowAnchor = new GPoint(7, 15 );

				var marker = new GMarker(point, freshIcon);

				GEvent.addListener(marker, "click", function() {

					var printAddress = "";
					var printCity = "";
					var printCountry = "";
					var printPhone = "";
					var printWebSite = "";

					if(Address != ""){
						printAddress = "<div class='info_details'>" + Address + "<\/div>";
					}

					if(City != "" && State != ""){
						printCity = "<div class='info_details'>" + City + ", " + State + "<\/div>";
					}else if(State != ""){
						printCity = "<div class='info_details'>" + State + "<\/div>";
					}

					if(zip != "" && Country != ""){
						printCountry = "<div class='info_details'>" + zip + ", " + Country + "<\/div>";
					}else if(Country != ""){
						printCountry = "<div class='info_details'>" + Country + "<\/div>";
					}

					if(phone != ""){
						printPhone = "<div class='info_details'>" + phone + "<\/div>";
					}

					if(WebSite != ""){
						printWebSite = "<div class='info_details'><a target='_newWindow' href='" + WebSite + "'>View Web Site<\/a><\/div>";
					}
					
					marker.openExtInfoWindow(
					  map,
					  "extInfoWindow",
					  "<div>"+
					  "<div class='info_title'>" + LName + "<\/div>"+
					  printAddress+
					  printCity+
					  printCountry+
					  printPhone+
					  printWebSite+
					  "<\/div>",
					  {beakOffset: 3}
					); 
				});
				map.addOverlay(marker);
			}
		});
	} else {

		var LatLon = Coords.split(',');
		var freshIcon = new GIcon(G_DEFAULT_ICON);
		freshIcon.image = "http://www.freshgear.com/images/map/marker_freshgear.png";
		/*freshIcon.iconAnchor = new GPoint(0, 0);*/
		freshIcon.infoWindowAnchor = new GPoint(7, 15 );

		var marker = new GMarker(new GLatLng(LatLon[0],LatLon[1]), freshIcon);

		GEvent.addListener(marker, "click", function() {

			var printAddress = "";
			var printCity = "";
			var printCountry = "";
			var printPhone = "";
			var printWebSite = "";

			if(Address != ""){
				printAddress = "<div class='info_details'>" + Address + "<\/div>";
			}

			if(City != "" && State != ""){
				printCity = "<div class='info_details'>" + City + ", " + State + "<\/div>";
			}else if(State != ""){
				printCity = "<div class='info_details'>" + State + "<\/div>";
			}

			if(zip != "" && Country != ""){
				printCountry = "<div class='info_details'>" + zip + ", " + Country + "<\/div>";
			}else if(Country != ""){
				printCountry = "<div class='info_details'>" + Country + "<\/div>";
			}

			if(phone != ""){
				printPhone = "<div class='info_details'>" + phone + "<\/div>";
			}

			if(WebSite != ""){
				printWebSite = "<div class='info_details'><a target='_newWindow' href='" + WebSite + "'>View Web Site<\/a><\/div>";
			}
			
			marker.openExtInfoWindow(
			  map,
			  "extInfoWindow",
			  "<div>"+
			  "<div class='info_title'>" + LName + "<\/div>"+
			  printAddress+
			  printCity+
			  printCountry+
			  printPhone+
			  printWebSite+
			  "<\/div>",
			  {beakOffset: 3}
			); 
		});
		map.addOverlay(marker);
	}
}

function GoToLocation(Coords, PlaceName) {

	if (Coords != 0) {
		var LatLon = Coords.split(',');
		map.setCenter(new GLatLng(LatLon[0], LatLon[1]), 16);
	}else{
		map.setCenter(new GLatLng(44.665676, -96.57505500000001), 3);
	}

}