var dblGoogleMapMarkerLat = 0;
var dblGoogleMapMarkerLng = 0;

    //var map = null;
    var geocoder = null;
    var mapTiny = new Array();

    google.load('maps' , '2');
    google.load('search' , '1');

    var map;
    var searcher;
    var searchControl;

function GM()
{
  var controlContainer = document.createElement('div'); // build the control div
  controlContainer.style.width = '350px'; // set the control width

  // We're ready to build our map...
  var m = $("#map")[0];
  map = new google.maps.Map2(m);

  // ...and add a couple of controls.
  map.addControl(new google.maps.LargeMapControl()); // Add a small map control
  map.addControl(new google.maps.MapTypeControl()); // Add the map type control

  // We'll wait to the end to actually initialize the map
  // So let's build the search control
  searchControl = new google.search.SearchControl();

  // Initialize a LocalSearch instance
  searcher = new google.search.LocalSearch(); // create the object
  searcher.setCenterPoint(map); // bind the searcher to the map

  // Create a SearcherOptions object to ensure we can see all results
  var options = new google.search.SearcherOptions(); // create the object
  options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);

  // Add the searcher to the SearchControl
  searchControl.addSearcher(searcher , options);

 // And second, we need is a search complete callback!
  searchControl.setSearchCompleteCallback(searcher , function()
  {
    map.clearOverlays();
    var results = searcher.results; // Grab the results array

    if (results.length>0)
    {
        var result = results[0]; // Get the specific result
        document.getElementById('hidpcalat').value = result.lat;
        document.getElementById('hidpcalon').value = result.lng;
        $('#results').val(results.length);
        var markerLatLng = new google.maps.LatLng(parseFloat(result.lat), parseFloat(result.lng));
        //var marker = new google.maps.Marker(markerLatLng, {draggable: true} ); // Create the marker

        // Bind information for the infoWindow aka the map marker popup
        /*marker.bindInfoWindow(result.html.cloneNode(true));
        result.marker = marker; // bind the marker to the result
        map.addOverlay(marker); // add the marker to the map
        */
        GoogleMapMarker(result.lat, result.lng, true);
        map.setCenter(new google.maps.LatLng(parseFloat(result.lat), parseFloat(result.lng)), 15);
    } else {
        $('#results').val('None');
    }

  });

  // Draw the control
  searchControl.draw(controlContainer);

  // Set the map's center point and finish!
  //map.setCenter(new google.maps.LatLng(37.443915 , -122.163610), 11);
  map.setCenter(new google.maps.LatLng(0, 0), 11);

}

function MapPostcode()
{
  var controlContainer = document.createElement('div'); // build the control div
  controlContainer.style.width = '350px'; // set the control width
  searchControl = new google.search.SearchControl();
  searcher = new google.search.LocalSearch(); // create the object
  var options = new google.search.SearcherOptions(); // create the object
  options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
  searchControl.addSearcher(searcher , options);
  searchControl.setSearchCompleteCallback(searcher , function()
  {
    var results = searcher.results; // Grab the results array

    if (results.length>0)
    {
        var result = results[0]; // Get the specific result
        document.getElementById('hidlat').value = result.lat;
        document.getElementById('hidlon').value = result.lng;
        //alert(result.lat);
        $('#salondirectoryform').submit();
    } else {
        alert("No Matches");
    }
  });
  searchControl.draw(controlContainer);
}

