/* 
 * JMMap - wrapper for map
 *
 */
if (typeof JMMap == "undefined") { 
    var JMMap = function(el, callbacks, markers, options) {
        
        this.el = el;
        this.map = null;
        
        this.options = this.copy(options, 
                            {
                                "lng": 18.06,
                                "lat":  59.32,
                                "zoom": 5, 
                                "maxZoom": 12,
                                "enableScrollWheelZoom": false, 
                                "extent": null
                            });
        
        this.callbacks = this.copy(callbacks, 
                                {
                                    move: null,
                                    zoom: null
                                }); 
                                
        this.icons = {};                                       
        this.markers = [];
        this.init(markers);
    };
        
    JMMap.Maps = { };

    
    JMMap.prototype = {
    
        toString: function() {
            return "JMMap Object";
        },
        
        copy: function(src, dest) {
            for (var p in src) {
                dest[p] = src[p];
            }
            return dest;
        },

        dispose: function() {
            var p;
            
            GEvent.clearInstanceListeners(this.map);
            this.map.clearOverlays();
            this.map = null;
            this.options = null;
            this.el = null;
            this.icons = null;

            // delete callbacks            
            for (p in this.callbacks) {
                var f = this.callbacks[p]; 
                delete this.callbacks[p];
                delete f;
            }
        },

        init: function(markers) {

            this.createIcons();
            
            if (GBrowserIsCompatible()) {
                
                if (JMMap.Maps[this.el.id]) {
                    var old = JMMap.Maps[this.el.id];
                    var latlng = old.map.getCenter();
                    /*
                    this.options.lat = latlng.lng();
                    this.options.lng = latlng.lat();
                    this.options.zoom = old.map.getZoom();
                    */
                    old.dispose();
                    old = null;
                } 
                
                this.map = new GMap2(this.el);
                this.map.addControl(new GSmallMapControl());                
                this.map.addControl(new GMapTypeControl());
                
                if (this.options.enableScrollWheelZoom) {
                    this.map.enableScrollWheelZoom();
                }

                var center = new GLatLng(this.options.lat, this.options.lng)
                var zoom = this.options.zoom;

                this.map.setCenter(center, zoom);

                if (this.options.extent) {
                    var bounds = new GLatLngBounds(new GLatLng(this.options.extent.bottom, this.options.extent.left), new GLatLng(this.options.extent.top, this.options.extent.right));
                    center = bounds.getCenter();
                    zoom = Math.min(Math.max(this.map.getBoundsZoomLevel(bounds) - 1, 1), this.options.maxZoom);
                    this.map.setCenter(center, zoom);
                }

                // Register callback functions;
                this.addListener("moveend", "onmove");
                this.addListener("zoomend", "onzoom");
                JMMap.Maps[this.el.id] = this;
                
                if (markers && markers.length > 0) {
                    this.addMarkers(markers);
                }

/*                
                if (this.markers.length > 0) {
                    var bounds      = this.map.getBounds();
                    var size        = this.map.getSize();
                    var mapHeight   = bounds.getNorthEast().lat() - bounds.getSouthWest().lat();
                    var mapWidth    = bounds.getNorthEast().lat() - bounds.getSouthWest().lng();

                    var markerMapHeight = size.height 
                                    
                
                }
*/                
            }
        },
    
        addListener: function(eventName, callbackName) {
            var m = this;
            GEvent.addListener(this.map, eventName, function() { m[callbackName].apply(m, arguments); });
        },
    
        onmove: function() {
            if (this.callbacks.move) {
                this.callbacks.move(this, this.map.getBounds());
            }
        },

        onzoom: function() {
            if (this.callbacks.zoom) {
                this.callbacks.zoom(this, this.map.getBounds());
            }
        }, 

        addMarkers: function(markers) {
            
            if (markers) {
                for (var i = 0; i < markers.length; i++) {
                    this.addMarker(markers[i].type, new GLatLng(markers[i].y, markers[i].x), markers[i].infoHTML);
                }
            }
        },

        addMarker: function(iconName, point, html) {
            var marker = this.createMarker(iconName, point, html)
            this.map.addOverlay(marker);
            this.markers.push(marker);
        },
        
        createMarker: function(iconName, point, html) {
              
            var icon = this.icons[iconName];
            var markerOptions = { icon: icon };
            var marker = new GMarker(point, markerOptions);

            GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(html);
            });

            return marker;
        }, 
        
        createIcons: function() {
            
            var icon; 
            var names = "house,people,office,shop,apartment".split(",");
            
            for (var i = 0; i < names.length; i++) {
                icon = new GIcon();
                icon.image = "/graphics/gmap_marker_" + names[i] + ".png";
                icon.iconSize = new GSize(40, 49);
                icon.iconAnchor = new GPoint(20, 49);
                icon.infoWindowAnchor = new GPoint(20, 49);
                this.icons[names[i]] = icon;
            }
        }
    };
}
