﻿var map = null;
var geocoder = null;
var steetLevel = 15;
var CityLevel = 10;
var UKLevel = 5
var address = "";
var prodCount = 0;
var isDrawn = false;
var localSearch = new GlocalSearch();
var host = "http://www.letsallshare.co.uk";

function LoadGoogleMap() {

    if (GBrowserIsCompatible()) {

        if ((!isDrawn) && (document.getElementById("map_canvas") != null)) {

            if (gMapURL == "") {
                gMapURL = document.URL;
            }

            $.getJSON(gMapURL, function(json) {

                //Initialise the map
                geocoder = new GClientGeocoder();
                map = new GMap2(document.getElementById("map_canvas"), { size: new GSize(gMapWidth, gMapHeight) });
                map.setUIToDefault();

                $.each(json, function(i, product) {
                    showAddress(product.Address, i);
                    address = product.Address;
                    prodCount++;
                });

                if (prodCount == 1) {
                    CentreMapOnAddress(address, steetLevel);
                } else if (prodCount > 1) {
                    showUKWide();
                }
            });

            isDrawn = true;
        }
    }
}

function CentreMapOnAddress(code, zoom) {
    if (geocoder) {
        geocoder.getLatLng(
          code,
          function(point) {
              if (!point) {
                  //Do nothing
              } else {
                  map.setCenter(point, zoom);
              }
          }
        );
    }
}

function showAddress(address, alpha) {
    if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
              if (!point) {
                  //alert("Sorry, google is unable to locate the address");
              } else {
                  var marker = GetMarker(point, alpha);
                  //GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(address); });
                  map.addOverlay(marker);
              }
          }
        );
    }
}

function GetMarker(point, alpha) {

    var baseIcon = new GIcon();
    baseIcon.shadow = host + "/Content/images/google/shadow.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);

    var letter = String.fromCharCode("A".charCodeAt(0) + alpha);
    var letterIcon = new GIcon(baseIcon);
    letterIcon.image = host + "/Content/images/google/LAS_Marker" + letter + ".png";

    var marker = new GMarker(point, { icon: letterIcon });

    return marker;

}

function showUKWide() {
    CentreMapOnAddress('Oxford , UK', UKLevel);
}

function usePointFromPostcode(postcode) {

    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);
            ShowPoint(point);
        } else {
            alert("Postcode not found!");
        }
    });

    localSearch.execute(postcode + ", UK");
}

function ShowPoint(point) {
    alert('Latitude: ' + point.lat() + '\nLongitude: ' + point.lng());
}
      
      