var map;
var geocoder;
var controls = false;

function load(controls, accuracy) {		
	map = new GMap2(document.getElementById("map"));
	//map.setCenter(new GLatLng(34, 0), 1);
	//map.addControl(new GSmallMapControl());
	if (controls) {
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
	}
	
	if (latitude && (latitude == parseFloat(latitude))) { 
		map.setCenter(new GLatLng(latitude, longitude), 15);
		
		point = new GLatLng(latitude,
						longitude);
		//if (accuracy == 0) {
			//use a custom icon if we have one set
			if(map_icon){
				var marker_options = {icon:map_icon, clickable:false};
			}
			else{
				var marker_options = {clickable:false};
			}
			marker = new GMarker(point, marker_options);
			map.addOverlay(marker);
		//}
		
	} else {
		geocoder = new GClientGeocoder();
		//showAddress(entered_address);
		checkAddress(entered_address);
	}
}//END LOAD

function centerMap(response) {
	place = response.Placemark[0];
	//alert (place.objects);
	map.setCenter(new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]), 14);
}

function jumpTo() {
	if (latitude && (latitude == parseFloat(latitude))) {
		map.setCenter(new GLatLng(latitude, longitude), 15);
	} else {
	//	alert("entered address : "+entered_address);
		geocoder.getLocations(entered_address, addAddressToMap);
		//geocoder.getLocations(entered_address, centerMap);
	}
}

// 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, we were unable to geocode that address");
	} else {
		place = response.Placemark[0];
		point = new GLatLng(place.Point.coordinates[1],
							place.Point.coordinates[0]);
		if(map_icon){
			var marker_options = {icon:map_icon, clickable:false};
		}
		else{
			var marker_options = {clickable:false};
		}
		marker = new GMarker(point, marker_options);
		if (!hidepinpoint)
			map.addOverlay(marker);
	}
}

function showAddress(address) {
   if (geocoder) {
	   geocoder.getLatLng(
		 address,
	   function(point) {
		   if (!point) {
			   //alert(address + " not found");
			} else {
				 map.setCenter(point, 15);
				 if(map_icon){
					var marker_options = {icon:map_icon, clickable:false};
				 }
				 else{
					var marker_options = {clickable:false};
				 }
				 var marker = new GMarker(point, marker_options);
				 if (!hidepinpoint)
					 map.addOverlay(marker);
				 //marker.openInfoWindowHtml(address);
		   }
	   }
	  );
	 }
 }

 // findLocation() is used to enter the sample addresses into the form.
function findLocation(address) {
	document.forms[0].q.value = address;
	showLocation();
}

//check to see that the address is valid
function checkAddress(address) {
	if (geocoder) {
		geocoder.getLatLng(address, function(point) {
		   if (!point) {
			   if(document.getElementById('only_suburb')) {
					address = document.getElementById('only_suburb').value;   
			   } else {
					address = suburb_only;
			   }
			   hidepinpoint = true;
			   showAddress(address);
			   
			} else { //it exists
				map.setCenter(point, 15);
				if(map_icon){
					var marker_options = {icon:map_icon, clickable:false};
				}
				else{
					var marker_options = {clickable:false};
				}
				marker = new GMarker(point, marker_options);
				map.addOverlay(marker);
				//marker.openInfoWindowHtml(address);
		   }
	   }
	  );
	}
}

//load the init function
window.addEvent('domready', function() {
	initialize_map();
});

