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(varsitelat, varsitelng) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		geocoder = new GClientGeocoder();
		map.addControl(new GLargeMapControl3D());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(varsitelat, varsitelng), 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 == 'people'){
		  
		  if(strGender==1){
				strGender = 'm';
		  } else if (strGender==2) {
				strGender = 'f';
		  } else if (strGender==3) {
				strGender = 'b';
			}
		  
		  theIcon = 'images/' + strIcon  + '_' + strGender +'.png';
		  marker.image = 'images/' + strIcon  + '_' + strGender +'.png';
		  
		    
		  
		  if (strIcon == 'map_icon'){
			marker.shadow = 'images/shadow_' + strIcon  + '_' + strGender +'.png';
			marker.iconSize = new GSize(23, 34);
			marker.shadowSize = new GSize(36, 29);
			marker.iconAnchor = new GPoint(9, 29);
			marker.infoWindowAnchor = new GPoint(13, 1);
		  } else if (strIcon == 'map_icon_multi'){
		  marker.image = 'images/map_icon_b.png';
		  marker.iconSize = new GSize(23, 25);
		  marker.shadow = 'images/shadow_map_icon_group.png';
		  marker.shadowSize = new GSize(31, 23);
		  marker.iconAnchor = new GPoint(10, 20);
		  marker.infoWindowAnchor = new GPoint(12, 1);
		  } else if (strIcon == 'map_icon_link'){
			marker.iconSize = new GSize(25, 34);
			marker.shadow = 'images/shadow_map_icon_alternate_' + strGender +'.png';
			marker.shadowSize = new GSize(36, 30);
			marker.iconAnchor = new GPoint(9, 29);
			marker.infoWindowAnchor = new GPoint(14, 1);
		  } else if (strIcon == 'map_icon_fav'){
			marker.iconSize = new GSize(25, 34);
			marker.shadow = 'images/shadow_map_icon_alternate_' + strGender +'.png';
			marker.shadowSize = new GSize(36, 30);
			marker.iconAnchor = new GPoint(9, 29);
			marker.infoWindowAnchor = new GPoint(14, 1);
		  } else if (strIcon == 'map_icon_love'){
			marker.iconSize = new GSize(25, 34);
			marker.shadow = 'images/shadow_map_icon_alternate_' + strGender +'.png';
			marker.shadowSize = new GSize(36, 30);
			marker.iconAnchor = new GPoint(9, 29);
			marker.infoWindowAnchor = new GPoint(14, 1);
		  } else if (strIcon == 'map_icon_bday'){
			marker.iconSize = new GSize(25, 34);
			marker.shadow = 'images/shadow_map_icon_alternate_' + strGender +'.png';
			marker.shadowSize = new GSize(36, 30);
			marker.iconAnchor = new GPoint(9, 29);
			marker.infoWindowAnchor = new GPoint(14, 1);
		  } else if (strIcon == 'map_icon_message'){
			 marker.iconSize = new GSize(27, 48);
			 marker.iconAnchor = new GPoint(5, 45);
		  } else {
			marker.iconSize = new GSize(23, 34);
			marker.iconAnchor = new GPoint(9, 29);
		  }
		  
	  } else if (strIconType == 'business'){
		  marker.image = 'images/map_icon_business.png';  
		  marker.iconSize = new GSize(21, 21);
		  marker.shadow = 'images/shadow_map_icon_group.png';
		  marker.shadowSize = new GSize(32, 24);
		  marker.iconAnchor = new GPoint(3, 22);
		  marker.infoWindowAnchor = new GPoint(12, 1);
	  } else if (strIconType == 'places'){
		  marker.image = 'images/map_icon_place.png';  
		  marker.iconSize = new GSize(20, 27);
		  marker.shadow = 'images/shadow_map_icon_place.png';
		  marker.shadowSize = new GSize(32, 24);
		  marker.iconAnchor = new GPoint(3, 22);
		  marker.infoWindowAnchor = new GPoint(12, 1);
	  } else if (strIconType == 'placessuggestion'){
		  marker.image = 'images/map_icon_place_suggestion.png';  
		  marker.iconSize = new GSize(20, 27);
		  marker.shadow = 'images/shadow_map_icon_place.png';
		  marker.shadowSize = new GSize(32, 24);
		  marker.iconAnchor = new GPoint(3, 22);
		  marker.infoWindowAnchor = new GPoint(12, 1);
	  } else if (strIconType == 'events'){  
		  marker.image = 'images/map_icon_event.png';
		  marker.iconSize = new GSize(25, 25);
		  marker.shadow = 'images/shadow_map_icon_group.png';
		  marker.shadowSize = new GSize(31, 23);
		  marker.iconAnchor = new GPoint(10, 20);
		  marker.infoWindowAnchor = new GPoint(12, 1);
	  } else if (strIconType == 'eventssuggestion'){  
		  marker.image = 'images/map_icon_event_suggestion.png';
		  marker.iconSize = new GSize(25, 25);
		  marker.shadow = 'images/shadow_map_icon_group.png';
		  marker.shadowSize = new GSize(31, 23);
		  marker.iconAnchor = new GPoint(10, 20);
		  marker.infoWindowAnchor = new GPoint(12, 1);
	  } else if (strIconType == 'groups'){
		  marker.image = 'images/map_icon_group.png';
		  marker.iconSize = new GSize(23, 25);
		  marker.shadow = 'images/shadow_map_icon_group.png';
		  marker.shadowSize = new GSize(37, 23);
		  marker.iconAnchor = new GPoint(11, 21);
		  marker.infoWindowAnchor = new GPoint(19, 1);
	  } else if (strIconType == 'groupssuggestion'){
		  marker.image = 'images/map_icon_group_suggestion.png';
		  marker.iconSize = new GSize(23, 25);
		  marker.shadow = 'images/shadow_map_icon_group.png';
		  marker.shadowSize = new GSize(37, 23);
		  marker.iconAnchor = new GPoint(11, 21);
		  marker.infoWindowAnchor = new GPoint(19, 1);
		  
		  
			  
	  } else if (strIconType == 'searchplaces'){
		  marker.image = 'images/map_icon_search_place_'+ getAlphaFromInt(strUserId) +'.png';  
		  marker.iconSize = new GSize(21, 21);
		  marker.shadow = 'images/shadow_map_icon_search.png';
		  marker.shadowSize = new GSize(25, 18);
		  marker.iconAnchor = new GPoint(16, 16);
		  marker.infoWindowAnchor = new GPoint(14, 1);
	  } else if (strIconType == 'searchevents'){  
		  marker.image = 'images/map_icon_search_event_'+ getAlphaFromInt(strUserId) +'.png';  
		  marker.iconSize = new GSize(21, 21);
		  marker.shadow = 'images/shadow_map_icon_search.png';
		  marker.shadowSize = new GSize(25, 18);
		  marker.iconAnchor = new GPoint(16, 16);
		  marker.infoWindowAnchor = new GPoint(14, 1);
	  } else if (strIconType == 'searchgroups'){
		  marker.image = 'images/map_icon_search_group_'+ getAlphaFromInt(strUserId) +'.png';  
		  marker.iconSize = new GSize(21, 21);
		  marker.shadow = 'images/shadow_map_icon_search.png';
		  marker.shadowSize = new GSize(25, 18);
		  marker.iconAnchor = new GPoint(16, 16);
		  marker.infoWindowAnchor = new GPoint(14, 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(showMapPeople,showMapPlaces,showMapEvents,ShowMapGroups,9,showSearchPlaces,showSearchGroups,showSearchEvents);
}

function loadMapIcons(mPe,mPl,mEv,mGr,mZ,mSPl,mSGr,MSEv){
		removeAllMarkers();
		if(mPe == 1){
			if(mZ > 8 && mZ < 13 ){loadPeople();}
			document.getElementById('show_friends_tickbox').style.backgroundImage = 'url(images/tickbox_blue_ticked.png)';
		} else {
			document.getElementById('show_friends_tickbox').style.backgroundImage = 'url(images/tickbox_blue_noticked.png)';
		}
		
		if(mPl == 1){
			if(mZ > 8 && mZ < 22 ){loadPlaces();}
			document.getElementById('show_places_tickbox').style.backgroundImage = 'url(images/tickbox_red_ticked.png)';
		} else {
			document.getElementById('show_places_tickbox').style.backgroundImage = 'url(images/tickbox_red_noticked.png)';
		}
		
		if(mEv == 1){
			if(mZ > 8 && mZ < 22 ){loadEvents();}
			document.getElementById('show_events_tickbox').style.backgroundImage = 'url(images/tickbox_yellow_ticked.png)';
		} else {
			document.getElementById('show_events_tickbox').style.backgroundImage = 'url(images/tickbox_yellow_noticked.png)';
		}
		
		if(mGr == 1){
			if(mZ > 8 && mZ < 22 ){loadGroups();}
			document.getElementById('show_groups_tickbox').style.backgroundImage = 'url(images/tickbox_green_ticked.png)';
		} else {
			document.getElementById('show_groups_tickbox').style.backgroundImage = 'url(images/tickbox_green_noticked.png)';
		}
		
		if(mZ > 12){
				document.getElementById('map_warning_msg').innerHTML = '<p class="map_warning"><img src="images/icon_map_warning.png" width="24" height="22" alt="Warning" style="float:left; margin-right:5px;" /> Friend Icons are hidden at this<br/>zoom level</p>';
		} else if(strShowMiniMapFriend!=''){
			document.getElementById('map_warning_msg').innerHTML = '<p class="map_warning"><img src="images/icon_map_warning.png" width="24" height="22" alt="Warning" style="float:left; margin-right:5px;" /> '+strShowMiniMapFriend+'</p>';
		} else {
			document.getElementById('map_warning_msg').innerHTML = '<p class="map_warning">Use these buttons to toggle what is displayed on the map</p>';
		}
		
		if(mSPl == 1){
			if(mZ > 8 && mZ < 22 ){LoadSearchPlaces();}	
		}
		
		if(mSGr == 1){
			if(mZ > 8 && mZ < 22 ){LoadSearchGroups();}
		}
		
		if(MSEv == 1){
			if(mZ > 8 && mZ < 22 ){LoadSearchEvents();}
		}
		
		loadBusinesses();
}

function showHideIconsTickClick(varClicked){
//VarClicked accepts - people or places or events or groups.
	if(varClicked == 'people'){
		if (showMapPeople == 1){
			showMapPeople = 0;
		} else {
			showMapPeople = 1;
		}
	}
	if(varClicked == 'places'){
		if (showMapPlaces == 1){
			showMapPlaces = 0;
		} else {
			showMapPlaces = 1;
		}
	}
	if(varClicked == 'events'){
		if (showMapEvents == 1){
			showMapEvents = 0;
		} else {
			showMapEvents = 1;
		}
	}
	if(varClicked == 'groups'){
		if (ShowMapGroups == 1){
			ShowMapGroups = 0;
		} else {
			ShowMapGroups = 1;
		}
	}
	if(varClicked == 'searchgroups'){
		if (showSearchGroups == 1){
			showSearchGroups = 0;
		} else {
			showSearchGroups = 1;
		}
	}
	if(varClicked == 'searchevents'){
		if (showSearchEvents == 1){
			showSearchEvents = 0;
		} else {
			showSearchEvents = 1;
		}
	}
	if(varClicked == 'searchplaces'){
		if (showSearchPlaces == 1){
			showSearchPlaces = 0;
		} else {
			showSearchPlaces = 1;
		}
	}
	
	
	
	if(intShowYourNewsFeed == 1)
	{
		
		getYourFriendsNews('your_friends_news_feed',intYourNewsUID,'',8,intYourNewsBType,'',intYourNewsUBID,showMapPeople,showMapPlaces,showMapEvents,ShowMapGroups);
	}
	
	intCurrentZoomLevel = map.getZoom();
	loadMapIcons(showMapPeople,showMapPlaces,showMapEvents,ShowMapGroups,intCurrentZoomLevel,showSearchPlaces,showSearchGroups,showSearchEvents);
	
	
}

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);
}