var map;
var bounds, sample_kml;

function load() {
map = new GMap2(document.getElementById("map"));

bounds = new GLatLngBounds();
		
//Set the initial center point (this will be adusted at the end to the bounds of the data points selected)
var centerPoint = new GLatLng(39.096,-4.219);
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	//map.addControl(new GScaleControl());
	//map.addControl(new GOverviewMapControl());
	
	map.addMapType(G_PHYSICAL_MAP);
	
	//Add custom control for Search Box
	if (document.getElementById("search-wrapper")) {
		var addControl = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7,28));
	document.getElementById("search-wrapper").style.display="block";
	addControl.apply(document.getElementById("search-wrapper"));
	map.getContainer().appendChild(document.getElementById("search-wrapper"));
	}
	
	map.enableDoubleClickZoom();
	map.setCenter(centerPoint, 5);
	
	GEvent.addListener(map, "click",findPlaces);
	
}

//Google Maps Geocoder
var geocoder = new GClientGeocoder();  //instantiate the geocoder
var place, country, region, locality, thoroughfare;

//Use client-side geocoding to see if there is a result
function geocodeAddress() {
var address = document.getElementById('search-box').value;
geocoder.getLocations(address, getAddress);
}

function getAddress(response) {
	if (!response || response.Status.code != 200) {
		alert("Sorry, that address didn't work.\nTry using a different address.");
		return false;
	} else {
	place = response.Placemark[0];
		var lat = place.Point.coordinates[1];
		var lng = place.Point.coordinates[0];
		var new_location = new GLatLng(lat,lng);
		map.setCenter(new_location, 5);
	}
	
}


function findPlaces(ol,point) {
	var lat = point.lat();
	var lng = point.lng();
	
	document.getElementById("search-results").innerHTML = '<div id="search-results-loading">Working...</div>';
	
	$("#search-results").load("findplaces.php?lat="+lat+"&lng="+lng);
	
}

function findBooks(place,sortby) {

	document.getElementById("search-results").innerHTML = '<div id="search-results-loading">Working...</div>';
	
	$("#search-results").load("findbooks.php?q="+place+"&s="+sortby);
	
}


function getBook(asin) {

	document.getElementById("search-details").innerHTML = '<div id="search-details-loading">Working...</div>';
	
	$("#search-details").load("getbook.php?q="+asin);
	
}


function showCountryIndex() {
	
	document.getElementById("overlay-wrapper").style.display = "block";
	
	document.getElementById("overlay-div").innerHTML = '<div style="display:block; height: 30px; width: 100%; background: #ffffff;"></div><div id="search-details-loading">Working...</div>';
	
	$("#overlay-div").load("countriesindex.php");
}