var iconmarker;
var marker;
var map = null; 
var geocoder = null;
var localSearch = new GlocalSearch();
var markersArray = new Array();
var intCurrentZoomLevel = 9;
var intAddPage;

function mapLoad() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		geocoder = new GClientGeocoder();
		map.addControl(new GLargeMapControl3D());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(51.12076493195686, 0.59600830078125), intCurrentZoomLevel, G_NORMAL_MAP);
		new GKeyboardHandler(map);
		map.enableContinuousZoom();
		var localSearch = new GlocalSearch();
		
	GEvent.addListener(map, "zoomend", function(oldLevel, newLevel) {
	intCurrentZoomLevel = newLevel;
	loadMapIcons(showMapPeople,showMapPlaces,showMapEvents,ShowMapGroups,intCurrentZoomLevel,showSearchPlaces,showSearchGroups,showSearchEvents);
	});
	
	if(intAddPage == '1'){
		google.maps.Event.addListener(map, "move", function() {
		var center = map.getCenter();
		var thisLatLng = center.toString();
		thisLatLng = thisLatLng.replace("(","").replace(")","");
		
		var thisLatLngSplit = new Array();
		thisLatLngSplit = thisLatLng.split(",");
		var thisLat = thisLatLngSplit[0];
		var thisLng = thisLatLngSplit[1];
		document.getElementById("lat").value = thisLat;
		document.getElementById("lng").value = thisLng;
		
		//document.getElementById("latlon").innerHTML = center.toString();
	});
	}
	}
}



function usePointFromPostcode(strLocation) {
	localSearch.setSearchCompleteCallback(null, 
		function() {
			if (localSearch.results[0]) {
				
				 var resultLat = localSearch.results[0].lat;
				 var resultLng = localSearch.results[0].lng;
				 //globalLat = resultLat;
				 //globalLng = resultLng;
				 alert(resultLat+", "+resultLng);
			} else {
				alert(strLocation + " not found");
			}
		});
	localSearch.execute(strLocation, "UK");
}

/*function usePointFromPostcode(postcode, strIcon, strIconType, strInfo, strGender) {
	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);
				 loadIcon(point, strIcon, strIconType, strInfo, strGender);
			  
			} else {
				alert(strLocation + " not found");
			}
		});
	localSearch.execute(postcode, "UK");
}*/

/*function loadIcon(point, strIcon, strIconType, strInfo, strGender) {
	

	marker = new GIcon(G_DEFAULT_ICON);
	marker.printImage  = strIcon;
	
	if (strIconType == 'people'){
	  
	  if(strGender=='1'){
		strGender = 'm';
	  } else {
		  strGender = 'f';
	  }
	  
	  theIcon = 'images/' + strIcon  + '_' + strGender +'.png';
	  marker.image = 'images/' + strIcon  + '_' + strGender +'.png';
	  
	  if (strIcon == 'map_icon'){
		marker.iconSize = new GSize(21, 31);
	  } else if (strIcon == 'map_icon_link'){
		 marker.iconSize = new GSize(25, 31); 
	  } else if (strIcon == 'map_icon_fav'){
		 marker.iconSize = new GSize(23, 31); 
	  } else if (strIcon == 'map_icon_love'){
		 marker.iconSize = new GSize(24, 31);
	  } else if (strIcon == 'map_icon_message'){
		 marker.iconSize = new GSize(27, 48); 
	  } else {
		marker.iconSize = new GSize(21, 31);
	  }
	  
	} else if (strIconType == 'places'){
	  marker.image = 'images/map_icon_place.png';
	  marker.iconSize = new GSize(17, 24);
	} else if (strIconType == 'events'){  
	  marker.image = 'images/map_icon_event.png';
	  marker.iconSize = new GSize(23, 22);
	} else if (strIconType == 'groups'){
	  marker.image = 'images/map_icon_group.png';
	  marker.iconSize = new GSize(21, 23);
	}
	
	markerOptions = { icon:marker };
	iconmarker = new GMarker(point, markerOptions);
	
	//var manager = new GMarkerManager(map);
	
	map.addOverlay(iconmarker);
	
	iconmarker.bindInfoWindowHtml(strInfo);
	
	markersArray.push(iconmarker);

}*/



