
var map=null;
var geocoder=null;
//var bounds = new GLatLngBounds();

function get_object(id)
{
	var object = null;
	if (document.layers) {
		object = documentlayers[id];
	}
	else if (document.all) {
		object = document.all[id];
	}
	else if (document.getElementById) {
		object = document.getElementById(id);
	}
	return object;
}	

function zoomToFit()
{
	newzoom = map.getBoundsZoomLevel (bounds);
	newcenter = bounds.getCenter();
	map.setCenter (newcenter,newzoom);
}		

function addMarker(oPoint,strHTMLInfo)
{
	var marker = new GMarker(oPoint,{draggable:true, autoPan:false});
	
	//var strLat = oPoint[1];
	//var strLng = oPoint[0];
	//var marker = new GMarker(new GLatLng(oPoint[1],oPoint[0]));
	
	//strHTMLInfo += '<br/><b>lat:</b>' + oPoint[1] + ' <b>long:</b>' + oPoint[0];
	
	GEvent.addListener(marker, "mouseover", function() {marker.openInfoWindowHtml(strHTMLInfo);})
	map.addOverlay(marker);
	//bounds.extend(point);
	//zoomToFit();	
}


function getCallBackFor(blnNavigatable, propertyName, address, id, addressCount)
{
	//javascript function closure
	return (function(point){getLocationsCallback(blnNavigatable, propertyName, address, id, addressCount, point);}); 
}

function getLocationsCallback(blnNavigatable, propertyName, address, id, addressCount, point)
{
	try
	{
		if (point)
		{
			if (addressCount == 1)
				map.setCenter(point, 16);
			else
				map.setCenter(point, 7);
			var thisAddressHTML;
			if (addressCount==1 || !blnNavigatable)
				thisAddressHTML = "<b>" + propertyName + "</b><br/>" + address;
			else
				thisAddressHTML = "<a href='showProperty.aspx?show=" + id + "'><b>" + propertyName + "</b><br/>" + address + "</a>";

			addMarker(point,thisAddressHTML);
		}
		else
		{
			alert(propertyName + '\n' + address + "\n is not found.\n"  ); 
		}
	}
	catch(e)
	{
		alter('getLocationsCallback:\n' + e.name + '\n' + e.message);
	}
}

function showAddressX3(strJSON, strElementName) {
	try
	{
		if (GBrowserIsCompatible()) 
		{		
			map = new GMap2(document.getElementById(strElementName));
			map.setMapType(G_SATELLITE_MAP);
			map.addControl(new GMapTypeControl());
			geocoder = new GClientGeocoder();
			if(geocoder)
			{
				var JSONObj = JSON.decode(strJSON);
				var NavigatableMapPoint=JSONObj.Navigatable;

				if(JSONObj.Properties.length != 1)
					map.addControl(new GLargeMapControl());
				else
					map.addControl(new GSmallMapControl());

				for (i=0; i<JSONObj.Properties.length; i++)
				{
					var thisPropName = JSONObj.Properties[i].propName;
					var thisAddress = JSONObj.Properties[i].propAddress;
					var thisAddressId = JSONObj.Properties[i].propId;
					var thisLat = JSONObj.Properties[i].lat;
					var thisLong = JSONObj.Properties[i].long;
					var thisPoint = new GLatLng(thisLat, thisLong);

					if (thisLat == '' || thisLong == '')
					{
						if (NavigatableMapPoint)
							geocoder.getLatLng(thisAddress, getCallBackFor(true,thisPropName, thisAddress, thisAddressId,JSONObj.Properties.length))
						else
							geocoder.getLatLng(thisAddress, getCallBackFor(false,thisPropName, thisAddress, thisAddressId,JSONObj.Properties.length))
					}
					else
					{
						try
						{
							if (!thisPoint)
							{
								alert('Latitude and Longitude of ' + thisAddress + ' does not exist');
							}
							else
							{
								if (NavigatableMapPoint)
									getLocationsCallback(true, thisPropName, thisAddress, thisAddressId, JSONObj.Properties.length, thisPoint);
								else
									getLocationsCallback(false, thisPropName, thisAddress, thisAddressId, JSONObj.Properties.length, thisPoint);
							}
						}
						catch(e)
						{
							alert('1: ' + e.name + '\n' + e.message);
						}
					}					
				}//for
			}//geocode
		}
	}
	catch(e)
	{
		alert('2: ' + e.name + '\n' + e.message);
	}
}

 function addToMap(propertyName, address, id, addressCount, response) 
 { 
	if (response.Status.code == G_GEO_SUCCESS) 
  { 
    var strObj = ""; 
    var strAdd=""; 
    var divLocationList; 
    var p; 
    var LatLong; 

    if (response.Placemark.length>0) 
    { 
			for (var i=0; i<response.Placemark.length; i++) 
			{ 
				p = response.Placemark[i].Point.coordinates; 
        strAdd=address.replace(/ /g,'&nbsp;'); 
        strAdd=strAdd.replace(/'/g,'\\\''); 					
				thisAddressHTML = "<a href='showProperty.aspx?show=" + id + "'><b>" + propertyName + "</b><br/>" + address + "</a>";
        
        addMarker(response.Placemark[i].Point,thisAddressHTML);
        
        centerLatiude =p[1]; 
        centerLongitude = p[0]; 
        strObj=strObj+" <a href=javascript:load() onclick='showimage()'>" + response.Placemark[i].address + "</a><br>"; 

				divLocationList=document.getElementById('divLocationList'); 
				divLocationList.style.backgroundColor="white"; 
        divLocationList.innerHTML += strObj; 
			} 
    } 
    else 
    { 
			divLocationList=document.getElementById('divLocationList'); 
      divLocationList.style.backgroundColor="white"; 
      divLocationList.innerHTML="<font color=red>No such location in a list</font>" 
    } 
	} 
  else 
  { 
		divLocationList=document.getElementById('divLocationList'); 
    divLocationList.style.backgroundColor="white"; 
    divLocationList.innerHTML="<font color=red>No such location in a list</font>" 
  } 
  return false; 
}
