$(function() {
	$("body").unload(function() {
		GUnload();
	});
	$('[id^=addressDetails_]').each(function(){
		if ($("#map_" + this.id.split('_')[1]).length > 0) $(this).append('<div><a href="#" id="viewMapLink_' + this.id.split('_')[1] + '">View Map</a></div>');
	});
	$('[id^=viewMapLink_]').each(function(){
		$(this).toggle(function(){
			mapAddress(this.id.split('_')[1]);
			$(this).text("Hide Map");
		},function(){
			$(this).text("View Map");
			$("#map_" + this.id.split('_')[1]).slideUp();
		});
	});
	$(document).ready(function(){
		var count = 0;
		$('[id^=viewMapLink_]').each(function(){
			count++;
			if (count == 1) {
				$(this).click();
			}
		});
		if (window.location.search.substring(1) == 'contactForm') {
			setTimeout(function() {$.scrollTo("#contactForm", 800); }, 1000);
			setTimeout(function() {document.forms["makeEnquiry"].elements[2].focus();}, 1000);
		}
	});
	$("#enquiryButton").click(function(){
		setTimeout(function() {document.forms["makeEnquiry"].elements[2].focus();}, 1000);
	});
});

var map = null;
var geocoder = null;

function mapAddress(addressId) {
	$("#map_" + addressId).slideDown(function(){
		loadMap(addressId);
	});
}

function loadMap(addressId) {
	var mapType = G_PHYSICAL_MAP;
	if ($("#map_" + addressId).hasClass('G_NORMAL_MAP')) mapType = G_NORMAL_MAP;
	if ($("#map_" + addressId).hasClass('G_HYBRID_MAP')) mapType = G_HYBRID_MAP;
	if ($("#map_" + addressId).hasClass('G_SATELLITE_MAP')) mapType = G_SATELLITE_MAP;

	address = $("#fullAddress_" + addressId).html();
	partialAddress = $("#partialAddress_" + addressId).html();
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_" + addressId));
		map.setCenter(new GLatLng(33.870491,151.208922), 13);
		map.addMapType(G_PHYSICAL_MAP)
		map.setMapType(mapType);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GOverviewMapControl());
		geocoder = new GClientGeocoder();
	}
	if (geocoder) {
		geocoder.getLatLng(address, function(point) {
			if (!point) {
				geocoder.getLatLng(partialAddress, function(point) {
					if (!point) {
						// alert("Sorry, that address could not be found!");
					} else {
						map.setCenter(point, 15);
						var marker = new GMarker(point);
						map.addOverlay(marker);
						// marker.openInfoWindowHtml(address);
					}
				});
			} else {
				map.setCenter(point, 15);
				var marker = new GMarker(point);
				map.addOverlay(marker);
				// marker.openInfoWindowHtml(address);
			}
		});
	}
}