function GoMap()
{
    MapPostcode();
    var pc = document.getElementById('txtpostcode').value;
    searchControl.execute(pc + ', UK');
}
    function GoogleMap()
    {
      if (GBrowserIsCompatible())
      {
        map = new GMap2(document.getElementById("map"));
        //map.setCenter(new GLatLng(52.905,-1.258), 13);
        //map.setCenter(new GLatLng(52.89258,-1.2599), 15);
        //map.setCenter(new GLatLng(52.898962,-1.301193), 11);
        //map.setCenter(new GLatLng(52.89258226860718, -1.2604424357414246), 11);
        map.setUIToDefault();
        
        // Create our "tiny" marker icon
        //map.openInfoWindowHtml(new GLatLng(52.89258226860718, -1.2604424357414246), "<div class='map_balloon'><b>eShotz Ltd<\/b><br\/>31-33 Meadow Lane, Long Eaton<br\/>Nottingham, NG10 2FE<br\/><img src='\/_admin\/_images\/offices_200.jpg' width='200' height='102' alt='eShotz Offices' \/><\/div>");
        }
    }
    function GoogleMapAdd(id, lat, lon, zoom)
    {
      if (GBrowserIsCompatible())
      {
        var i = mapTiny.length;
        mapTiny[i] = new GMap2(document.getElementById(id));
        mapTiny[i].setCenter(new GLatLng(lat,lon), zoom);
        //mapTiny[i].setUIToDefault();
        
        var icon = new GIcon();
        icon.image = "http://www.google.com/mapfiles/marker.png";
        icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
        icon.iconSize = new GSize(12, 20);
        icon.shadowSize = new GSize(22, 20);
        icon.iconAnchor = new GPoint(6, 20);
        icon.infoWindowAnchor = new GPoint(5, 1);
        
        var point = new GLatLng(lat, lon);
        
        mapTiny[i].addOverlay(new GMarker(point, icon));
        }        
    }
    function GoogleMapMarker(Lat, Lng, draggable)
    {
        var icon = new GIcon();
        //icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
        icon.image = "http://www.google.com/mapfiles/marker.png";
        icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
        icon.iconSize = new GSize(12, 20);
        icon.shadowSize = new GSize(22, 20);
        icon.iconAnchor = new GPoint(6, 20);
        icon.infoWindowAnchor = new GPoint(5, 1);
        
        var point = new GLatLng(Lat, Lng);        
        var marker = new GMarker(point, {draggable: draggable});

        GEvent.addListener(marker, "dragstart", function()
        {
          //map.closeInfoWindow();
        });

        GEvent.addListener(marker, "dragend", function()
        {
            document.getElementById('hidlat').value = marker.getPoint().lat();
            document.getElementById('hidlon').value = marker.getPoint().lng();
          //marker.openInfoWindowHtml("Just bouncing along...");
        });
    
        map.addOverlay(marker);
    }
    function GoogleMapCenter(Lat, Lng, Zoom)
    {
        map.setCenter(new GLatLng(Lat, Lng), Zoom);
    }
    
    function GoogleMapAddress(address)
    {
        /*geocoder = new GClientGeocoder();
        geocoder.getLatLng( address, function(point)
        {
            if (!point)
            {
                alert(address + " not found");
            } else {
                $('#points').val(point);
                map.setCenter(point, 13);
                var marker = new GMarker(point);
                map.addOverlay(marker);
                marker.openInfoWindowHtml(address);
            }
        });
        */
        searchControl.execute(address);
    }
    function GoogleMapType(strType)
    {
        switch (strType)
        {
            case 'Map':
                map.setMapType(G_NORMAL_MAP);
                break;
            case 'Satellite':
                map.setMapType(G_SATELLITE_MAP);
                break;
            case 'Hybrid':
                map.setMapType(G_HYBRID_MAP);
                break;
            case 'Terrain':
                map.setMapType(G_PHYSICAL_MAP);
                break;
        }
    }
    function GoogleMapZoom(zoom)
    {
        map.setZoom(zoom);
    }

    function GoogleMapLocation(address)
    {
        geocoder = new GClientGeocoder();
        geocoder.getLocations(address, GoogleMapLocationShow);
    }

    function GoogleMapLocationShow(response)
    {
        map.clearOverlays();
        if (!response || response.Status.code != 200)
        {
            alert("\"" + response + "\" not found");
        } else {
            place = response.Placemark[0];
            dblGoogleMapMarkerLat = place.Point.coordinates[1];
            dblGoogleMapMarkerLng = place.Point.coordinates[0];
            point = new GLatLng(dblGoogleMapMarkerLat, dblGoogleMapMarkerLng);
            marker = new GMarker(point);
            map.addOverlay(marker);
            map.setCenter(new GLatLng(dblGoogleMapMarkerLat, dblGoogleMapMarkerLng), map.getZoom());
            //marker.openInfoWindowHtml(place.address + '<br><b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
          
            var p;
            p = p + place.Point.coordinates[1];
            p = p + ",";
            p = p + place.Point.coordinates[0];
            $('#points').val(point);
            
            //alert(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber);
            //alert(place.toString());
        }
    }