/*
 The Organic Supermarket
 Written by Ian Collier, of lidd Consulting on behalf of Atonic Internet Limited
 This work copyright lidd Consulting and Atonic Internet Limited
 Please do not copy or modified this work without seeking the express permission of
 the copyright holder beforehand.
 Thank you
*/

/* google map javascript supporting file*/

// map variable
var map = '';

// load function
function load() {
	// check for browser compatibility
   if (GBrowserIsCompatible()) {
		// new map object
   	map=new GMap2(document.getElementById("map"));
      map.setCenter(new GLatLng(51.502444,-0.132093), 13);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
	}
}

// function to get map
function gmap_geocode_postcode(postcode) {
	var s=document.createElement('script');
	s.src='http://geo.localsearchmaps.com/?cb=gmap_map_postcode&use=google2&country=uk&zip=' + postcode;
	s.type='text/javascript';
	// if map is not null then replace child
	if(document.getElementById('map')!=null) {
		document.getElementById('map').appendChild(s);
	}
}

// function to centre map and add marker
function gmap_map_postcode(lon,lat) {
	// centre map
	map.setCenter(new GLatLng(lon,lat),13);
	// set the map pointer
	// get the map bounds
	var bounds=map.getBounds();
	// get southwest point
	var southWest=bounds.getSouthWest();
	// get northeast point
	var northEast=bounds.getNorthEast();
	// calculate spans
	var lngSpan=northEast.lng()-southWest.lng();
	var latSpan=northEast.lat()-southWest.lat();
	// create point
	var point=new GLatLng(southWest.lat()+latSpan*0.5,southWest.lng()+lngSpan*0.5);
	// add marker
	map.addOverlay(new GMarker(point));
	// add an info balloon
//	map.openInfoWindowHtml(point);		
}

// function to change the google map
function changemap() {
	// get the postcode
	var postcode=document.getElementById("txtpostcode").value;
	// check if postcode is blank
	if(postcode!="") {
    	// update the map
		gmap_geocode_postcode(postcode);		
	}
}
