/*********************************************************************
 * Google maps stuff
 * *******************************************************************/
var _uid;

//create icons
var icon = new GIcon();
icon.image = "images/gselicon.png";
icon.shadow = "images/gsselicon.png";
icon.iconSize = new GSize(70, 52);
icon.shadowSize = new GSize(70, 52);
icon.iconAnchor = new GPoint(35, 51);
icon.infoWindowAnchor = new GPoint(26, 1);

var oicon = new GIcon();
oicon.image = "images/gicon.png";
oicon.shadow = "images/gsicon.png";
oicon.iconSize = new GSize(70, 52);
oicon.shadowSize = new GSize(70, 52);
oicon.iconAnchor = new GPoint(35, 51);
oicon.infoWindowAnchor = new GPoint(26, 1);

var officon = new GIcon();
officon.image = "images/gajploc.png";
officon.shadow = "images/gsicon.png";
officon.iconSize = new GSize(70, 52);
officon.shadowSize = new GSize(70, 52);
officon.iconAnchor = new GPoint(35, 51);
officon.infoWindowAnchor = new GPoint(26, 1);

var localSearch = {}; //create this for UK postcode lookup

// Creates a marker at the given point with the given number label
function createMarker(point,uid) {
  //var marker = new GMarker(point,oicon);
  if(uid == 'office'){
  	var marker = new GMarker(point,officon);
  }else{
	  if(uid == _uid){
		var marker = new GMarker(point,icon);
	  }else{
		var marker = new GMarker(point,oicon);
		  GEvent.addListener(marker, "click", function(){
			window.location = 'property_detail.php5?gpid='+uid;
		});
	  }
  }
  return marker;
}

function findNearestTo(locStr) {
	localSearch[locStr] = new GlocalSearch();
	localSearch[locStr].setSearchCompleteCallback(null, 
		function() {
			if (localSearch[locStr].results[0]){		
				var resultLat = localSearch[locStr].results[0].lat;
				var resultLng = localSearch[locStr].results[0].lng;
				var point = new GLatLng(resultLat,resultLng);
				xajax_findNearest(resultLat,resultLng);
			}
		});	
		
	localSearch[locStr].execute(locStr + ', UK');
}

function addMarkerAtLocation(locStr,uid) {
	if(maps[locStr] != null && maps[locStr] != undefined){
		point = new GLatLng(maps[locStr]['lat'],maps[locStr]['lng']);
		placeMarkerAtPoint(point,uid);
	}else{
		localSearch[locStr] = new GlocalSearch();
		localSearch[locStr].setSearchCompleteCallback(null, 
			function() {
				if (localSearch[locStr].results[0]){		
					var resultLat = localSearch[locStr].results[0].lat;
					var resultLng = localSearch[locStr].results[0].lng;
					var point = new GLatLng(resultLat,resultLng);
					xajax_storeMapCache(locStr,uid,resultLat,resultLng);
					placeMarkerAtPoint(point,uid);
				}
			});	
			
		localSearch[locStr].execute(locStr + ', UK');
	}
}

function centerOnLocation(locStr,uid,zoom) {
	localSearch[locStr] = new GlocalSearch();
	localSearch[locStr].setSearchCompleteCallback(null, 
		function() {
			if (localSearch[locStr].results[0]){		
				var resultLat = localSearch[locStr].results[0].lat;
				var resultLng = localSearch[locStr].results[0].lng;
				var point = new GLatLng(resultLat,resultLng);
				map.setCenter(point,zoom);
			}else{
				alert("Can't find that location");
			}
		});	
		
	localSearch[locStr].execute(locStr + ', UK');
}


function initMapFromLocation(locStr,uid) {
	_uid = uid;
	if(maps[locStr] != null && maps[locStr] != undefined){
		point = new GLatLng(maps[locStr]['lat'],maps[locStr]['lng']);
		drawMap(point);
	}else{
		localSearch[locStr] = new GlocalSearch();
		localSearch[locStr].setSearchCompleteCallback(null, 
			function() {
				if (localSearch[locStr].results[0])
				{		
					var resultLat = localSearch[locStr].results[0].lat;
					var resultLng = localSearch[locStr].results[0].lng;
					var point = new GLatLng(resultLat,resultLng);
					xajax_storeMapCache(locStr,uid,resultLat,resultLng);
					drawMap(point);
				}
			});	
			
		localSearch[locStr].execute(locStr + ', UK');
	}
}


function placeMarkerAtPoint(point,uid){
	var marker = createMarker(point,uid);
	map.addOverlay(marker);
}

function setCenterToPoint(point){
	map.setCenter(point, 16);
	_centerPoint = point;
}
_centerPoint = null;

function drawMap(point){
	if(document.getElementById('map')){
		map = new GMap2(document.getElementById('map'));
		//add controls
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
	
		//do markers
		map.enableContinuousZoom();
		xajax_loadMappingData();
		map.setCenter(point, 15);
		_centerPoint = point;
	}
}

function resetCenter(){
	if(_centerPoint != null){
		map.setCenter(_centerPoint,15);
	}
}
