/* (c) Sam Gratrix */


var google_map1;
var google_map2;

// ===========================================================================================================================
// Map functions

function createMarker(point, text)
{
  var marker = new GMarker(point);

  GEvent.addListener(marker, "click",
    function()
    {
      marker.openInfoWindow(text);
    }
  );

  return marker;
}

function placeMarkerAtPoint(point)
{
  var marker = new GMarker(point, icon);

  google_map1.addOverlay(marker);
}

function setCenterToPoint(point)
{
  google_map1.setCenter(point, 11);
}

function showPointLatLng(point)
{
  alert("Latitude: " + point.lat() + "\nLongitude: " + point.lng());
}

function mapPointInfo(map, point, target)
{
    var html = '<p>Lat, Long = ' + point.y.toFixed(6) + ', ' + point.x.toFixed(6) + '</p>';

    document.getElementById(target).innerHTML = html;
}

function mapOverlayInfo(map, point, target)
{
    var html = '<p>Lat, Long = ' + point.y.toFixed(6) + ', ' + point.x.toFixed(6) + '</p>';

    document.getElementById(target).innerHTML = html;
}


// ===========================================================================================================
// Add my own buttons to the map. A GratrixMapControl is a GControl that displays icon buttons, which is
// subclassed from the GControl by setting the prototype object to an instance of the GControl object.
// Once constructed the buttons can be added. Each button creates one div and they are all placed in a
// container div which is returned as the control element. This control is returned to the map class so that
// the buttons can be positioned.

function GratrixMapControl()
{
    this.data = new Array();

    this.addButton = function(icon, event, action)
    {
        this.data.push(new Array(icon, event, action));
    }
}

GratrixMapControl.prototype = new GControl();

GratrixMapControl.prototype.initialize = function(map)
{
  var container = document.createElement("div");

  for(key in this.data)
  {
    var button = this.data[key];

    var div = document.createElement("div");
    var img = document.createElement("img");

    var s = "16px";

    div.style.margin     = "3px";
    div.style.border     = "0";
    div.style.padding    = "0";
    div.style.width      = s;
    div.style.height     = s;
    div.style.minHeight  = s;
    div.style.lineHeight = s;

    img.src = button[0];
    img.style.margin     = "0";
    img.style.border     = "0";
    img.style.padding    = "0";
    img.style.cursor     = "pointer";
    img.style.width      = s;
    img.style.height     = s;
    img.style.minHeight  = s;
    img.style.lineHeight = s;

    container.appendChild(div);
    div.appendChild(img);

    GEvent.addDomListener(img, button[1], button[2]);
  }

  map.getContainer().appendChild(container);

  return container;
}

GratrixMapControl.prototype.getDefaultPosition = function()
{
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(2, 2));
}


// ===========================================================================================================
// Page initialise

function mapLoad()
{
  if(GBrowserIsCompatible())
  {
    var point = new GLatLng(54.622978, -2.592773);

    google_map1 = new GMap2(document.getElementById("googlemap1"), {draggableCursor: 'crosshair', draggingCursor: 'pointer'});
//  google_map1 = new GMap2(document.getElementById("googlemap1"));
    google_map2 = new GMap2(document.getElementById("googlemap2"));

//  google_map1.addControl(new GLargeMapControl());
//  google_map1.addControl(new GSmallZoomControl());
//  google_map1.addControl(new GMapTypeControl(1));
    google_map1.addControl(new GScaleControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10)));

    var map1control = new GratrixMapControl();
    map1control.addButton("icons/add.png",    "click", function() { google_map1.zoomIn();  } );
    map1control.addButton("icons/remove.png", "click", function() { google_map1.zoomOut(); } );
    google_map1.addControl(map1control);

    var map2control = new GratrixMapControl();
    map2control.addButton("icons/add.png",    "click", function() { google_map2.zoomIn();            } );
    map2control.addButton("icons/remove.png", "click", function() { google_map2.zoomOut();           } );
    map2control.addButton("icons/undo.png",   "click", function() { globalCastleMarkerSet.clobber(); } );
    google_map2.addControl(map2control);

    google_map1.setCenter(point, 1, G_SATELLITE_MAP);
    google_map2.setCenter(point, 0, G_PHYSICAL_MAP);
/*
    GEvent.addListener(google_map1, "mousemove",
      function(point)
      {
        if(point) mapPointInfo(google_map1, point, "googletxt1");
      }
    );
*/
    GEvent.addListener(google_map1, "click",
      function(overlay, point)
      {
        if(!overlay && point) mapPointInfo(google_map1, point, "googletxt1");
      }
    );
  }
}

