/*
 * ! Ext JS Library 3.2.0 Copyright(c) 2006-2010 Ext JS, Inc.
 * licensing@extjs.com http://www.extjs.com/license
 */
/**
 * @class Ext.ux.GMapPanel
 * @extends Ext.Panel
 * @author Shea Frederick
 */
Ext.ux.GMapPanel3 = Ext.extend(Ext.Panel, {

	kmlUrl : null,
	
	geoXml: null,

	overlays : null,
	
	mapOptions: {},
	
	initComponent : function() {

		var defaultConfig = {
			plain : true,
			zoom: 0,
			yaw : 180,
			pitch : 0,
			zoom : 0,
			mapTypeId: google.maps.MapTypeId.HYBRID
		};
		
		Ext.applyIf(this.mapOptions, defaultConfig);

		if(!Ext.isObject(this.mapOptions.center) && this.center)
		{
			this.mapOptions.center = new google.maps.LatLng(this.center.lat,this.center.lng);
		}
		
		Ext.ux.GMapPanel3.superclass.initComponent.call(this);
		
		this.on('resize', function(){
			if(this.gmap)
			{
				google.maps.event.trigger(this.gmap, 'resize');			
			}
		},this);

	},
	afterRender : function() {
		
		if (this.ownerCt) {
			var wh = this.ownerCt.getSize();

			Ext.applyIf(this, wh);
		}
	
		Ext.ux.GMapPanel3.superclass.afterRender.call(this);

		this.gmap = new google.maps.Map(this.body.dom, this.mapOptions);
		
		if(this.kmlUrl)
		{
			if(!this.overlays)
				this.overlays = [];
			else if(!Ext.isArray(this.overlays))
				this.overlays = [this.overlays];
				
			this.overlays.push(new google.maps.KmlLayer(this.kmlUrl));
		}
		
		this.addOverlays(this.overlays);
		
	},
	
	onOverlayClick: function(ev)
	{
		this.fireEvent('mapoverlayclick',this.gmap, ev);
	},
	
	getMap : function() {

		return this.gmap;

	},
	getCenter : function() {

		return this.getMap().getCenter();

	},
	
	getCenterObject : function() {

		var ll = this.getCenter();
		return {
			lat : ll.lat(),
			lng : ll.lng()
		};

	},
	
	addOverlays : function(ovls) {

		if(ovls)
		{
			if(!Ext.isArray(ovls))
				ovls = [ovls];
			
			for(var i=0;i<ovls.length; i++)
			{
				ovls[i].setMap(this.gmap);
				google.maps.event.addListener(ovls[i], 'click', this.onOverlayClick.createDelegate(this)); 
			}
		}

	},
	
	onDestroy: function()
	{
		Ext.ux.GMapPanel3.superclass.onDestroy.call(this);
	}

});

Ext.reg('gmappanel3', Ext.ux.GMapPanel3);