
window.addEvent('domready', function(){

    if (GBrowserIsCompatible()) {
        
        var mapView = new GLatLng(51.864504, -2.242466);
        
        mapObj = new GMap2($('map'));
        
        mapObj.setCenter(mapView, 17, G_NORMAL_MAP);
        
        mapObj.enableDoubleClickZoom();
        mapObj.enableContinuousZoom();
        mapObj.enableScrollWheelZoom()
        mapObj.addControl(new GLargeMapControl());
        mapObj.addControl(new GMapTypeControl());
        
        var baseIcon = new GIcon(G_DEFAULT_ICON);
        mapObj.addOverlay(new GMarker(mapView, {icon:new GIcon(G_DEFAULT_ICON)}));
        
        mapObj.addOverlay(new GMarker(mapView, {icon:baseIcon}));
        
        var mapDirections = new GDirections(mapObj, $('directions'));
        
        GEvent.addListener(mapDirections, 'error', function() {
            
            var errorCode = mapDirections.getStatus().code;
            var error = '\n\nError Code: ' + errorCode;
            
            switch (errorCode) {
                case G_GEO_SUCCESS:              error = 'No errors occured!' + error; break;
                case G_GEO_BAD_REQUEST:          error = 'The directions are too complicated.' + error; break;
                case G_GEO_SERVER_ERROR:         error = 'Unknown Google Maps error.' + error; break;
                case G_GEO_MISSING_QUERY:        error = 'No address was specified.' + error; break;
                case G_GEO_UNKNOWN_ADDRESS:      error = 'Unable to find the specified address.' + error; break;
                case G_GEO_UNAVAILABLE_ADDRESS:  error = 'Unable to provide directions from the specified address.' + error; break;
                case G_GEO_UNKNOWN_DIRECTIONS:   error = 'Unable to compute directions from the specified address.' + error; break;
                case G_GEO_BAD_KEY:              error = 'Internal server error.' + error; break;
                case G_GEO_TOO_MANY_QUERIES:     error = 'Internal server error.' + error; break;
            }
            
            alert(error);
        });
        
    }
    
    $('getDirections').addEvent('submit', function(e){
        
        if (e) e.preventDefault();
        
        var fromEl = $$('input[name=directions_from]')[0];
        
        if (fromEl) {
            
            var from = fromEl.getProperty('value');
            
            mapDirections.clear();
            mapDirections.load('from: ' + from + ' to: 51.864504, -2.242466');
            
        }
    });
    
});
