var map;
var geocoder;
var currentMapLink;
$(document).ready(
	function(){
		$(".mapbox").bind("click", function(){ return loadMap($(this));});
	}
);
$(document).unload( function () { GUnload(); } );

function loadMap(obj){
	currentMapLink = obj;
	
	if (GBrowserIsCompatible()) {
		tb_show( $(obj).attr("title"), "#TB_inline?width=600&height=400&inlineId=mapper" );
		if(typeof(map == "undefined")){
			map = new GMap2($("#map_canvas")[0]);	
			map.addControl(new GLargeMapControl());
			var mapControl = new GHierarchicalMapTypeControl();
			mapControl.clearRelationships();
			// Add control after you've specified the relationships
			map.addControl(mapControl);		
			geocoder = new GClientGeocoder();	
		}
		address = $(currentMapLink).attr("rel").split("|")[0];
		showLocation(address);
		return false; //Don't fire the default fallback link
	}
	return true; //Fire the fallback link
}


// addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(response) {
	map.clearOverlays();
	if (!response || response.Status.code != 200) {
		alert("Sorry, there is no map available for "+$(currentMapLink).attr("title")+" at this time");
	} else {
		place = response.Placemark[0];
		point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);		
		mapzoom = $(currentMapLink).attr("rel").split("|")[1]*1;
		map.setCenter(point, mapzoom);
		marker = new GMarker(point);
		map.addOverlay(marker);
		marker.openInfoWindowHtml("<h3>"+$(currentMapLink).attr("title")+"</h3>"+place.address);
	}
}

// showLocation() is called when you click on the Search button
// in the form.  It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation(address) {
	geocoder.getLocations(address, addAddressToMap);
}



	
