var Maps = {
	load: function() {
		if (GBrowserIsCompatible()) {
			if(document.getElementById("map")) {
				var map = new GMap2(document.getElementById("map"));
				
				map.addControl(new GSmallMapControl());
				var geocoder = new GClientGeocoder();
				var address = Maps.getAddress();
				geocoder.getLatLng(
					address,
					function(point) {
						map.setCenter(point, 13);
						var marker = new GMarker(point);
						map.addOverlay(marker);
						marker.openInfoWindowHtml(Maps.getInformation());
						GEvent.addListener(marker, "click",
							function() {
								marker.openInfoWindowHtml(Maps.getInformation());
							}
						);
					}
				);
			}
		}
	},
	
	getAddress: function() {
		var container = document.getElementById("main-address");
		var address = "";
		for(var nodes = container.childNodes, len = nodes.length, x = 0; x < len; x++) {
			if(nodes[x].nodeType == 1) {
				if(nodes[x].getAttribute("class") != "postal-code") {
					address += nodes[x].firstChild.nodeValue;
					address += ", ";
				}
			}
		}
		
		if(address.length > 0) {
			address = address.substring(0, address.length - 2);	
		}
		
		return address;
	},
	
	getInformation: function() {
		var infoString = "";
		if(document.getElementById("tournament-location")) {
			infoString += "<h3>" + document.getElementById("tournament-location").innerHTML + "</h3>";
		}
		var address = document.getElementById("main-address");
		var phone = document.getElementById("main-phone");
		var addressForLink = this.getAddress();
		infoString += address.innerHTML + "<br />" + phone.innerHTML + "<br /><a href=\"http://maps.google.com/?q=" + addressForLink + "\" target=\"_blank\">Driving Directions/Print Version</a>";
		
		return infoString;
	}
};

$(function() {Maps.load();});
Event.observe(window, "unload", function() {GUnload();});