var geoZoom = 12;
var geoPoints = new Array();
var geoMarkers = new Array();

function addGeoPoint(flLatitude, flLongitude, strTitle, intID, intDistance, strDescription)
{
  var objPoint = new Object();
  objPoint.Latitude = flLatitude;
  objPoint.Longitude = flLongitude;
  if (typeof(strTitle) != "undefined")
    objPoint.Title = strTitle;
  if (typeof(intID) != "undefined")
    objPoint.ID = intID;
  if (typeof(intDistance) != "undefined")
    objPoint.Distance = intDistance;
  if (typeof(strDescription) != "undefined")
    objPoint.Description = strDescription;
  geoPoints[geoPoints.length] = objPoint;
}

function setGeoZoom(intDistance)
{
  if (intDistance > 51)
    geoZoom = 7;
  else if (intDistance > 21)
    geoZoom = 8;
  else if (intDistance > 11)
    geoZoom = 9;
  else if (intDistance > 6)
    geoZoom = 10;
  else if (intDistance > 3)
    geoZoom = 11;
}

function geoMapLoad()
{
  if (GBrowserIsCompatible())
  {
    var letteredIcon = new GIcon(G_DEFAULT_ICON, "http://maps.google.com/intl/en_uk/mapfiles/icon_green.png");
    letteredIcon.iconSize = new GSize(24, 38);
    var markerOptions = { icon:letteredIcon };

    var map = new GMap2(document.getElementById("maplocation"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.addControl(new GScaleControl());
    for (var intIdx = 0; intIdx < geoPoints.length; intIdx++)
    {
      var point = new GLatLng(geoPoints[intIdx].Latitude, geoPoints[intIdx].Longitude);
      if (intIdx == 0)
      {
        map.setCenter(point, geoZoom);
        if (geoPoints.length > 1)
          var marker = new GMarker(point, markerOptions);
        else
          var marker = new GMarker(point);
      }
      else
        var marker = new GMarker(point);
      marker.html = getMarkerHTML(geoPoints[intIdx]);
      map.addOverlay(marker);
      GEvent.addListener(marker, "click", function() {
        this.openInfoWindowHtml("<p>" + this.html + "</p>");
      });
      geoMarkers[geoMarkers.length] = marker;
    }
 }
}

function getMarkerHTML(geoPoint)
{
  var strHTML = "<strong>" + geoPoint.Title + "</strong>";
  if (typeof(geoPoint.Description) != "undefined")
    strHTML += "<br>" + geoPoint.Description;
  if (typeof(geoPoint.Distance) != "undefined")
  {
    strHTML += "<br><small>" + geoPoint.Distance + " mile" + (parseInt(geoPoint.Distance) > 1 ? "s" : "");
    if (typeof(geoPoint.ID) != "undefined")
      strHTML += " [<a href=\"viewvacancies.cfm?ID=" + geoPoint.ID + "\">Full details</a>]";
    strHTML += "</small>";
  }
  else if (typeof(geoPoint.ID) != "undefined")
    strHTML += "<br><small>[<a href=\"viewvacancies.cfm?ID=" + geoPoint.ID + "\">Full details</a>]</small>";
  return strHTML;
}

function findLocation(intIndex)
{
  GEvent.trigger(geoMarkers[intIndex], "click");
}

function toggleGeoMap()
{
  var mapDiv = document.getElementById("maplocation");
  mapDiv.style.display = (mapDiv.style.display == "") ? "none" : "";
}

function addLoadEvent(func)
{
  var oldonload = window.onload;
  if (typeof window.onload != "function")
    window.onload = func;
  else
    window.onload = function() {
      oldonload();
      func();
    }
}

function addUnLoadEvent(func)
{
  var oldonunload = window.onunload;
  if (typeof window.onunload != "function")
    window.onunload = func;
  else
    window.onunload = function() {
      oldonunload();
     func();
   }
}

