﻿window.addEvent('domready', function(){  
var localSearch = new GlocalSearch();
var map = new GMap2(document.getElementById("map"));
map.enableScrollWheelZoom();
map.enableContinuousZoom();
    
//--add zoom controls
map.addControl(new GLargeMapControl());
    
//--center map to latitude longditude of office
var latLng = new GLatLng("53.476681","-2.497329",  12)
map.setCenter(latLng,12);
    
//--override google maps styles
document.getElementById("map").style.border='1px solid black';
document.getElementById("map").style.height='300px';
    
//--helper function to return a GMarker
function createInfoMarker(point, address) {
    var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(address);} );
    return marker;
}

//--helper function to GeoCode a postcode and callback with a GLatLng point
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);
            }else{
                alert("Postcode not found!");
            }
        });  
    localSearch.execute(postcode + ", UK");
}
    
//--callback function to re-center the map and add the address bubble.
function geocodeCallback(point) {
    map.setCenter(point,12);
    var address = "SASS, The Haybarn<br/>Mere Hall Park<br/>Warrington Road<br/>Mere<br/>Cheshire<br/>WA16 0PY";
    var marker = createInfoMarker(point, address);
    map.addOverlay(marker);
    //--show the address bubble
    marker.openInfoWindowHtml(address);
}
    
//--initiate geocoding
usePointFromPostcode("WA16 0PY",geocodeCallback);

});
    