function LawyerManager(lawyers, enableSearch)
{
	this._lawyers = lawyers;
	this._lawyerHash = [];
	
  for (var i=0; i<lawyers.length; i++)
  {
    var lawyer = lawyers[i];
        
    if (this._lawyerHash[lawyers[i].ID] == null)
			this._lawyerHash[lawyers[i].ID] = lawyer;
  }
  
  if (g_map.IsEnabled)
		this.MinZoom = Math.min(15, g_map.GetZoom());
	
	this.EnableSearch = enableSearch && g_map.IsEnabled;
  
	this.ShowOnMap = function()
	{
		for (var i=0; i<this._lawyers.length; i++)
		{
			var lawyer = this._lawyers[i];
			
			lawyer.ShowOnMap();
		}
						
		var instance = this;
		
		if (this.EnableSearch)
		{
			g_map.OnMapMoved = function(considerable, zoom) { if (considerable && zoom < 17) { instance.MapMoved(); } };
			g_map.AttachZoomHandler(function(oldLevel, newLevel) { if (newLevel < 17) instance.MapZoomed(oldLevel, newLevel); });
		}
	}
	
	this.DoSearchOnMap = function()
	{
		var b = g_map.GetBounds();	
		
		var boundsField = $get('MapBounds');
		boundsField.value = b.swLat + ',' + b.swLng + ',' + b.neLat + ',' + b.neLng;
		
		g_map.ClearMarkers();
				
		if (typeof this.SearchOnMapHandler == 'function')
			this.SearchOnMapHandler();
	}
	
	this.MapMoved = function()
	{
		this.DoSearchOnMap();
	}
	
	this.MapZoomed = function(oldLevel, newLevel)
	{	
		if (newLevel < this.MinZoom)
		{
			g_map.SetZoom(this.MinZoom);
			return;
		}
							
		if (oldLevel > newLevel)
		{
			this.DoSearchOnMap();
		}		
	}
	
	this.FindOnMap = function(id)
	{
		var lawyer = this._lawyerHash[id];
		if (lawyer != null)
		{
			lawyer.FindOnMap();
		}		
	}
}


function Lawyer(id, lat, lng, officeId)
{
  this.ID = id;
  this.Lat = lat;
  this.Lng = lng;
  this.OfficeID = officeId;
  
  this.ShowInfoPanel = function()
  {    
    var instance = this;
    web.LawyerListing.LawyerService.GetLawyer(this.ID, this.OfficeID,
			function(result, ctx) { instance.ShowInfoPanelCallBack(result, ctx) });
  }  
  
  this.ShowInfoPanelCallBack = function(result, ctx)
  {
    var div = document.createElement('div');
    
    var html = "<table cellpadding='0' cellspacing='0' class='pageBasic11' style='width:270px'>";
    
    html += "<tr><td colspan='2'><img src='Images/0.gif' height='0' width='216' /></td></tr>";
    
    html += "<tr><td rowspan='5' style='padding-right:7px;width:48px;' valign='top'><img src='" + result.PhotoUrl + "' height='48' /></td>";
    html += "<td style='width:168px'><b>" + result.FullName + "</b></td></tr>";
		html += "<tr><td style='font-size:larger;font-style:italic;'>" + result.LawFirmName + "</td></tr>";
    html += "<tr><td>" + result.AddressLine1 + "</td></tr>";
    html += "<tr><td>" + result.AddressLine2 + "</td></tr>";
		html += "<tr><td>" + result.AddressLine3 + "</td></tr>";
    
    var detailsHtml = "<a href=\"" + result.DetailsPage + "\">details</a>";
    var contactHtml = (result.ContactPage) ? "&nbsp;&nbsp;<a href=\"" + result.ContactPage + "\">contact</a>" : "";
        
    html += "<tr><td colspan='2'><hr style='width:100%'/></td></tr>";
    html += "<tr><td colspan='2' align='right' style='padding:0px 7px;'>" + detailsHtml + contactHtml + "</td></tr>";
        
    html += "</table>";
    
    div.innerHTML = html;
    
    g_map.ShowInfoWindow(this.Lat, this.Lng, div);
  }
  
  this.HideInfoPanel = function()
  {
    g_map.HideInfoWindow();
  }
  
  this.ShowOnMap = function()
  {
		if (!g_map.IsEnabled)
			return;
			
    var instance = this;
    
    // add marker
    g_map.AddMarker(this.Lat, this.Lng, g_MapLawyerIconURL, 
      { 
        'mouseover' : function() { instance.ShowInfoPanel(); }
        //'mouseout' : function() { instance.HideInfoPanel(); }
      });
    
  }
  
  this.FindOnMap = function()
  {
		if (!g_map.IsEnabled)
			return;

		g_map.SetCenter(this.Lat, this.Lng);
		
		this.ShowInfoPanel();
  }
}