var map; 

// var localSearch = new GlocalSearch();

var curpoint;
var geocoder;


var icon = new GIcon();
icon.image = "http://www.google.com/mapfiles/marker.png";
icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
icon.iconSize = new GSize(20, 34);
icon.shadowSize = new GSize(37, 34);
icon.iconAnchor = new GPoint(10, 34);


  function initialize_x() { 

    if (GBrowserIsCompatible()) { 
       map = new GMap2(document.getElementById("map")); 
       map.setCenter(new GLatLng(54.0, 0.5), 5); 
       map.setUIToDefault(); 
     } 
   } 



function initialize() 
	{ 
	  var latlng = new google.maps.LatLng(51.657, -0.3775);     
	  var myOptions = { zoom: 15,  center: latlng,  mapTypeId: google.maps.MapTypeId.ROADMAP };     
	  map = new google.maps.Map(document.getElementById("map"), myOptions);   
	  geocoder = new google.maps.Geocoder();
	}  


  function codeAddress(pc) 
  {

   // alert(pc);

   var address = pc + ", UK"; // document.getElementById("address").value; 
   geocoder.geocode( {'address': address}, 
   function(results, status) 
   	{ if (status == google.maps.GeocoderStatus.OK) 
   		{ map.setCenter(results[0].geometry.location);
		   var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location });       
		 } 
		 else 
		 { 
		 	alert("Geocode was not successful for the following reason: " + status);       
		 } 
	 });   
} 




function usePointFromPostcode(postcode, callbackFunction) {

	localSearch.setSearchCompleteCallback(null, 
		function() {
			if (localSearch.results[0])
			{		
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				var point = new GLatLng(resultLat,resultLng);
				curpoint = point;
				callbackFunction(point);
			} else{
				alert("Postcode not found!");
			}

		});	

		

	localSearch.execute(postcode + ", UK");

}



function setCenterToPoint(point){

	map.setCenter(point, 14);

	placeMarkerAtPoint(point);

}



function placeMarkerAtPoint(point){

	var MarkOpts = {title: "Marker for Event Location",clickable: true,icon: icon};

	var marker = new GMarker(point,MarkOpts);

	map.addOverlay(marker);

}



function showPointLatLng(point){

	alert("Latitude: " + point.lat() + "\nLongitude: " + point.lng());

}

	

 function SetPostCodeMap(Cell) {

  var postcode = Cell.innerHTML;

  var calpc = Cell.parentNode.childNodes[3].innerHTML; 

    

if (calpc.length > 6)

{

  var t = Cell.parentNode.childNodes[1].innerHTML; 

  document.getElementById('mapcaption').innerHTML = t ;

  usePointFromPostcode(calpc, setCenterToPoint);

  placeMarkerAtPoint(curpoint); 

}  

}

 


