var GoogleMap = new Class({
    
    options: {
        lat: 53.598125,
        lng: -2.294333,
        zoom: 17,
        mapType: google.maps.MapTypeId.HYBRID,
        markers: [],
        directions: true
    },
    
    Implements: [Options],
    
    initialize: function(container, options){
        
        this.setOptions(options); 
        
        this.container = $(container);
        
        var mapArea = new Element('div', {'class': 'map'}).inject(this.container);
        
        var mapView = new google.maps.LatLng(this.options.lat, this.options.lng);
        
        var mapObj = new google.maps.Map(mapArea, {
            zoom: this.options.zoom,
            center: mapView,
            mapTypeId: this.options.mapType,
            navigationControlOptions: {
                style: google.maps.NavigationControlStyle.SMALL
            }
        });
        
        var markerObj = [], infoObj = [];
        
        this.options.markers.each(function(marker, i){
            
            markerObj[i] = new google.maps.Marker({
                position: new google.maps.LatLng(marker.lat, marker.lng),
                map: mapObj
            });
            
            if (marker.info) {
                
                infoObj[i] = new google.maps.InfoWindow({
                    content: marker.info
                });
                
                google.maps.event.addListener(markerObj[i], 'click', function(){
                    infoObj[i].open(mapObj, markerObj[i]);
                });
                
            }
            
        });
        
        if (this.options.directions == true) {
            
            var form = new Element('form', {
                action: '#',
                method: 'post',
                events: {
                    submit: function(e){
                        
                        if (e) e.preventDefault();
                        
                        var request = {
                            origin: this.getElement('input').getProperty('value'),
                            destination: mapView,
                            travelMode: google.maps.DirectionsTravelMode.DRIVING,
                            unitSystem: google.maps.DirectionsUnitSystem.IMPERIAL,
                            provideRouteAlternatives: true
                        }
                        
                        new google.maps.DirectionsService().route(request, function(response, status) {
                            
                            switch (status) {
                                
                                case google.maps.DirectionsStatus.OK:
                                    mapDirections.setDirections(response);
                                    directions.setStyle('display', 'block');
                                    break;
                                
                                case google.maps.DirectionsStatus.INVALID_REQUEST:
                                    alert('Too many waypoints were provided. The total allowed waypoints is 8, plus the origin and destination.');
                                    break;
                                
                                case google.maps.DirectionsStatus.NOT_FOUND:
                                    alert('At least one of the origin, destination, or waypoints could not be geocoded.');
                                    break;
                                
                                case google.maps.DirectionsStatus.OVER_QUERY_LIMIT:
                                    alert('The webpage has gone over the requests limit in too short a period of time.');
                                    break;
                                
                                case google.maps.DirectionsStatus.REQUEST_DENIED:
                                    alert('The webpage is not allowed to use the directions service.');
                                    break;
                                
                                case google.maps.DirectionsStatus.UNKNOWN_ERROR:
                                    alert('A directions request could not be processed due to a server error. The request may succeed if you try again.');
                                    break;
                                
                                case google.maps.DirectionsStatus.ZERO_RESULTS:
                                    alert('No route could be found between the origin and destination.');
                                    break;
                                
                            }
                            
                        });
                        
                    }
                }
            }).inject(this.container);
            
            new Element('h5', {text: 'Get Directions'}).inject(form);
            
            var p = new Element('p').inject(form);
            
            new Element('label', {text: 'Enter your postcode or address:'}).inject(p);
            new Element('input', {type: 'text', name: 'origin'}).inject(p);
            
            new Element('a', {
                href: '#',
                text: 'Show Directions',
                events: {
                    click: function(e){
                        if (e) e.preventDefault();
                        form.fireEvent('submit');
                    }
                }
            }).inject(p);
            
            var directions = new Element('div', {
                'class': 'directions',
                styles: {
                    'display': 'none'
                }
            }).inject(this.container);
            
            var mapDirections = new google.maps.DirectionsRenderer({
                map: mapObj,
                panel: directions
            });
            
        }
    }
    
});

