var checkFlag = 0
var propertyDataGlob = ""
var map = 0
	//#####################################################################
	//#####################################################################
	//DO NOT RECREATE/REDEFINE OR EVEN USE TWICE ON ANY OBJECTS EVER EVER EVER EVER FOR THIS MAP!!!!!!!!!!!!
	//#####################################################################
	//######################################################################
function initialize() {
	if (GBrowserIsCompatible()) 
	{
		
		
		map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(52.617224,-1.83197), 11);
		//map.setCenter(new GLatLng(51.4358, 0.12663), 10);
		//map.disableDragging();
		map.addMapType(G_SATELLITE_MAP); 
		setupSat();
		mapResize( 560, 500 )	
		checkFlag = 0
		
		if( propIDvar != "")
		{
			if ( propIDvar != 'NULL')
			{
				if( latVar != "")
				{
					if ( latVar != 'NULL')
					{
						if( lonVar != "")
						{
							if ( lonVar != 'NULL')
							{
								mapResize( 560, 500 )
								map.setCenter(new GLatLng(latVar, lonVar), 9);
								mapZoom( 2 )
								checkFlag = 1;
								map.disableDragging();
							}
						}
					}
				}
			}
		}
		
		if (checkFlag == 0)
		{
			removeGroupofMarkers(3)
			removeGroupofMarkers(1)
		}
		maploaded = 1
	//	createPropertyDetailsColumn( propertyDataArrayWithoutNULL[0] )
	}
}
function createGroupofMarker(markerDataArray, markerHeadersArray, tempMarkerDataArray, typeNumber)	
{
	//alert(markerDataArray.length)
	var arrayPosition=0;
	
	switch (typeNumber)
	{
		case 1:
		{
			arrayPosition = 0
			break;
		}
		case 2:
		{
			arrayPosition = 50
			break;
		}
		case 3:
		{
			arrayPosition = 100
			break;
		}
	}

	
	for ( i = 0; i < markerDataArray.length ; i++)
	{
		if( markerDataArray[i] != "")
		{
			if (markerDataArray[i] != null)
			{
			
				if (typeNumber == 2)
				{
					//alert(markerDataArray[i])
				}
				tempMarkerDataArray = (markerDataArray[i].split(","))
				
				for ( j = 0 ; j < markerHeadersArray.length ; j++ )
				{
					//@TODO get rid of the starting space
					if (typeNumber == 3)
					{
					//	alert(markerHeadersArray[j])
					}
					
					if (markerHeadersArray[j] == " lon")
					{

						lon = tempMarkerDataArray[j]
					
					}
					if (markerHeadersArray[j] == " lat")
					{

						lat = tempMarkerDataArray[j]
						
					}		
					if (markerHeadersArray[j] == " propertyLon")
					{
						lon = tempMarkerDataArray[j]
					}
					if (markerHeadersArray[j] == " propertyLat")
					{

						lat = tempMarkerDataArray[j]
					}	
					
				}
							
				if (lon != -999 )
				{
					
					if ( typeNumber == 2 )
					{
						if ( checkFlag == 0 )
						{
							var toolTipMsg = tempMarkerDataArray[0] + "," + tempMarkerDataArray[1]
							
							
							toolTipMsg += '<br/><strong><a href="mapdetails.asp?propID='+tempMarkerDataArray[0]+'&lon='+tempMarkerDataArray[3]+'&lat='+tempMarkerDataArray[4]+'">More Details</a></strong>'
							
						}
						else
						{
							var toolTipMsg =  "<table width='210px'><tr><td class='general'>"+unescape(tempMarkerDataArray[3])
							toolTipMsg += "<br/>"+tempMarkerDataArray[13]+" <span class='generalBold'>PCM</span><br/>"+tempMarkerDataArray[12]+" <span class='generalBold'>Bedrooms</span>"
						//	toolTipMsg += '<br/><strong><a href="'+window.location.pathname+'?propID='+tempMarkerDataArray[0]+'&lon='+tempMarkerDataArray[3]+'&lat='+tempMarkerDataArray[4]+'">More Details</a></strong>'
							toolTipMsg += "<br/><a href='javascript:viewProperty("
							toolTipMsg += tempMarkerDataArray[0]
							toolTipMsg += ")'>More Details</a>"
							toolTipMsg += "</td><td><img src='assets/padding.gif' height='75px' width='75px'></td></tr></table>"
						}
					}
					else
					{
						if ( typeNumber == 3 )
						{
							var toolTipMsg = "<table width='210px'><tr><td colspan='2' class='general'>"+tempMarkerDataArray[1]+"</td><tr><td class='general'>"+tempMarkerDataArray[2]+"<br/>"+tempMarkerDataArray[3]+"<br/>"+tempMarkerDataArray[4]+"<br/>"+tempMarkerDataArray[5]+"</td><td  class='general' valign='bottom' style='padding-left:5px;padding-top:5px;'><a href='http://www.ofsted.gov.uk/oxedu_providers/full/(urn)/"+tempMarkerDataArray[0]+"/' target='_blank'>Ofsted</a></td></tr></table>"
						}
						else
						{
							if ( typeNumber == 1 )
							{
								var toolTipMsg = "<span class='general'>"+tempMarkerDataArray[1] + " Station</span>"
							}
							else
							{
								var toolTipMsg = ""
							}
						}
					}
					/*if ( typeNumber == 3 )
					{
						alert(lat)
						alert(lon)
					}*/
					markerArray[arrayPosition] = createNewMarker(new GLatLng(lat,lon), toolTipMsg, typeNumber )
					map.addOverlay(markerArray[arrayPosition]);
					arrayPosition++;
				}
				
				if ( i >=49)
				{
					i = markerDataArray.length;	
				}
			}
		}	
	}
	
	/*
	for ( i = 0; i < markerDataArray.length ; i++)
	{
		if( markerDataArray[i] != "")
		{
			if (markerDataArray[i] != null)
			{
				tempMarkerDataArray = (markerDataArray[i].split(","))
				if ( tempMarkerDataArray[3] != -999 )
				{
					if ( typeNumber == 2 )
					{
						if ( checkFlag == 0 )
						{
							var toolTipMsg = tempMarkerDataArray[0] + "," + tempMarkerDataArray[1]
							toolTipMsg += '<br/><strong><a href="mapdetails.asp?propID='+tempMarkerDataArray[0]+'&lon='+tempMarkerDataArray[3]+'&lat='+tempMarkerDataArray[4]+'">More Details</a></strong>'
						}
						else
						{
							var toolTipMsg = tempMarkerDataArray[1]
							//toolTipMsg += '<br/><strong><a href="'+window.location.pathname+'?propID='+tempMarkerDataArray[0]+'&lon='+tempMarkerDataArray[3]+'&lat='+tempMarkerDataArray[4]+'">More Details</a></strong>'
						}
					}
					else
					{
						var toolTipMsg = tempMarkerDataArray[0] + "," + tempMarkerDataArray[1]
						
					}
					markerArray[arrayPosition] = createNewMarker(new GLatLng(tempMarkerDataArray[3],tempMarkerDataArray[4]), 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()) 
	{
		if ( maploaded == 1 )
		{
			//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] != undefined )
		{
			if ( markerArray[arrayPosition].isHidden())
			{
				markerArray[arrayPosition].show()
			}
			else
			{
				markerArray[arrayPosition].hide()
			}
			arrayPosition++
		}
	}
}
function ReCentreGmap(lat, lon)
{

	//alert("realigning map to new Coords to "+lat+" "+lon)
	if ( ( (lat <= 91 ) && (lat >= -91) ) || ( (lon <= 181 ) &&  ( lon >= -181 ) ) )
	{
		map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(lat, lon), 13);	

	//	map.addControl(mapControl);	
	}
	else
	{
		//alert("WARNING: its appears u might be no where??")
	}
}
function toogleDetailedZoom( )
{
	if (GBrowserIsCompatible()) 
	{
		//placing map.setZoom or any zoom function will fail unless they are called from in the inisalize function
		//WARNING zoom level 18 is beyond the recomended level of the API due to the fact zoom levels past 15 might not have data. 
		if ( map.getZoom() > 13 )
		{
			var newZoomLevel = 13
		//	document.getElementById('toggleZoom').innerHTML="Zoom In"
		}
		else
		{
			var newZoomLevel = 18
		//	document.getElementById('toggleZoom').innerHTML="Zoom Out"
			
		}
			//alert( newZoomLevel );
		map.setZoom( newZoomLevel );
	}
}
function setupSat()
{	

	map = new GMap2(document.getElementById("map_canvas"));
//	map.addMapType(G_SATELLITE_MAP); 

	mapControl = new GMapTypeControl();
	//map.addControl(mapControl);	
}

function removeGmapControlpanel(mapControl2)
{

	map = new GMap2(document.getElementById("map_canvas"));
	map.removeControl(mapControl2)
}

function changeMap ( type )
{
	switch ( type ) 
	{
	 case "G_MAP_TYPE":
        case 1:
			map.setMapType(G_NORMAL_MAP);		
		break;
		case 2:
			map.setMapType(G_SATELLITE_MAP);		
		break;
		case 3:
			map.setMapType(G_HYBRID_MAP);			
		break;	  
	}
}