// create map object
var map;
// create geocoder object
var geocoder;
// current region
// check the official documentation to change region
// http://code.google.com/apis/maps/documentation/v3/services.html#CountryCodes
// requires a ccTLD (county code top-level domain)
// for a list of ccTLD's visit: http://en.wikipedia.org/wiki/CcTLD
var region = 'uk';

// on dom ready
$(document).ready(function(){

	// confirm delete actions
	if($('.confirm_delete').length) {
		$('.confirm_delete').click(function(){
			return confirm('Are you sure you want to delete this?');
		});
	}

	// if the map_canvas id exists
	if($('#map_canvas').length) {
		// set default lat/lng
		var lat = 54.622978;
		var lng = -2.592773;

		// get pre-populated value and focus map
		var display_marker = false;
		if($('#latitude').length) {
			val = $('#latitude').val()*1;
			if(val != '' && !isNaN(val)) {
				lat = val;
				display_marker = true;
			}
		}
		if($('#longitude').length) {
			val = $('#longitude').val()*1;
			if(val != '' && !isNaN(val)) {
				lng = val;
			}
		}

		// create new geocoder object
		geocoder = new google.maps.Geocoder();

		// create new lat/long object
		var latlng = new google.maps.LatLng(lat,lng);
		// set map options
		var myOptions = {
			zoom: 8,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		// display map
		map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

		if(display_marker) {
			// create infoWindow
			//var contents_str = "<div id='infoWindow'><h1>Testing</h1><p>This is a sample paragraph</p></div>";
			//var infowindow = new google.maps.InfoWindow({
			//	content: contents_str
			//});
			
			// create a map marker
			var marker = new google.maps.Marker({
				map: map,
				position: latlng
			});

			// add click handler
			//google.maps.event.addListener(marker, 'click', function() {
			//	infowindow.open(map,marker);
			//});
		}
	}

	// if the address form field exists
	if($('#address').length) {
		// add an onBlur event
		$('#address').blur(function(){
			// get the entered address
			var address = $(this).val();
			// if address is not empty
			if(address != '') {
				// do the address lookup
				address_lookup(address,region);
			}
		});
	}
});


/**
 * Lookup an address
 * @param string address
 * @param string region
 */
function address_lookup(address,region) {
	// set default region
	if(region==null || region == '') {
		region = 'uk';
	}

	// address not empty
	if(address != '') {
		$('#ajax_msg').html('<p>Loading...</p>');
		// lookup the address
		geocoder.geocode( {'address':address,'region':region}, function(results, status) {
			// if the address was found
			if(status == google.maps.GeocoderStatus.OK) {
				$('#ajax_msg').html('<p>Done</p>');
				// insert lat/long into form
				$('#latitude').val( results[0].geometry.location.lat() );
				$('#longitude').val( results[0].geometry.location.lng() );
				
				// set zoom option
				map.setZoom(10);
				// center the map on the new location
				map.setCenter(results[0].geometry.location);
				
				// create a map marker
				var marker = new google.maps.Marker({
					map: map,
					position: results[0].geometry.location
				});
			} else {
				// display error
				$('#ajax_msg').html('<p>Geocoder failed to retrieve address: '+status+'</p>');
			}
		});
	}
}
