/**
 * Copyright (c) 2008 The Open Planning Project
 * Taken from:
 * http://svn.opengeo.org/geoext/core/trunk/lib/GeoExt/widgets/map/MapPanel.js
 */

Ext.namespace("GeoServices");

GeoServices.MapPanel = Ext.extend(Ext.Panel, {

    initComponent: function(){        
        var defConfig = {
            plain: true,
            border: false
        };
        
        Ext.applyIf(this, defConfig);

        this.addEvents("mapready");       
 
        GeoServices.MapPanel.superclass.initComponent.call(this);        
    },
    
    onRender: function() {
        GeoServices.MapPanel.superclass.onRender.apply(this, arguments);    
        this.map = new OpenLayers.Map(this.body.dom, this.mapOptions);
        this.fireEvent("mapready", this);
    },

    afterRender: function() {
        var size = this.ownerCt.getSize();
        Ext.applyIf(this, size);
        
        GeoServices.MapPanel.superclass.afterRender.call(this);    

        if(this.controls instanceof Array) {
            this.addControls(this.controls);
        }
        if(this.layers instanceof Array) {
            this.addLayers(this.layers);
            if(this.center) {
                var location = new OpenLayers.LonLat(center[0], center[1]);
                var zoom;
                if(this.resolution) {
                    zoom = this.map.getZoomForResolution(this.resolution);
                }
                this.map.setCenter(location, zoom);
            } else {
                this.map.zoomToMaxExtent();
            }
        }
        this.on({"bodyresize": this.updateMapSize, scope: this});
    },
    
    updateMapSize: function() {
        if(this.map) {
            this.map.updateSize();
        }
    },

    setSize: function(width, height, animate) {
        this.updateMapSize();
        GeoServices.MapPanel.superclass.setSize.call(this, width, height, animate);        
    },

    getCenter: function() {        
        return this.map.getCenter();
    },

    getZoom: function() {        
        return this.map.getZoom();
    },

    getResolution: function() {
        return this.map.getResolution();
    },
    
    getExtent: function() {
        return this.map.getExtent();
    },
    
    addControls: function(controls) {
        for(var i=0, len=controls.length; i<len; ++i) {
            this.map.addControl(controls[i]);
        }
    },
 
    addLayers: function(layers) {
        this.map.addLayers(layers);
    }

});

Ext.reg('gx_mappanel', GeoServices.MapPanel); 
