/* This Javascript is based on code provided by the Blackpool Community Church Javascript Team
	http://www.commchurch.freeserve.co.uk/   
	http://www.econym.demon.co.uk/googlemaps/ 
*/

var map;
var geocoder = null;

/* Arrays to hold copies of the markers and html used by the side_bar */
var gmarkers = [];
var htmls = [];
var i = 0;
/* Arrays to hold variants of the info window html with get direction forms open */
var to_htmls = [];
var from_htmls = [];

var zoom;
var infoSeparator = "|";
var fullInfoCount = 5; /* count of info parts that include lat/lng (pattern of line - lat|lng|address|html|link) */
var partInfoCount = 3; /* count of info parts that don't include lat/lng (pattern of line - address|html|link) */

function loadMap(mapId, startPointLat, startPointLng, region, markersData, zm, markerImageSrc)
{
  zoom = zm;
  if (GBrowserIsCompatible())
  {
    // create the map
    map = new GMap2(document.getElementById(mapId));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    geocoder = new GClientGeocoder();

    if ((startPointLat != 0) && (startPointLng != 0))
    {
      var point = new GLatLng(startPointLat, startPointLng);
      map.setCenter(point, zoom);
      loadMarker(markersData, 0, markerImageSrc);
      //setTimeout('loadMarker(markersData, 0, "' + markerImageSrc + '")', 1500); // <- delay helps to display under IE7
    }
    else if (geocoder)
    {
      geocoder.getLatLng(region, function(point)
      {
        if (!point)
        {
          //address not found
        }
        else
        {
          map.setCenter(point, zoom);
          loadMarker(markersData, 0, markerImageSrc);
          //setTimeout('loadMarker(markersData, 0, "' + markerImageSrc + '")', 1500); // <- delay helps to display under IE7
        }
      });
    }
  }
  else
  {
    alert("Sorry, the Google Maps API is not compatible with this browser");
  }
}

function loadMarker(markersData, markerIndex, markerImageSrc)
{
  if (markerIndex < markersData.length)
  {
    parts = markersData[markerIndex].split(infoSeparator);
    markerIndex++;
    /* Lat/lng were given in a data string */
    if (parts.length == fullInfoCount)
    {
      var lat = parseFloat(parts[0]);
      var lng = parseFloat(parts[1]);
      var addr = parts[2];
      var html = parts[3];
      var link = parts[4];
      var point = new GLatLng(lat, lng);
      addMarker(point, html, markerImageSrc, link);
      loadMarker(markersData, markerIndex, markerImageSrc);
    }
    /* Lat/lng were not given in a data string so it's necessary to learn them from address */
    else if (parts.length == partInfoCount)
    {
      var addr = parts[0];
      var html = parts[1];
      var link = parts[2];
      if (geocoder)
      {
        geocoder.getLatLng(addr, function(point)
        {
          if (!point)
          {
            //address not found
          }
          else
          {
            addMarker(point, html, markerImageSrc, link);
          }
          loadMarker(markersData, markerIndex, markerImageSrc);
        });
      }
    }
  }
}

function addMarker(point, html, markerImageSrc, link)
{
 var htmlToMarker = "<img src='" + markerImageSrc + "'></img>";
 htmlToMarker += "<br/><br/><br/>";
 htmlToMarker += html;
 htmlToMarker += "<br/>";
//  var htmlToMarker = "<table><tr>";
  //htmlToMarker += "<td/><td><img src='" + markerImageSrc + "'></img></td></tr>";				
 // htmlToMarker += "<tr><td>";
//  htmlToMarker += html;
//  htmlToMarker += "<br/></td><td/></tr></table>";
  if (link != "")
  {
    htmlToMarker += "<a href='" + link + "'>Go to Community Details</a>";
    htmlToMarker += "<br/>";
  }

  // The info window version with the "to here" form open
  to_htmls[i] = htmlToMarker + '<br>Get directions: <b>To here</b> - <a href="javascript:fromhere(' + i + ')">From here</a>' +
    '<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
    '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
    '<INPUT value="Get Directions" TYPE="SUBMIT">' +
    '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + 
    // "(" + name + ")" + 
    '"/>';
  // The info window version with the "to here" form open
  from_htmls[i] = htmlToMarker + '<br>Get directions: <a href="javascript:tohere(' + i + ')">To here</a> - <b>From here</b>' +
    '<br>End address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
    '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
    '<INPUT value="Get Directions" TYPE="SUBMIT">' +
    '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
    // "(" + name + ")" + 
    '"/>';
  // The inactive version of the direction info
  htmlToMarker += '<br/>Get directions: <a href="javascript:tohere(' + i + ')">To here</a> - <a href="javascript:fromhere(' + i + ')">From here</a>';

  var marker = createMarker(point, htmlToMarker);
  map.addOverlay(marker);
  if (link == "")
  {
    marker.openInfoWindowHtml(htmlToMarker);
  }
}

/* A function to create the marker and set up the event window */
function createMarker(point, html)
{
  var marker = new GMarker(point);
  GEvent.addListener(marker, "click", function()
  {
    marker.openInfoWindowHtml(html);
  });
  gmarkers[i] = marker;
  htmls[i] = html;
  i++;
  return marker;
}
      
/*  Functions that open the directions forms */
function tohere(i) {
  gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}

function fromhere(i) {
  gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}