var google_map;
var google_search = new GlocalSearch();

var icon = new GIcon();
icon.image      = "http://www.google.com/mapfiles/marker.png";
icon.shadow     = "http://www.google.com/mapfiles/shadow50.png";
icon.iconSize   = new GSize(20, 34);
icon.shadowSize = new GSize(37, 34);
icon.iconAnchor = new GPoint(10, 34);


// ===========================================================================================================================
// Search for place using google and then update the map.

function usePointFromPostcode(postcode, callbackFunction)
{
  google_search.setSearchCompleteCallback(null,
    function()
    {
      if(google_search.results[0])
      {
        var resultLat = google_search.results[0].lat;
        var resultLng = google_search.results[0].lng;
        var point     = new GLatLng(resultLat, resultLng);

        callbackFunction(point);
      }
      else
      {
        alert("Address not found!");
      }
    }
  );

  google_search.execute(postcode + ", UK");
}


function map_search_mark_center(address)
{
  google_search.setSearchCompleteCallback(null,
    function()
    {
      if(google_search.results[0])
      {
        var resultLat = google_search.results[0].lat;
        var resultLng = google_search.results[0].lng;

        var point = new GLatLng(resultLat, resultLng);

        var marker = createMarker(point, address);

        google_map.addOverlay(marker);

        google_map.setCenter(point, 11);
      }
      else
      {
        alert(address + " not found!");
      }
    }
  );

  google_search.execute(address);
}


// ===========================================================================================================================
// Map functions

function createMarker(point, text)
{
  var marker = new GMarker(point);

  GEvent.addListener(marker, "click",
    function()
    {
      marker.openInfoWindowHtml(text);
    }
  );

  return marker;
}

function placeMarkerAtPoint(point)
{
  var marker = new GMarker(point, icon);

  google_map.addOverlay(marker);
}

function setCenterToPoint(point)
{
  google_map.setCenter(point, 11);
}

function showPointLatLng(point)
{
  alert("Latitude: " + point.lat() + "\nLongitude: " + point.lng());
}

function mapLoad()
{
  if(GBrowserIsCompatible())
  {
    google_map = new GMap2(document.getElementById("googlemap"));

    google_map.addControl(new GLargeMapControl());
    google_map.addControl(new GMapTypeControl());

    google_map.setCenter(new GLatLng(54.622978,-2.592773), 5, G_HYBRID_MAP);
  }
}


// ===========================================================================================================================
// Page initialise

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();
    }
  }
}




