
var propertyDataGlob = ""
var markerArray = new Array(51)
var zoom = 10
function initialize() {
	//alert("begin")
	if (GBrowserIsCompatible()) 
	{
		if ( ( lon != -999 ) && (checkFlag == 0) )
		{
			map = new GMap2(document.getElementById("map_canvas"));
			//lon = 60
			map.setCenter(new GLatLng(lon, lat), zoom);
			//map.setCenter(new GLatLng(51.4358, 0.12663), 10);
			//map.disableDragging();

			var tableDataArray = (distdata.split("|"));
			var tableHeadersArray = (distHeaders.split(","))
		
			var type
			//for some unknown reason there is no map.setsize method for the GOOGLE MAP API ???? 
			//it would be wise to move this into its own function
			//so to resize the map you must overwrite the maps containers size then invoke map.checkResize();
		
			var d=document.getElementById('map_canvas');
			d.style.height=350+"px";//350 (125 for menu) (2x 300 for each column with 5 gap)
			d.style.width=350+"px";//350 
			//THIS MUST BE CALLED AFTER the container is resized to avoid problems.
			map.checkResize();
			//var tempDataArray = new Array(tableHeadersArray.length);
			

			var tempDataArray = new Array(tableHeadersArray.length);
			var DataArrayWithoutNULL = new Array(100)
			
			type = 2
			//due to a fault in the above VB code which will need to be fixed this code is required to filter out all the empty sections of the array
			//pass in the bad array with all the junk as well as the new array all the data is to be sotred in
			//the final value is the limit the result array can contain
			//DataArrayWithoutNULL = removeEmptyArrayValues( tableDataArray, DataArrayWithoutNULL, DataArrayWithoutNULL.length )
			createGroupofMarker(tableDataArray, tableHeadersArray, tempDataArray, type)	

				
		
			var bounds= map.getBounds().getNorthEast()
			var bounds2 = map.getBounds().getSouthWest()
		
			type = 1
			map.addOverlay(createNewMarker( new GLatLng(lon,lat),userPostCode, 1 ));
			var count = 0
			while ( count < 3 )
			{
				count = 0;
				for ( i = 0 ; i < markerArray.length ; i++ )
				{
					if ( markerArray[i] != undefined )
					{
						var distLat1 = markerArray[i].getLatLng();
						var distLng1 = markerArray[i].getLatLng();
						
						
						var distLat = distLat1.lat();
						var distLng = distLng1.lng();
					
						if  ( (distLat < bounds.lat() ) && ( distLat > bounds2.lat()) ) 
						{
							if (( distLng < bounds.lng() )&&( distLng > bounds2.lng() ))
							{	
								count++
							}
						}
					}
					else
					{
						count = 3
					}
				}
				if (count <= 2 )
				{
					zoom = zoom - 1;
					map.setZoom ( zoom );
					//ReCentreGmap(lon, lat, zoom)
					bounds = map.getBounds().getNorthEast()
					bounds2 = map.getBounds().getSouthWest()
					
				}
			}
		
		}
		else
		{
			var d=document.getElementById('map_canvas');
			d.innerHTML = "An Error has occurred either your postcode cannot be found or there is a problem with a server."
		}
			
		
		map.addControl(new GSmallMapControl());
	//	ReCentreGmap(lon, lat, zoom)
	}
}
function createGroupofMarker(markerDataArray, markerHeadersArray, tempMarkerDataArray, typeNumber)	
{
	
	var arrayPosition=0;
	var lon 
	var lat
	
	
	
	switch (typeNumber)
	{
		case 1:
		{
			arrayPosition = 0
			break;
		}
		case 2:
		{
			arrayPosition = 1
			break;
		}
		case 3:
		{
			arrayPosition = 0
			break;
		}
	}

	for ( i = 0; i < markerDataArray.length ; i++)
	{
		
		if( markerDataArray[i] != "")
		{
			if (markerDataArray[i] != null)
			{
				
				tempMarkerDataArray = (markerDataArray[i].split(","))
				
				
				for ( j = 0 ; j < markerHeadersArray.length ; j++ )
				{
					//alert(markerHeadersArray[j])
					if (markerHeadersArray[j].indexOf('Lon') != -1)
					{
						lon = tempMarkerDataArray[j]
					}
					if (markerHeadersArray[j].indexOf('Lat') != -1)
					{
						lat = tempMarkerDataArray[j]
					}		
				}

				if (lon != -999 )
				{
					
					var toolTipMsg = "<font face='Arial' size='-1'><b>" + tempMarkerDataArray[4] + "</b><br/>" + tempMarkerDataArray[5] +"<br/>" + tempMarkerDataArray[6]+"<br/>" + tempMarkerDataArray[7]+"<br/>" + tempMarkerDataArray[9]+"<br/><b>Tel:</b> " + tempMarkerDataArray[11] + "</font>"
					
					//alert (lat+"<---lat  lon--->"+lon)
					markerArray[arrayPosition] = createNewMarker(new GLatLng(lat,lon), toolTipMsg, typeNumber )
					map.addOverlay(markerArray[arrayPosition]);
					arrayPosition++;
				}
				
			/*	if ( i >=49)
				{
					i = markerDataArray.length;	
				}*/
			}
		}	
	}		
}
function mapResize( width, height )
{
	if (GBrowserIsCompatible()) 
	{
		//for some unknown reason there is no map.setsize method for the GOOGLE MAP API ???? 
		//it would be wise to move this into its own function
		//so to resize the map you must overwrite the maps containers size then invoke map.checkResize();
		var d=document.getElementById('map_canvas');
		
		d.style.width=width+"px";//600,350 
		d.style.height=height+"px";//475,350 (125 for menu) (2x 300 for each column with 5 gap)
		//THIS MUST BE CALLED AFTER the container is resized to avoid problems.
		map.checkResize();

	}
}

