// member list data constructor and end-user helper methods
var Attorneys = new Array();
function Attorney(attID, attName, attTitle, attFirm, attAddr1, attAddr2, attCity, attState, attZip, attPhone, attCell, attFax, attEmail, attWebsite, attLat, attLong, attFace, attBio)
{
  var ntabs = 0;
  this.ID = attID;
  this.Name = attName;
  this.Title = attTitle;
  this.Firm = attFirm;
  this.Addr1 = attAddr1;
  this.Addr2 = attAddr2;
  this.City = attCity;
  this.State = attState;
  this.Zip = attZip;
  this.Phone = attPhone;
  this.Cell = attCell;
  this.Fax = attFax;
  this.Email = attEmail;
  this.Website = attWebsite;
  this.Latitude = attLat;
  this.Longitude = attLong;
  this.Face = attFace;
   
  this.Tabs = new Array();
// personal information
  html = "<div><table class='info'><tr>"
	+ (attFace=="" ? "" : "<td><img class='info' src='../faces/"+attFace+"'></td>")
	+ "<td class='info'><h1 class='info'>"+attName+"</h1>"
	+ (attFirm=="" ? "" : "<h2 class='info'>"+attFirm+"</h2>")
	+ (attAddr1=="" ? "" : attAddr1+"<br />")
	+ (attAddr2=="" ? "" : attAddr2+"<br />")
	+ attCity + ", " + attState + "  " + attZip + "<br /><br />"
	+ (attPhone=="" ? "" : attPhone+" office<br />")
	+ (attCell=="" ? "" : attCell+" cell<br />")
	+ (attFax=="" ? "" : attFax+" fax<br />")
	+ "<br />"
	+ (attEmail=="" ? "" : "<a class='info' href='mailto:"+attEmail+"'><b>"+attEmail+"</b></a><br />")
	+ (attWebsite=="" ? "" : "<a class='info' target='_newwindow' href='"+attWebsite+"'>"+attWebsite+"</a><br />")
	+ "</td></tr></table></div>";
  this.Tabs[ntabs++] = new GInfoWindowTab("Contact",html);

// biography (optional)
  if (attBio!=="") this.Tabs[ntabs++] = new GInfoWindowTab("Bio","<div><table class='info'><tr><td class='info'>"+attBio+"</td></tr></table></div>");

// zoom and driving directions
  this.Tabs[ntabs++] = new GInfoWindowTab("Map", "<div id='dirs"+attID+"' class='info'></div><table><tr><td align='right' class='directions'>Your<br>Address</td><td><form name='get_address'><input type='text' id='address' class='info'></form></td><td class='directions'><a href='#' onclick='window.open(getDirections(document.get_address.address.value, \""+attAddr1+","+attCity+" "+attState+"\")); return false' class='info'>Driving<br>Directions</a></td></tr></table>")
}

function add(attID, attName, attTitle, attFirm, attAddr1, attAddr2, attCity, attState, attZip, attPhone, attCell, attFax, attEmail, attWebsite, attLat, attLong, attFace, attBio)
{
	Attorneys[Attorneys.length] = new Attorney(attID, attName, attTitle, attFirm, attAddr1, attAddr2, attCity, attState, attZip, attPhone, attCell, attFax, attEmail, attWebsite, attLat, attLong, attFace, attBio)
}

function DisplayAttorneys()
{
  var html = "<table width='100%' class='attorneys'>";
  for (var i=0; i<Attorneys.length; i++)
    {
		var atty = Attorneys[i].Name;
		var idx = atty.lastIndexOf(" ")
		html+="<tr><td class='attorneys' onclick='clickListLink("+i+"); return false;'>"+atty.substr(0,idx+1)+"<span class='highlight'>"+atty.substr(idx+1,1)+"</span>"+atty.substr(idx+2)+"</td></tr>"
    }
	html+="</table>";
	document.getElementById("memberlist").innerHTML = html;
}

// Google Maps processing methods
var markers = [];
var bounds;

function clickListLink(x) {
	GEvent.trigger(markers[x], 'click');
	if (self.location.href.indexOf('&maponly=1') > 0) map.setZoom(map.getZoom()-1);
	return false;
}

function getDirections (fromAddr, toAddr) {
	if (fromAddr=="") {
		alert('Please enter a starting address in the box provided.'); 
		document.address.focus();
		return false;
	}
    fromAddr = fromAddr.replace(/\+/g, '%2B');
    fromAddr = fromAddr.replace(/ /, '+');
    return 'http://maps.google.com/?q=from:+' + fromAddr + "+to:+" + toAddr;
}

function centerAndZoomOnBounds() {
  var center = bounds.getCenter();
  map.closeInfoWindow();
  var newZoom = map.getBoundsZoomLevel(bounds)-1;
  if (map.getZoom() != newZoom) {
	map.setCenter(center, newZoom);
  } else {
	map.panTo(center);
  }
}

function createMarker(id,point,tabs) {
  var marker = new GMarker(point, bIcon);
  GEvent.addListener(marker, "click", 
	function() {
	  marker.openInfoWindowTabsHtml(tabs)
	  var zoom = new GMap2(document.getElementById("dirs"+id));
	  zoom.setCenter(point, 16);
      zoom.addControl(new GSmallMapControl());
//    zoom.addControl(new GMapTypeControl());
      zoom.setMapType(G_HYBRID_MAP);
      var zoomMarker = new GMarker(point, bIcon, true);
	  zoom.addOverlay(zoomMarker)
	}
  );
  markers[id] = marker;
  return marker;
}

var map;
var bounds;
var points;

function initMap() {

  if (!GBrowserIsCompatible()) return false; // assure browser compatability with Google Maps

// generate map
  map = new GMap2(document.getElementById('map'));
  map.addControl(new GLargeMapControl());
//map.addControl(new GMapTypeControl());
//map.addControl(new GOverviewMapControl());
  map.setCenter(new GLatLng(mapLatitude, mapLongitude), 9);
  map.setMapType(G_NORMAL_MAP);

// initialize flag icon
  bIcon = new GIcon();
  bIcon.image = '../images/mapg.png';  // blue hue pointer [mapr, mapy, mayg also avail]
  bIcon.iconSize = new GSize(20,34);
  bIcon.shadow = '../images/shadow.png';  // shadow image related to the series above
  bIcon.shadowSize = new GSize(37,34);
  bIcon.iconAnchor = new GPoint(9,34);
  bIcon.infoWindowAnchor = new GPoint(9,2);
  bIcon.infoShadowAnchor = new GPoint(18,25);

// create point set and infobox from attorneys list
  points = new Array();
  for (var i=0; i<Attorneys.length; i++)
  {
	var atty = Attorneys[i];
	var point = new GLatLng(atty.Latitude, atty.Longitude, true);
  	var marker = createMarker(atty.ID, point, atty.Tabs);
	map.addOverlay(marker);
	
	points[i] = point;
	markers[i] = marker;
  }

// optimize display based on included values
  bounds = new GLatLngBounds();
  for (var j=0; j<points.length; j++) bounds.extend(points[j]);
  centerAndZoomOnBounds()
}