//<![CDATA[

google.load("maps", "2");
googleMaps = {
    map : null, 
    gdir : null,
    initial : null,
    initial2 : null,
    initialize : function() { 
        this.initial = false;
        this.initial2 = false;
        this.map = new google.maps.Map2(document.getElementById("map"));
        this.map.setCenter(new google.maps.LatLng(50.880077,-1.073914),8);
        this.gdir = new GDirections(this.map, document.getElementById("directions"));
        GEvent.addListener(this.gdir, "load", function(){googleMaps.onGDirectionsLoad();});
        GEvent.addListener(this.gdir, "error", function(){googleMaps.handleErrors();});
        if ($('fromAddress').value!='' && $('toAddress').value!='') {
            this.setDirectionsUsesCoordinates($('fromAddress').value, $('toAddress').value);
        }
        if($('calc_distance_button')) { $('calc_distance_button').disabled = 0; }
        //this.setDirections("San Francisco", "Mountain View", "en_US");
    },
    setDirections : function(fromAddress, toAddress, locale) {
        if(this.initial) return false;
        this.initial = true;
        if(!locale) locale = 'en';
        this.gdir.load("from: " + fromAddress + " to: " + toAddress,
                        {" locale: ": locale });
    },
    setDirectionsUsesCoordinates : function(fromPostcode, toPostcode, locale) {
        if(this.initial2) return false;
        this.initial2 = true;
        if(!locale) locale = 'en';
        new Ajax.Request(
            baseurl+'/forms/getpostcodecoordinates/do', 
            {
                parameters : 'p1='+fromPostcode+'&p2='+toPostcode,
                method: "post",
                onComplete: function(xmlhttp) {
                    googleMaps.initial2 = false;
                    xmlhttp.responseText.evalScripts();                    
                    var from = '';
                    var to = '';
                    if(__postcodes.p1.x == '' || __postcodes.p1.y == '') {
                        from = 'uk, '+__postcodes.p1.p;
                    } else {
                        from = __postcodes.p1.x+' '+__postcodes.p1.y;
                    }
                    if(__postcodes.p2.x == '' || __postcodes.p2.y == '') {
                        to = 'uk, '+__postcodes.p2.p;
                    } else {
                        to = __postcodes.p2.x+' '+__postcodes.p2.y;
                    }                    
                    googleMaps.setDirections(from,to);
                }
            }
        );
    },
    onGDirectionsLoad : function() {
        this.initial = false;
        var distance = this.gdir.getDistance().html;
        distance = distance.replace(/([0-9\.\,]{1,}).*/,"$1");
        distance = distance.replace(/\,/,".");
        //distance = (Math.round(100*distance/1.609344)/100); //distance in miles 
        $('distance_of_move').value = distance; 
        $('distance_of_move_h').value = distance; 

        $('fromAddress_h').value = $('fromAddress').value; 
        $('toAddress_h').value = $('toAddress').value; 
    },
    handleErrors : function() {
        this.initial = false;
        switch(this.gdir.getStatus().code) {
            case G_GEO_UNKNOWN_ADDRESS:
                alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.");
                break;
            case G_GEO_SERVER_ERROR:
                alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.");
                break;
            case G_GEO_MISSING_QUERY:
                alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.");
                break;
            /*case G_UNAVAILABLE_ADDRESS:
                alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
                break;*/
            case G_GEO_BAD_KEY:
                alert("The given key is either invalid or does not match the domain for which it was given.");
                break;
            case G_GEO_BAD_REQUEST:
                alert("A directions request could not be successfully parsed.");
                break;
            default:
                alert("An unknown error occurred.");
                break;
        }
        $('distance_of_move').value = '';
    }
};
google.setOnLoadCallback(function(){googleMaps.initialize();});

//]]>