function loadIcon(strUserId, strLocation, strIcon, strIconType, strInfo, strGender, strLat, strLng) {
	
	var resultLat = '';
	var resultLng = '';
	
	if(strLat == ''){
		resultLat = '84.83422451455144';
		resultLng = '179.82421875';
	} else {
		resultLat = strLat;
		resultLng = strLng;
	}
	
	localSearch.execute(strLocation, "UK");
		  
	  marker = new GIcon(G_DEFAULT_ICON);
	  
	  marker.printImage  = strIcon;
	  
	  if (strIconType == 'House'){	  
		  marker.image = 'images/dot_active.png';  
		  marker.iconSize = new GSize(21, 21);
		  marker.shadow = 'images/dot_active.png';
		  marker.shadowSize = new GSize(32, 24);
		  marker.iconAnchor = new GPoint(3, 22);
		  marker.infoWindowAnchor = new GPoint(12, 1);
	  }
	  
	  
	  markerOptions = { icon:marker };
		
	  var point = new GLatLng(resultLat, resultLng);

	  iconmarker = new GMarker(point, markerOptions);
	  
	  var manager = new GMarkerManager(map);
	  
	  
	  map.addOverlay(iconmarker);
	  
	  
	  iconmarker.bindInfoWindowHtml(strInfo);
	  
	  markersArray.push(iconmarker);
	  
}

function removeAllMarkers()
{
   for (var i = 0; i < markersArray.length; i++) 
   {
      map.removeOverlay(markersArray[i]);
   }
   
   markersArray = new Array();
}


//function addLoadEvent(func) {
//  var oldonload = window.onload;
//  if (typeof window.onload != 'function') {
//	window.onload = func;
//  } else {
//	window.onload = function() {
//	  oldonload();
//	  func();
//	}
//  }
//}

//function addUnLoadEvent(func) {
//	var oldonunload = window.onunload;
//	if (typeof window.onunload != 'function') {
//	  window.onunload = func;
//	} else {
//	  window.onunload = function() {
//		oldonunload();
//		func();
//	  }
//	}
//}

function onLoadMapIcons(){
	loadMapIcons();
}

function loadMapIcons(){
		removeAllMarkers();
		LoadSearchPlaces();
}


function newCentreMap(strLat,strLng){
	map.setCenter(new GLatLng(strLat, strLng), intCurrentZoomLevel, G_NORMAL_MAP);
}

function newCentreMapZoomed(strLat,strLng,intZoom){
	map.setCenter(new GLatLng(strLat, strLng), intZoom, G_NORMAL_MAP);
}



//Postcode validator code
var geocoder = null;
var point;

function checkValidPostCode(postcode) {
	localSearch.setSearchCompleteCallback(null, 
		function() {
			if (localSearch.results[0] && localSearch.results[0].lat && localSearch.results[0].lng) {
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				document.getElementById('latadd').value=resultLat;
				document.getElementById('lngadd').value=resultLng;
			} else {
				document.getElementById('latadd').value='';
				document.getElementById('lngadd').value='';
			}
		});
	localSearch.execute(postcode, "UK")
}


//Get Lat Lng of Postcode
var vargetLatLngFromPostCode;
function getLatLngFromPostCode(postcode, intBusinessID) {
	localSearch.setSearchCompleteCallback(null, 
		function() {
			if (localSearch.results[0] && localSearch.results[0].lat && localSearch.results[0].lng) {
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				vargetLatLngFromPostCode = resultLat+','+resultLng;
				ajaxGenericNoWait('includes/actions/business_add_latlng.php', '', '&bid='+intBusinessID+'&lat='+resultLat+'&lng='+resultLng);
;
			}
		});
	localSearch.execute(postcode, "UK")
}


// Search Postcode to town search
function checkValidPostCodeSearch(postcode) {
	localSearch.setSearchCompleteCallback(null, 
		function() {
			if (localSearch.results[0] && localSearch.results[0].lat && localSearch.results[0].lng) {
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				document.getElementById('lat').value=resultLat;
				document.getElementById('lng').value=resultLng;
				
				var geocoder = new GClientGeocoder();
				var latlng = new google.maps.LatLng(resultLat, resultLng);
				geocoder.getLocations(latlng, function(addresses) {
						if(addresses.Status.code != 200) {
//								alert("reverse geocoder failed to find an address for " + latlng.toUrlValue());
						} else { 
								var varAddressArr = addresses.Placemark[0].address.split(", ");
								varAddressArrLen = varAddressArr.length - 3;
//								alert(varAddressArr[varAddressArrLen]);
						}
				});
			} else {
				document.getElementById('lat').value='';
				document.getElementById('lng').value='';
			}
		});
	localSearch.execute(postcode, "UK");
}