function mapZoom( zoomValue )
{
	if (GBrowserIsCompatible()) 
	{
		//this should work but doesn't!!!
		//placing map.setZoom or any zoom function will fail unless they are called from in the inisalize function
		var newZoomLevel = zoomValue +9
		//alert( newZoomLevel );
		map.setZoom( newZoomLevel );
	}
}
function createNewMarker( position, Msg, type )
{
	switch (type)
	{
		case 1:
		{
			var markerIcon = new GIcon(G_DEFAULT_ICON);
			markerIcon.image = "http://gmaps-samples.googlecode.com/svn/trunk/markers/blue/blank.png";
			break;
		}
		case 2:
		{
			var markerIcon = new GIcon(G_DEFAULT_ICON);
			markerIcon.image = "http://gmaps-samples.googlecode.com/svn/trunk/markers/green/blank.png";
			break;
		}
		case 3:
		{
			var markerIcon = new GIcon(G_DEFAULT_ICON);
			markerIcon.image = "http://gmaps-samples.googlecode.com/svn/trunk/markers/orange/blank.png";
			break;
		}
	}
	// Set up our GMarkerOptions object
	markerOptions = { icon:markerIcon };
	
	var marker = new GMarker(position,markerOptions );
	
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(Msg);    
	});
	return marker
}

function removeEmptyArrayValues( firstArray, resultArray, limit )
{
	var trueI = 0;
	
	for ( i = 0; i < firstArray.length ; i++)
	{
		if( firstArray[i] != "")
		{
			if (firstArray[i] != 'NULL')
			{
				resultArray[trueI] = (firstArray[i])
				//alert (resultArray[trueI])
				trueI++
			}
		}
		if ( trueI >= limit-1 )
		{
			i = firstArray.length
		}
	}
	return resultArray
}

function removeGroupofMarkers(type)
{
	arrayPosition = 0
	switch (type)
	{
		case 2:
		{
			arrayPosition = 50
			break;
		}
		case 3:
		{
			arrayPosition = 100
			break;
		}
	}
	// i'm aware this is hard coded very bad very bad sort this out later
	for ( i = 0 ; i < 50 ; i++)
	{
		//marker.hide()
		if (markerArray[arrayPosition] != "")
		{
			if( markerArray[arrayPosition] != 'NULL')
			{
				if (markerArray[arrayPosition] != undefined )
				{
					//alert(markerArray[arrayPosition])
					markerArray[arrayPosition].hide()
				}
			}
		}
		arrayPosition++
	}
}

function UnhideGroupofMarkers(type)
{
	arrayPosition = 0
	switch (type)
	{
		case 2:
		{
			arrayPosition = 50
			break;
		}
		case 3:
		{
			arrayPosition = 100
			break;
		}
	}
	// i'm aware this is hard coded very bad very bad sort this out later
	for ( i = 0 ; i < 50 ; i++)
	{
		//marker.hide()
		markerArray[arrayPosition].show()
		arrayPosition++
	}
}

function ToggleGroupofMarkers(type)
{
	arrayPosition = 0
	switch (type)
	{
		case 2:
		{
			arrayPosition = 50
			break;
		}
		case 3:
		{
			arrayPosition = 100
			break;
		}
	}
	// i'm aware this is hard coded very bad very bad sort this out later
	for ( i = 0 ; i < 50 ; i++)
	{
		//marker.hide()
		if ( markerArray[arrayPosition].isHidden())
		{
			markerArray[arrayPosition].show()
		}
		else
		{
			markerArray[arrayPosition].hide()
		}
		arrayPosition++
	}
}
//these following functions should be called by the Gmap API but they are not? 
//best bet would be to look on some newsgroups later.
function infowindowclose()
{
	if (GBrowserIsCompatible()) 
	{
		alert("well done you just closed an HTML info windows")
	}
}

function infowindowbeforeclose() 
{
	if (GBrowserIsCompatible()) 
	{
		alert("well done you just closed an HTML info windows")
	}
}
	
function infowindowopen() 
{
	if (GBrowserIsCompatible()) 
	{
		alert("well done you just opened an HTML info windows")
	}
}

function load() 
{
	if (GBrowserIsCompatible()) 
	{
		alert("Load Complete")
	}
}

function ReCentreGmap(lat, lon, zoom)
{
	var zoomlevel = zoom
	var map2
	//alert("realigning map to new Coords to "+lat+" "+lon+" zoom level:"+zoom)
	if ( ( (lat <= 91 ) && (lat >= -91) ) || ( (lon <= 181 ) &&  ( lon >= -181 ) ) )
	{
		map2 = new GMap2(document.getElementById("map_canvas"));
		map2.setCenter(new GLatLng(lat, lon));	
	}
	else
	{
		//alert("WARNING: its appears u might be no where??")
	}
}