
var MooMap = new Class({
     _gmap: null,
     _center: null,
     _markers: null,
     _zoom: null,
     
     Implements: [Options],
    
    options: {
        center: '46.517766,6.627488',
        zoom_level: 5,
        zoom_level_2: 10,
        map_type: 'G_NORMAL_MAP',
        map_control_type: 'GMapTypeControl',
        map_control_zoom: 'GSmallMapControl',
        overview: 0,
        dragging: 0,
        double_click: 0,
        scroll_wheel_zoom: 0,
        center_link: 'All',
        info_width: null
    },
    
    initialize: function(canvas, markers, options) {
        this.setOptions(this.options, options);
        
        this._gmap = new GMap2($(canvas));
        this._gmap.addMapType(G_PHYSICAL_MAP);
        this._gmap.setMapType(eval(this.options.map_type));
        if (this.options.map_control_type != 'None') {
            if (this.options.map_control_type == 'GMapTypeControlShort') {
                this._gmap.addControl(new GMapTypeControl(true)); 
            } else {
                this._gmap.addControl(new GMapTypeControl());
            }
        }
        if (this.options.map_control_zoom != 'None') {
            this._gmap.addControl(eval('new '+this.options.map_control_zoom+'()'));
        }
        if (this.options.overview == 1) {
            this._gmap.addControl(new GOverviewMapControl());
        }
        if (this.options.dragging == 1) {
            this._gmap.enableDragging();
        } else {
            this._gmap.disableDragging();
        }
        if (this.options.double_click == 1) {
            this._gmap.enableDoubleClickZoom();
        } else {
            this._gmap.disableDoubleClickZoom();
        }
        if (this.options.scroll_wheel_zoom == 1) {
            this._gmap.enableScrollWheelZoom();
        } else {
            this._gmap.disableScrollWheelZoom();
        }
        
        this._center = this._makePoint(this.options.center);
        this._gmap.setCenter(this._center, this.options.zoom_level);
        this._zoom = this.options.zoom_level;
        
        this._markers = [];
        if (markers) {
            markers.each(function(marker, index) {
                var point = this._makePoint(marker.location);
                var gmarker = new GMarker(point, {
                    icon:G_DEFAULT_ICON,
                    title: marker.title 
                });
                var infoWindowOpts = {};
                if (this.options.info_width) {
                    infoWindowOpts.maxWidth = this.options.info_width;
                }
                gmarker.bindInfoWindowHtml(marker.content, infoWindowOpts);
                this._markers.push(gmarker);
                this._gmap.addOverlay(gmarker);
            }.bind(this));
        }
        
        GEvent.addListener(this._gmap.getInfoWindow(), "closeclick", function() {
            this._gmap.setCenter(this._center, this.options.zoom_level_1);
        }.bind(this));
        
    },
    
    _makePoint: function(location) {
        var latLng = location.split(',', 2);
        var point = new GLatLng(parseFloat(latLng[0]), parseFloat(latLng[1]));
        return point;
    }
});

var MooSearchMap = new Class({
     _gmap: null,
     _location: null,
     _gmarker: null,
     
     Implements: [Options],
    
    options: {

    },
    
    initialize: function(canvas, location, search, options) {
        //this.setOptions(this.options, options);
        
        this._gmap = new GMap2($(canvas));
        this._location = $(location);
        
        this._gmap.addMapType(G_PHYSICAL_MAP);
        this._gmap.addControl(new GMapTypeControl());
        this._gmap.addControl(new GLargeMapControl());
        
        var latLng = this._location.value.split(',', 2);
        var point = new GLatLng(parseFloat(latLng[0]), parseFloat(latLng[1]));
        if (! isNaN(point.lng()) && ! isNaN(point.lat())) {
            this._gmap.setCenter(point, 5);
        } else {
            var point = new GLatLng(parseFloat(0), parseFloat(-180));
            this._gmap.setCenter(point, 1);
        }
        this._gmarker = new GMarker(point, { 
            icon:G_DEFAULT_ICON, 
            draggable: true 
        });
        this._gmap.addOverlay(this._gmarker);
        
        GEvent.addListener(this._gmarker, "dragend", function() {
            this._location.value = this._gmarker.getLatLng().lat()+','+this._gmarker.getLatLng().lng();
            this._gmap.setCenter(this._gmarker.getLatLng());
        }.bind(this));
        
        GEvent.addListener(this._gmap, "moveend", function(point) {
            this._location.value = this._gmap.getCenter().lat()+','+this._gmap.getCenter().lng();
            this._gmarker.setPoint(this._gmap.getCenter());
        }.bind(this));      
        
        
        // search
        var searchContainer = $(search);
        if (searchContainer) {
            
            var localSearch = new GlocalSearch();
            var searchControl = new GSearchControl();
            var options = new GsearcherOptions();
            options.setExpandMode(GSearchControl.EXPAND_MODE_CLOSED);
            searchControl.addSearcher(new GlocalSearch(), options);
            
            localSearch.setCenterPoint(this._gmap);
            
            searchControl.draw(searchContainer);
            
            searchControl.setSearchCompleteCallback(this, function(sc, searcher) {
                if (searcher.results && searcher.results.length > 0) {
                    for (var i = 0; i < searcher.results.length; i++) {
                        var result = searcher.results[i];
                    
                        if (result.GsearchResultClass == GlocalSearch.RESULT_CLASS ) {
                            var point = new GLatLng(parseFloat(result.lat), parseFloat(result.lng));
                            this._gmap.setCenter(point);
                            this._gmarker.setPoint(point);
                            break;
                        }
                    }
                }
            });
            //searchControl.setSearchStartingCallback(this, App.prototype.OnSearchStarting);
        }
    }
});
