var map;
var localSearch = new GlocalSearch();
var zoom = 12;
var maptype = G_NORMAL_MAP;
var icon = new GIcon(G_DEFAULT_ICON);
icon.image = "http://www.hordenparishcouncil.gov.uk/images/marker.png";
icon.shadow = "http://www.hordenparishcouncil.gov.uk/images/shadow50.png";
icon.iconSize = new GSize(20, 34);
icon.shadowSize = new GSize(37, 34);
icon.iconAnchor = new GPoint(10, 34);
icon.infoWindowAnchor = new GPoint(10,34);

var iconNM = new GIcon(G_DEFAULT_ICON);
iconNM.image = "http://www.hordenparishcouncil.gov.uk/images/marker2.png";
iconNM.shadow = "http://www.hordenparishcouncil.gov.uk/images/shadow50.png";
iconNM.iconSize = new GSize(20, 34);
iconNM.shadowSize = new GSize(37, 34);
iconNM.iconAnchor = new GPoint(10, 34);
iconNM.infoWindowAnchor = new GPoint(10,34);

function usePointFromPostcode(postcode, callbackFunction) 
{	
  localSearch.setSearchCompleteCallback(null, 
    function() {
		     if (localSearch.results[0])
		     {		
			 var resultLat = localSearch.results[0].lat;
			 var resultLng = localSearch.results[0].lng;
			 var point = new GLatLng(resultLat,resultLng);
			 callbackFunction(point,postcode);
			}
                  else
                  {
			  alert("Postcode not found!");
			}
		    });		
  localSearch.execute(postcode + ", UK");
}

function placeMarkerAtPoint(point,pc)
{
  var marker = new GMarker(point,icon);
  map.addOverlay(marker);
}

function setCenterToPoint(point)
{
  map.setCenter(point, zoom, maptype);
}

function showPointLatLng(point)
{
  alert("Latitude: " + point.lat() + "\nLongitude: " + point.lng());
}

var amenities = [];
amenities[0] = new amenity(54.7644, -1.3159, 'Horden Parish Council', 'Horden Parish Council', 'Horden Parish Council', iconNM, 0);
amenities[8] = new amenity(54.765800, -1.318900, 'Green Leaf Cafe', 'Green Leaf Cafe', '<table><tr><td><img class="border" src="amenity/27/green_leaf_thumb.jpg" alt="Green Leaf Cafe"></td><td><h1>Green Leaf Cafe</h1><a href="amenities_8.aspx">Green Leaf Cafe</a></td></tr></table>', icon, 8);
amenities[11] = new amenity(54.769306, -1.323338, 'Sunderland Road Playing Fields', 'Sunderland Road Playing Fields', '<table><tr><td><img class="border" src="amenity/33/bulb_planting_edited_thumb.jpg" alt="Bulb Planting"></td><td><h1>Sunderland Road Playing Fields</h1><a href="amenities_11.aspx">Sunderland Road Playing Fields</a></td></tr></table>', icon, 11);
amenities[12] = new amenity(54.766830, -1.319046, 'Horden Welfare Park', 'Horden Welfare Park', '<table><tr><td><img class="border" src="amenity/36/ja18_035_thumb.jpg" alt="Horden Welfare Park"></td><td><h1>Horden Welfare Park</h1><a href="amenities_12.aspx">Horden Welfare Park</a></td></tr></table>', icon, 12);
amenities[13] = new amenity(54.761680, -1.316986, 'Horden Memorial Park', 'Horden Memorial Park', '<table><tr><td><img class="border" src="amenity/39/blackhills_road_park_031_thumb.jpg" alt="Horden Memorial Park"></td><td><h1>Horden Memorial Park</h1><a href="amenities_13.aspx">Horden Memorial Park</a></td></tr></table>', icon, 13);
amenities[14] = new amenity(54.773960, -1.337585, 'Horden Cemetery', 'Horden Cemetery', '<table><tr><td></td><td><h1>Horden Cemetery</h1><a href="amenities_14.aspx">Horden Cemetery</a></td></tr></table>', icon, 14);
amenities[15] = new amenity(54.763958, -1.316471, 'Horden Social Welfare Centre', 'Horden Social Welfare Centre', '<table><tr><td><img class="border" src="amenity/44/swc_outside_thumb.jpg" alt="Entrance"></td><td><h1>Horden Social Welfare Centre</h1><a href="amenities_15.aspx">Horden Social Welfare Centre</a></td></tr></table>', icon, 15);
amenities[16] = new amenity(54.775940, -1.316643, 'Maritime Crescent Play Area', 'Maritime Crescent Play Area', '<table><tr><td><img class="border" src="amenity/48/grants_houses_003_thumb.jpg" alt="Maritime Play Area"></td><td><h1>Maritime Crescent Play Area</h1><a href="amenities_16.aspx">Maritime Crescent Play Area</a></td></tr></table>', icon, 16);


function amenity(latitude,longitude,title,tooltip,infowindow,icon,rec)
{
  this.latitude = latitude;  
  this.longitude = longitude;  
  this.title = title;  
  this.tooltip = tooltip;  
  this.infowindow = infowindow;  
  this.icon = icon;
  this.rec = rec;
  this.addMarker = function()
  {
    map.addOverlay(createInfoMarker(new GLatLng(this.latitude, this.longitude), this.tooltip, this.infowindow,this.icon,this.rec));
  }  
}

function createInfoMarker(point, title, address, ic, rec) 
{ 
  var marker = new GMarker(point,{title:title,icon:ic}); 
  GEvent.addListener(marker, "click", function() 
    { marker.openInfoWindowHtml(address); } 
  );
  GEvent.addListener(marker, "dblclick", function() 
    { if (rec != 0) window.location = "amenities_"+rec+".aspx";
    } 
  );
  return marker; 
} 

function showAmenities()
{
  for (var a in amenities)
  {
    amenities[a].addMarker(); 
  }
}

function mapLoad() 
{
  if (GBrowserIsCompatible()) 
  {
    map = new GMap2(document.getElementById("map"));		
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(54.7644, -1.3159), 13, G_NORMAL_MAP);
    showAmenities();
  }
}

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();
  }
 }
}

addLoadEvent(mapLoad);
addUnLoadEvent(GUnload);
