﻿var map;
var localSearch = new GlocalSearch();

var RadiusMetresTop = 0
var RadiusMetresBottom = 0
var RadiusMetresLeft = 0
var RadiusMetresRight = 0
var defaultZoom = 15

function usePointFromPostcode(postcode, radius, 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);
				setCenterToPoint(point, radius, resultLng, resultLat);
			}else{
				alert("Postcode not found!");
			}
		});	
		
	localSearch.execute(postcode + ", UK");
}

function placeMarkerAtPoint(point)
{
	var marker = new GMarker(point,icon);
	map.addOverlay(marker);
}

function setCenterToPoint(point, radius, resultLng, resultLat)
{   
    if (radius == 10)
    {
        destination2(resultLng, resultLat, radius, 11, point);
    }
    else if (radius == 20)
    {
        destination2(resultLng, resultLat, radius, 10, point);
    }
    else if (radius == 30)
    {
        destination2(resultLng, resultLat, radius, 9, point);
    }
    else if (radius == 40)
    {
        destination2(resultLng, resultLat, radius, 8, point);
    }
    else if (radius == 50)
    {
        destination2(resultLng, resultLat, radius, 7, point);
    }
    else
    {
        destination2(resultLng, resultLat, radius, 17, point);
    }
}

function showPointLatLng(point)
{
	alert("Latitude: " + point.lat() + "\nLongitude: " + point.lng());
}

function load() {
    var lng = 51.57361;
    var lat = -0.760276;
    var radius = 15;
    destination(lng, lat, defaultZoom);
}

function zoomin() {
    var lng = 51.57361;
    var lat = -0.760276;
    defaultZoom = defaultZoom +1;
    destination(lng, lat, defaultZoom);
}

function zoomout() {
    var lng = 51.57361;
    var lat = -0.760276;
    defaultZoom = defaultZoom -1;
    destination(lng, lat, defaultZoom);
}

function destination(lng, lat, radius) {
	if (GBrowserIsCompatible()) {
	    var baseIcon = new GIcon();
        baseIcon.image = "http://gomazdasouth.hpsdev.co.uk/html/images/mazdaIcon.gif";
        baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconSize = new GSize(20, 34);
        baseIcon.shadowSize = new GSize(37, 34);
        baseIcon.iconAnchor = new GPoint(9, 34);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);
        baseIcon.infoShadowAnchor = new GPoint(18, 25);
        
		map = new GMap2(document.getElementById("googlemap"));
		map.addControl(new GScaleControl());
		map.addControl(new GMapTypeControl());
		var postCodePoint = new GLatLng(lng,lat);
		map.setCenter(postCodePoint, radius, G_NORMAL_MAP);
        var point = new GLatLng(lng,lat);
        map.addOverlay(new GMarker(point));    
    }
}	

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();
	  }
	}
}

function createMarker(point,name) {
    var baseIcon = new GIcon();
    baseIcon.image = "http://gomazdasouth.hpsdev.co.uk/html/images/mazdaIcon.gif";
    baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    baseIcon.iconSize = new GSize(33, 26);
    baseIcon.shadowSize = new GSize(37, 34);
    baseIcon.iconAnchor = new GPoint(9, 34);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);
    baseIcon.infoShadowAnchor = new GPoint(18, 25);
    
    var icon = new GIcon(baseIcon);
    
    var markerOptions = { icon:icon };
    var marker = new GMarker(point, markerOptions);
    GEvent.addListener(marker, "click", function() {
     marker.openInfoWindowHtml(name);
    });
    return marker;
}

function myclick(i) {
        gmarkers[i].openInfoWindowHtml(htmls[i]);
      }      

addLoadEvent(load);
addUnLoadEvent(GUnload);
