google.load("maps", "2");
function business (name, type, lat, lon, link, email, street, cityst, phone, description){
  function checknull (item) {
    if (item != "")
      return ("<br>"+item);
    return item;
  }
  this.name = name;
  this.type = type;
  this.lat = lat;
  this.lon = lon;
  this.infotext = "<div class='infoWind'>";
  if (link == "")
    this.infotext += name;
  else
    this.infotext += "<a href='"+link+"'>"+name+"</a>";
  this.infotext += checknull(street)+checknull(cityst)+checknull(phone);
  if (email != "")
    this.infotext += "<br><a href='mailto:"+email+"'>"+email+"</a>";
  if (description != ""){
    splitd = description.split(" ");
    var newd = "";
    for (i=0; i<splitd.length; i++){
      newd += splitd[i];
      if (newd.length <= 40)
        newd += " ";
      else {
        this.infotext += "<br>"+newd;
        newd = "";
      }
    }
    if (newd.length > 0)
      this.infotext += "<br>"+newd;
  }
  this.infotext += "</div>";
  this.marker = "";
}
business.prototype.recenterMap = function () {
  map.setCenter(this.marker.getLatLng());
  this.marker.openInfoWindowHtml(this.infotext);
}

business.prototype.addNewMarker = function () {
  var i = 0;
  do {
    if (this.type == markers[i].type){
      this.marker = new GMarker (new GLatLng(this.lat, this.lon), markerOptions[i]);
      i = markers.length;
    }
    i++;
  }
  while (i < markers.length);
}

function markerData (type, image, wimage, himage, shadow, wshadow, hshadow, xanchor, yanchor, xtextanchor, ytextanchor){
  this.type = type;
  this.image = image;
  this.wimage = wimage;
  this.himage = himage;
  this.shadow = shadow;
  this.wshadow = wshadow;
  this.hshadow = hshadow;
  this.xanchor = xanchor;
  this.yanchor = yanchor;
  this.xtextanchor = xtextanchor;
  this.ytextanchor = ytextanchor;
}

function updateMarker(image, wimage, himage, shadow, wshadow,
  hshadow, xanchor, yanchor, xtextanchor, ytextanchor) {
  var marker = new GIcon();
  marker.image = image;
  marker.shadow = shadow;
  marker.iconSize = new GSize(wimage, himage);
  marker.shadowSize = new GSize(wshadow, hshadow);
  marker.iconAnchor = new GPoint(xanchor, yanchor);
  marker.infoWindowAnchor = new GPoint(xtextanchor, ytextanchor);
  return marker;
}

function alertme () {
  var center = map.getCenter();
  latit = center.lat();
  longit = center.lng();
}
function startMap () {
  // Create a map object
  map = new google.maps.Map2(document.getElementById('insetmap'));
  map.setCenter(new google.maps.LatLng(38.52614,-122.31353), 11);  
  gdir = new GDirections(map, document.getElementById("directions"));
  GEvent.addListener(gdir, "error", handleErrors);
  map.addControl(new GSmallMapControl(), new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(-40,0)));
  var mapControl = new GMenuMapTypeControl();
  map.addControl(mapControl);
  // Create the markers for on the map
  for (var i = 0; i < markers.length; i++) {
    mapmarker[i] = new GIcon();
    mapmarker[i] = updateMarker(markers[i].image, markers[i].wimage, markers[i].himage,
      markers[i].shadow, markers[i].wshadow, markers[i].hshadow, markers[i].xanchor,
      markers[i].yanchor, markers[i].xtextanchor, markers[i].ytextanchor);
    markerOptions[i] = { icon:mapmarker[i] };
  }
 // Add Markers to map
  for (var i = 0; i < gbus.length; i++) {
    gbus[i].addNewMarker();
    map.addOverlay(gbus[i].marker);
    gbus[i].marker.bindInfoWindowHtml(gbus[i].infotext);
  }
}
function displayDirections (from, to, myaddress){
  var fromAddress;
  var toAddress;
  var fromoffset = -1;
  var tooffset = -1;
  for (var i = 0; i < from.length; i++)
    if (from[i].checked)
      fromoffset = i;
  if (fromoffset == -1)
    alert('Please select a "From" location.');
  else {
    for (var i = 0; i < to.length; i++)
      if (to[i].checked)
        tooffset = i;
    if (tooffset == -1)
      alert('Please select a "To" location.');
    else {
      if (fromoffset >= gbus.length)
        fromAddress = myaddress;
      else
        fromAddress = gbus[fromoffset].lat + "," + gbus[fromoffset].lon;
      if (tooffset >= gbus.length)
        toAddress = myaddress;
      else
        toAddress = gbus[tooffset].lat + "," + gbus[tooffset].lon;
      if ((fromAddress == "") || (toAddress == ""))
        alert ("Please enter an address.");
      else
        setDirections (fromAddress, toAddress);
    }
  }
}
function setDirections (fromAddress, toAddress) {
  gdir.load("from: " + fromAddress + " to: " + toAddress);
}
function handleErrors(){
  if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
    alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
  else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
    alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
  
  else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
    alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
  
  else if (gdir.getStatus().code == G_GEO_BAD_KEY)
    alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
  
  else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
    alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
  
  else alert("An unknown error occurred.");
}
var map;
var gdir;
var markerOptions = new Array();
var gbus = new Array ();
var markers = new Array();
var mapmarker = new Array();


