var map;
var fulladdress;
var type, company, name, address, city, state, zip, date, phone, email, url, latitude, longitude;
var gmarkers = [];
var html = [];
var to_htmls = [];
var from_htmls = [];

var redicon = new GIcon();
redicon.image = "../images/redmarker.png";
redicon.shadow = "../images/marker_shadow.png";
redicon.iconSize = new GSize(20, 26);
redicon.shadowSize = new GSize(38, 26);
redicon.iconAnchor = new GPoint(10, 26);
redicon.infoWindowAnchor = new GPoint(10, 1);

var yellowicon = new GIcon();
yellowicon.image = "../images/yellowmarker.png";
yellowicon.shadow = "../images/marker_shadow.png";
yellowicon.iconSize = new GSize(20, 26);
yellowicon.shadowSize = new GSize(38, 26);
yellowicon.iconAnchor = new GPoint(10, 26);
yellowicon.infoWindowAnchor = new GPoint(10, 1);

var greenicon = new GIcon();
greenicon.image = "../images/greenmarker.png";
greenicon.shadow = "../images/marker_shadow.png";
greenicon.iconSize = new GSize(20, 26);
greenicon.shadowSize = new GSize(38, 26);
greenicon.iconAnchor = new GPoint(10, 26);
greenicon.infoWindowAnchor = new GPoint(10, 1);

var blueicon = new GIcon();
blueicon.image = "../images/bluemarker.png";
blueicon.shadow = "../images/marker_shadow.png";
blueicon.iconSize = new GSize(20, 26);
blueicon.shadowSize = new GSize(38, 26);
blueicon.iconAnchor = new GPoint(10, 26);
blueicon.infoWindowAnchor = new GPoint(10, 1);

function myclick(k) {gmarkers[k].openInfoWindowHtml(html[k]);}
function tohere(k) {gmarkers[k].openInfoWindowHtml(to_htmls[k]);}
function fromhere(k) {gmarkers[k].openInfoWindowHtml(from_htmls[k]);}

