var map;
var directions;
var loc;

function directionsLoad() {

    if (GBrowserIsCompatible()) {
        
        map = new GMap2($("map"));
//        loc = new GLatLng(53.4232022725631, -2.9505382013909);
        loc = new GLatLng(53.4218021721515, -2.9495382013909);
        directions = new GDirections(map,$("directions"));

        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());

        

        map.setCenter(loc, 15);

        map.addOverlay( new GMarker( loc) );


        GEvent.addListener(directions, "load", onDirectionsLoad);
        GEvent.addListener(directions, "error", handleErrors);
        
    }else{
//no browser support
}

}

function getDirections(){
    directions.load("from:" + $("from_address").value + ", UK to: " + " 21 Belmont Road, Liverpool, Merseyside, L6 5BG, UK");
}


function onDirectionsLoad(){
    $("direction_results").style.display="block";
}

function handleErrors(){
    alert("An error occurred, try retyping your departure address in an alternate format\n TIP: Try without spaces in postcodes");
}

//Setup event bindings for other components and run on page load actions
Event.observe(window, 'load', function() {
    directionsLoad();

    //ensure that on submit of the directions form the getDirections function is called and the submit is disabled
    Event.observe('directions_form', 'submit', function(event) {
        //stop the submit action from carrying on
        event.stop();        
        getDirections();
    });

    //bind event to the directions form button to actually get the directions
    Event.observe('directions_form_button', 'click', function() {
        getDirections();
    });
    
    //bind event to the directions form button to actually get the directions
    Event.observe('from_address', 'keypress', function(event) {
        if(event.keyCode== Event.KEY_RETURN){
            getDirections();
        }
    });

});