var geocoder  = null;
var map	   = null;
var locations = new Array();
var addresses = new Array();
var rendered  = 0;
var infowindow = null;


function propertyAddress()
{
	this.address	= "";
	this.markerHTML = "";
	this.Lattitude  = 0;
	this.Longitude  = 0;
}


function propertyMap()
{
	geocoder  = null;
	map	   = null;
	locations = new Array();
	addresses = new Array();
	rendered  = 0;
}

  propertyMap.prototype.initialize = function() 
					{
						var latlng = new google.maps.LatLng(-34.397, 150.644);
					
						var myOptions = {
					      zoom: 8,
					      /*center: latlng,*/
					      mapTypeId: google.maps.MapTypeId.ROADMAP,
						  panControl: 1,
						  zoomControl: 1,
						  mapTypeControl: 1,
						  scaleControl: 1,
						  streetViewControl: 1,
						  panControlOptions: { position: (addresses.length==1?google.maps.ControlPosition.BOTTOM_RIGHT:google.maps.ControlPosition.TOP_LEFT) },
  						  zoomControlOptions: { position: google.maps.ControlPosition.TOP_LEFT, style: (addresses.length==1?google.maps.ZoomControlStyle.LARGE:google.maps.ZoomControlStyle.LARGE) },
						  streetViewControlOptions: {position: google.maps.ControlPosition.TOP_RIGHT} 

					    };
					    
						map	  = new google.maps.Map(document.getElementById("map_canvas2"), myOptions);
						geocoder = new google.maps.Geocoder();
						infowindow = new Ebubble(map, "/images/icon_gmap-infowindow-directions.png", new google.maps.Size(251,147), new google.maps.Size(219,115), new google.maps.Point(12,12), new google.maps.Point(22,134), 1);
				   }

	propertyMap.prototype.addAddress = function(addressObj)
					{
						addresses[addresses.length]	= addressObj;
					}

	centerMap = function()
					{
							bounds = new google.maps.LatLngBounds();
							
							for(i=0; i < locations.length; i++)
							{
								bounds.extend(locations[i]);
							}

							//center = bounds.getCenter();
							//zoom   = this.map.getBoundsZoomLevel(bounds);

							map.fitBounds(bounds);
							
							if(locations.length ==1) 
							{
								map.setCenter(locations[0]); map.setZoom(16);								
							}
							var projection = map.getProjection();
							map_center = projection.fromLatLngToPoint(map.getCenter());
					}


	propertyMap.prototype.showAddress = function(addressObj)
					{
					   if(addressObj.Lattitude == '0' || addressObj.Longitude == '0' || addressObj.Lattitude == '' ||  addressObj.Longitude == '' )
					   {				

						  geocoder.geocode( { 'address': addressObj.address}, function(results, status) {
						  if (status == google.maps.GeocoderStatus.OK) 
						  {
							//intialize the information popup window
							//var infowindow = new google.maps.InfoWindow({content: addressObj.markerHTML}); //new EBubble(map, "/images/icon_gmap-infowindow-directions.png", new google.maps.Size(251,147), new google.maps.Size(219,115), new google.maps.Point(12,12), new google.maps.Point(22,134), true);//new google.maps.InfoWindow({content: addressObj.markerHTML});
							//new google.maps.InfoWindow({content: addressObj.markerHTML});
							
							//this.map.setCenter(results[0].geometry.location);

							var marker = new google.maps.Marker({
								map: map, 
								icon: "http://2.pointwidepropertymanagement.com/images/icon_idx-home.gif",
								position: results[0].geometry.location
							});

							//make the info window open on click of the marker
							google.maps.event.addListener(marker, 'click', function() {infowindow.openOnMarker(marker, addressObj.markerHTML) /*infowindow.open(map,marker);*/});
							google.maps.event.addListener(marker, 'mouseover', function() {infowindow.openOnMarker(marker, addressObj.markerHTML)});
							google.maps.event.addListener(marker, 'mouseout', function() { setTimeout(function(){ infowindow.hide();}, 3000); });

							locations[locations.length] = results[0].geometry.location;

						  } else {
							//alert("Geocode was not successful for the following reason: " + status);
						  }

						  rendered++;

 						  //all addresses rendered, set zoom and center on the map
						  if(rendered == addresses.length)
							  {
								 centerMap();								 
							  }
						});
					 }
					 else
					 {
						 var loc	=  new google.maps.LatLng(addressObj.Lattitude, addressObj.Longitude);	
						 var marker = new google.maps.Marker({
								map: map, 
								icon: "http://2.pointwidepropertymanagement.com/images/icon_idx-home.gif",
								position: loc
						 });

						google.maps.event.addListener(marker, 'click', function() {infowindow.openOnMarker(marker, addressObj.markerHTML) /*infowindow.open(map,marker);*/});
						google.maps.event.addListener(marker, 'mouseover', function() {infowindow.openOnMarker(marker, addressObj.markerHTML)});
						google.maps.event.addListener(marker, 'mouseout', function() { setTimeout(function(){ infowindow.hide();}, 3000); });


						locations[locations.length] = loc;

						rendered++;

						if(rendered == addresses.length)
						{
						 centerMap();								 
						}
					 }
				   }

	
	propertyMap.prototype.render	   = function()
					{		

						this.initialize();					
						
						j = addresses.length;

						for(i=0; i < j; i++)
						{
						
							try
							{
								this.showAddress(addresses[i]);						
							}
							catch (ex)
							{
							}																											
						}
					}