function load() {
	if (GBrowserIsCompatible()) {
		var i = 0;
	
		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl());
		map.setCenter(new GLatLng(43.094994, -84.008507), 8);

                var circle;
                var center = new GLatLng(43.094994, -84.008507);
				var circlePoints = Array();
				
				with (Math) {
					var rLat = (20/3963.189) * (180/PI);
					var rLng = rLat/cos(center.lat() * (PI/180));

					for (var a = 0 ; a < 361 ; a+=1 ) {
						var aRad = a*(PI/180);
						var x = center.lng() + (rLng * cos(aRad));
						var y = center.lat() + (rLat * sin(aRad));
						var point = new GLatLng(parseFloat(y),parseFloat(x));
						circlePoints.push(point);
					}
                }

                map.removeOverlay(circle);
                circle = new GPolygon(circlePoints, '#FFCC00', 2, 1, '#FFCC00', 0.2);	
				map.addOverlay(circle);
		
		function createMarker(point, html, type) {
			switch (type) {
				case "service_providers": var marker = new GMarker(point, yellowicon); break
				case "events": var marker = new GMarker(point, greenicon); break
				default: var marker = new GMarker(point, yellowicon);
			}
			
			to_htmls[i] = html + '<br><font class="text_small">Directions: <b>To here</b> - <a style="color:#0000FF;" href="javascript:fromhere(' + i + ')">From here</a>' +
			'<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
			'<input type="text" SIZE=40 MAXLENGTH=80 name="saddr" id="saddr" value="" /><br>' +
			'<INPUT value="Get Directions" TYPE="SUBMIT">' +
			'<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + 
			'"/></font>';

			from_htmls[i] = html + '<br><font class="text_small">Directions: <a style="color:#0000FF;" href="javascript:tohere(' + i + ')">To here</a> - <b>From here</b>' +
			'<br>End address:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
			'<input type="text" SIZE=40 MAXLENGTH=80 name="daddr" id="daddr" value="" /><br>' +
			'<INPUT value="Get Directions" TYPE="SUBMIT">' +
			'<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
			'"/></font>';

			html = html + '<br><font class="text_small">Directions: <a style="color:#0000FF;" href="javascript:tohere('+i+')">To here</a> - <a style="color:#0000FF;" href="javascript:fromhere('+i+')">From here</a></font>';
			
			GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});
			gmarkers[i] = marker;
			html[i] = html;
			i++;
			return marker;
		}
		
		var process_it = function(xml) {
			var xml = GXml.parse(xml);
			var records = xml.documentElement.getElementsByTagName("record");
			var type = xml.documentElement.getElementsByTagName("type").item(0).firstChild.data;
			
			switch (type) {
				case "service_providers":
					for (var j = 0; j < records.length; j++) {
						latitude = parseFloat(records[j].getElementsByTagName("latitude").item(0).firstChild.data);
						longitude = parseFloat(records[j].getElementsByTagName("longitude").item(0).firstChild.data);
						name = records[j].getElementsByTagName("name").item(0).firstChild.data;
						address = records[j].getElementsByTagName("address").item(0).firstChild.data;
						city = records[j].getElementsByTagName("city").item(0).firstChild.data;
						state = records[j].getElementsByTagName("state").item(0).firstChild.data;
						zip = records[j].getElementsByTagName("zip").item(0).firstChild.data;
						phone = records[j].getElementsByTagName("phone").item(0).firstChild.data;
						email = records[j].getElementsByTagName("email").item(0).firstChild.data;
						url = records[j].getElementsByTagName("url").item(0).firstChild.data;
				
						html = "<font class='text_small'>";
				
						if (name != "-") {
							html = html + "<b>" + name + "</b>";
						}
				
						if (address != "-") {
							if (city != "-") {
								if (state != "-") {
									if (zip != "-") {
										html = html + "<br>" + address + "<br>" + city + ", " + state + " " + zip;
									}
								}
							}
						}
				
						if (phone != "-") {
							html = html + "<br>" + phone;
						}
				
						if (email != "-") {
							html = html + "<br>Email: <a href='mailto:" + email + "'>" + email + "</a>";
						}
				
						if (url != "-") {
							html = html + "<br>Website: <a href='http://" + url + "'>" + url + "</a>";
						}
				
						html = html + "</font>";
				
						var point = new GLatLng(latitude, longitude);
						var marker = createMarker(point, html, type);
						map.addOverlay(marker); 
					} break
				case "events":
					for (var j = 0; j < records.length; j++) {
						latitude = parseFloat(records[j].getElementsByTagName("latitude").item(0).firstChild.data);
						longitude = parseFloat(records[j].getElementsByTagName("longitude").item(0).firstChild.data);
						name = records[j].getElementsByTagName("name").item(0).firstChild.data;
						address = records[j].getElementsByTagName("address").item(0).firstChild.data;
						city = records[j].getElementsByTagName("city").item(0).firstChild.data;
						state = records[j].getElementsByTagName("state").item(0).firstChild.data;
						zip = records[j].getElementsByTagName("zip").item(0).firstChild.data;
						date = records[j].getElementsByTagName("date").item(0).firstChild.data;
						phone = records[j].getElementsByTagName("phone").item(0).firstChild.data;
						email = records[j].getElementsByTagName("email").item(0).firstChild.data;
						url = records[j].getElementsByTagName("url").item(0).firstChild.data;
						
						html = "<font class='text_small'>";
				
						if (name != "-") {
							html = html + "<b>" + name + "</b>";
						}
						
						if (date != "-") {
							html = html + "<br><font color=#0000CC>" + date + "</font>";
						}
				
						if (address != "-") {
							if (city != "-") {
								if (state != "-") {
									if (zip != "-") {
										html = html + "<br>" + address + "<br>" + city + ", " + state + " " + zip;
									}
								}
							}
						}
				
						if (phone != "-") {
							html = html + "<br>" +phone;
						}
				
						if (email != "-") {
							html = html + "<br>Email: <a href='mailto:" + email + "'>" + email + "</a>";
						}
				
						if (url != "-") {
							html = html + "<br>Website: <a href='http://" + url + "' target='0'>" + url + "</a>";
						}
				
						html = html + "</font>";
				
						var point = new GLatLng(latitude, longitude);
						var marker = createMarker(point, html, type);
						map.addOverlay(marker);
					} break
			}
		}
		GDownloadUrl("xml/events.xml", process_it);
		GDownloadUrl("xml/service_providers.xml", process_it);
	}
}
