/* ----------------------------------------------------------------------------
 
 	Any additional googlemap functionality is placed in this file.
	
	eg. any map event handlers
	
   ---------------------------------------------------------------------------- */	

	var requestURL = 'http://'+window.location.host+'/assets/modules/googlemaps/ajax_handler.php';
	
	function reloadMap()
	{
		document.getElementById('map_status').style.display = 'block';
		document.getElementById('map_status').innerHTML = 'Loading venues...';
		var bounds = map.getBounds();
		
		GDownloadUrl(requestURL+'?mode=update_map&ne='+bounds.getNorthEast().toUrlValue()+'&sw='+bounds.getSouthWest().toUrlValue()+'&z='+map.getZoom(),  function(responseText, httpCode){ 
			if(httpCode == 200)
			{
				document.getElementById('map_status').innerHTML = 'Adding markers...';
				map.clearOverlays();
				try
				{
					var points = eval('(' + responseText + ')');
					
					for(i = 0; i < points.length; i++)
					{
						var thispoint = points[i];
						
						if(thispoint.totalpoints > 4)
						{
							var popup_contents = thispoint.totalproperties+' venues in this area.<br /><a href="#" onclick="map.setCenter(new GLatLng('+thispoint.latitude+', '+thispoint.longitude+'),  map.getZoom()+3); return false;">Zoom in</a> to see them all.';
						}
						else
						{
							if(thispoint.totalpoints == 1)
							{
								var popup_contents = pointHTML(thispoint.points[0]);
							}
							else
							{
								var popup_contents = new Object;
								for(j = 0; j < thispoint.points.length; j++)
								{
									popup_contents[(j+1)] = pointHTML(thispoint.points[j]);
								}
								
							}
						}
						
						var marker = createMarker(new GLatLng(thispoint.latitude, thispoint.longitude), popup_contents, icons[thispoint.icon], thispoint.tooltip);
						map.addOverlay(marker);
						
						
					}
					
					document.getElementById('map_status').innerHTML = '&nbsp;';
					document.getElementById('map_status').style.display = 'none';

				}
				catch(err)
				{
					document.getElementById('map_status').innerHTML = 'Error loading venues - ' + err.description ;
				}
			}
		});
		
	}
	
	function reloadMapClearMarkers()
	{
		map.clearOverlays();
		reloadMap();
	}
	
	function pointHTML(point)
	{
		var display = '<div class="infoWindowHolder" style="width: 350px;">';
		display += '<strong>'+point.name+'</strong><br />';
		display += point.address1+'<br />';
		display += point.address2+'<br />';
		display += point.address3+'<br />';
		display += point.postCode+'<br /><br />';
		display += 'Tel: '+point.telephone+'<br />';
		display += '<a href="'+point.website+'" onclick="window.open(this.href); return false;">'+point.website+'</a>';
		display += '</div>';
		
		return display;
	}
	