/**
 * Copyright (c) 2008 The Open Planning Project
 */

Ext.namespace("GeoExt.component");
GeoExt.component.MapPanel = Ext.extend(Ext.Panel, {

    initComponent: function(){        

        var defConfig = {
            plain: true,
            border: false
        };
        
        Ext.applyIf(this, defConfig);
        
        GeoExt.component.MapPanel.superclass.initComponent.call(this);      
				           
    },

    afterRender: function() {

        var size = this.ownerCt.getSize();
        Ext.applyIf(this, size);
        
        GeoExt.component.MapPanel.superclass.afterRender.call(this);    
       
        //this.map = new OpenLayers.Map(this.body.dom, this.mapOptions);

	this.map = new OpenLayers.Map(this.body.dom, {controls: [], numZoomLevels: 10 });

			this.map.addControl(new OpenLayers.Control.PanZoomBar());
			this.map.addControl(new OpenLayers.Control.MouseToolbar());			
			this.map.addControl(new OpenLayers.Control.MousePosition());		
			this.map.addControl(new OpenLayers.Control.OverviewMap());						           
            
			//this.map.setCenter(new OpenLayers.LonLat(-99.5953, 38.2115), 4);
			//if (!this.map.getCenter()) this.map.zoomToMaxExtent();

        if(this.controls instanceof Array) {
            this.addControls(this.controls);
        }
        if(this.layers instanceof Array) {
            this.addLayers(this.layers);
        }
        this.map.zoomToMaxExtent();
        this.ownerCt.on({"move": this.updateMapSize, scope: this});
    },
    
    updateMapSize: function() {
        if(this.map) {
            this.map.updateSize();
        }
    },

    onResize: function(w, h){
        this.updateMapSize();
        GeoExt.component.MapPanel.superclass.onResize.call(this, w, h);
    },

    setSize: function(width, height, animate) {
        this.updateMapSize();
        GeoExt.component.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('mappanel', GeoExt.component.MapPanel); 
