var GoogleMaps = {
    load : function (id) {
        if (GBrowserIsCompatible()) {
            this.map = new GMap2(document.getElementById(id));
            this.map.addControl( new GSmallMapControl() );
            this.head();
        }
    },
    head : function () {
        var point = GoogleMapPoint.get('head');
        var p = new GLatLng(parseFloat(point.x),parseFloat(point.y));
        this.map.setCenter(p, 16);
        this.map.addOverlay(new GMarker(p, GoogleMapPoint.icon()));
    },
    showDirections : function (from, to) {
        this.destroy();
        this.directions = new GDirections(this.map);
        var searchFrom = GoogleMapPoint.get(from);
        var searchTo   = GoogleMapPoint.get(to);
        var searchStr  = searchFrom.x+','+searchFrom.y +' to '+searchTo.x+','+searchTo.y;
        this.directions.load(searchStr);
        GEvent.addListener(this.directions, "addoverlay", function() {
            GoogleMaps.map.setZoom(15);
        });
    },
    enterAddress : function () {
        $('form#getDirectionsForm').show();
    },
    getDrivingDirections : function () {
        this.destroy();
        this.directions = new GDirections(this.map, document.getElementById('drivingDirections'));
        //this.directions = new GDirections(this.map);
        var searchFrom = $('input#address').val();
        var searchTo   = GoogleMapPoint.get('head');
        var searchStr  = 'from: '+searchFrom+' to: '+searchTo.x +', '+searchTo.y;

        this.directions.load(searchStr);

        $('div#drivingDirections').empty();
        GEvent.addListener(this.directions, "load", function () {
            $('div#nonCarDirections').slideUp(300, function () {
                $('div#drivingDirections').slideDown(300);
            }).empty();
        });
        GEvent.addListener(this.directions, "error", function () {
            alert('Sorry, We could not find your start address.');
        });

    },
    destroy : function () {
        this.map.clearOverlays();
    },
    // New Function
    showDirectionsTottenham : function () {
        this.destroy();
        var encodedPolyline = new GPolyline.fromEncoded({
            color: "#e78200",
            weight: 8,
            points: "kxlyH~pXLdIBfAgB|AsBbB_Az@u@t@YcA",
            levels: "BBBBBBBB",
            zoomFactor: 32,
            numLevels: 4
        });
        this.map.addOverlay(encodedPolyline);
    },
    showDirectionsGoodge : function () {
        this.destroy();
        var encodedPolyline = new GPolyline.fromEncoded({
            color: "#5ea74f",
            weight: 8,
            points: "ormyHjfYxA_BvAwApAsAbBeBt@`CbAnD",
            levels: "BBBBBBB",
            zoomFactor: 32,
            numLevels: 4
        });
        this.map.addOverlay(encodedPolyline);
    }
}

var GoogleMapPoint = {
    get : function (key) {
        switch (key){
            case 'head' 			: return {
                x:51.518233,
                y:-0.133974,
                s:'Head London W1T 2DQ'
            };            							break;// HEAD LONDON
            case 'tottenhamCourtRoad' 	: return {
                x:51.516351,
                y:-0.130886,
                s:'Tottenham+Court+Road+Station+(Tottenham+Court+Road)'
            };  	break;//TOTTENHAM COURT ROAD
            case 'goodgeStreet'	 	: return {
                x:51.520553,
                y:-0.134448,
                s:'Goodge+Street+station+(Goodge+Street)'
            };  					break;//GOODGE STREET
        }
    },
    icon : function () {
        var icon = new GIcon();
        icon.image = "/images/marker.png";
        icon.iconSize = new GSize(90, 103);
        icon.shadowSize = new GSize(90, 103);
        icon.iconAnchor = new GPoint(28, 95);
        icon.infoWindowAnchor = new GPoint(0, 0);
        icon.infoShadowAnchor = new GPoint(0, 0);
        return icon;
    }
}
