Tve = {

    office: null,
    map: null,
    icon: null,
    marker: null,
    directionsPanel: null,
    directions: null,
    
    debug: function(str) {
        var console = window.console || false;
        if(console) {
            console.debug(str);
        }
    },

    init: function() {
        Tve.office = new GLatLng(51.601809, -0.634736);
    },
    
    load: function() {
        Tve.map = new GMap2(document.getElementById("map"));
        Tve.map.addControl(new GLargeMapControl());
        Tve.map.addControl(new GMapTypeControl());
        
        Tve.marker = new GMarker(Tve.office)
        Tve.directionsPanel = document.getElementById("directions_list");
        Tve.directions = new GDirections(Tve.map, Tve.directionsPanel);
        GEvent.addListener(Tve.directions, 'error', Tve.onDirectionsUnavailable);
        GEvent.addListener(Tve.directions, 'addoverlay', Tve.onDirectionsFinished);
        GEvent.addListener(Tve.directions, 'load', Tve.onDirectionsAvailable);
        Tve.resetMap(Tve.map);
    },
    
    Google: {
        geocodePostcode: function(postcode, callbackFunction) {
            var localSearch = new GlocalSearch();
            localSearch.setSearchCompleteCallback(null,
                function() {  
//                    Tve.debug("Found "+ localSearch.results.length +" results");
                    if (localSearch.results[0]) {
                        var resultLat = localSearch.results[0].lat;
                        var resultLng = localSearch.results[0].lng;
                        var point = new GLatLng(resultLat,resultLng);
//                        Tve.debug("Using " + point);
                        callbackFunction(point);
                    } else {
                        callbackFunction(null);
                    }
                }
            );

            localSearch.execute(postcode + ", UK");
        }
    },
    
    resetMap: function(map) {
        map.setCenter(Tve.office, 12);
        map.removeOverlay(Tve.marker);
        map.addOverlay(Tve.marker);
    },
    
    getDirectionsFrom: function(place) {
        if(place == ""){
            Tve.onDirectionsUnavailable(Tve.map, "Please enter something - I don't know where you're coming from!");
            return;
        };
        Tve.onDirectionsSearch(Tve.map); 
//        Tve.debug("Directions from "+place);
        Tve.Google.geocodePostcode(place, Tve.showDirectionsFrom);
    },
    
    showDirectionsFrom: function(point) {
        Tve.resetMap(Tve.map);
        if(point) {
            var directionsString = point.y+','+point.x + " to " + Tve.office.y+','+Tve.office.x;
//            Tve.debug("Directions: "+directionsString);
            Tve.directions.load(directionsString);
        } else {
            Tve.onDirectionsUnavailable(Tve.map);
        }
    },
    
    onDirectionsSearch: function(map) {
        Tve.directions.clear();
        Effect.BlindUp('directions_errors', {duration:0.2});
    },

    onDirectionsAvailable: function(map) {
    },

    onDirectionsFinished: function(map) {
        Tve.directions.getMarker(1).hide();
        window.location.hash = '#directions';
        $('directions_container').style.display = '';
    },


    onDirectionsUnavailable: function(map, error) {
        error = error || "We couldn't find directions to Tve from here. Please ensure you"+
        " have supplied a UK location &ndash; a postcode will give best"+
        " results.";
        $('directions_errors').innerHTML = error;
        Effect.BlindDown('directions_errors', {duration:0.2});
        $('saddr').blur();
        $('saddr').focus();
    }
};