/**
 * @author Shea Frederick
 * @Modifier nat
 */
Ext.namespace('DreamOne.windows.map.google');
 
/**
 *
 * @class GMapPanel
 * @extends Ext.Panel
*/
DreamOne.windows.map.google.GMapPanel = Ext.extend(Ext.Panel, {
	
	initComponent : function(){
        var defConfig = {
        	plain: true,
        	zoomLevel: 3,
        	yaw: 180,
        	pitch: 0,
        	zoom: 0,
        	gmapType: 'map',
            border: false
        }        
        Ext.applyIf(this,defConfig);
        
		DreamOne.windows.map.google.GMapPanel.superclass.initComponent.call(this);        
    },
    
    preparedIcon : function(color){
    	var f = new GIcon();
		  f.image = '/Hades/pages/images/singlemap/icons/' + color + '.png';
		  f.shadow = '/Hades/pages/images/singlemap/icons/shadow.png';
		  f.iconSize = new GSize(12,20);
		  f.shadowSize = new GSize(22,20);
		  f.iconAnchor = new GPoint(6,20);
		  f.infoWindowAnchor = new GPoint(6,1);
		  f.infoShadowAnchor = new GPoint(13,13);
		return f;
    },
    
    afterRender : function(){
        
        var wh = this.ownerCt.getSize();
        Ext.applyIf(this, wh);
        
		DreamOne.windows.map.google.GMapPanel.superclass.afterRender.call(this);	
        
		if (this.gmapType === 'map'){
			this.gmap = new GMap2(this.body.dom);
		}
		
		if (this.gmapType === 'panorama'){
			this.gmap = new GStreetviewPanorama(this.body.dom);
		}
		/*
		if (typeof this.addControl === 'object' && this.gmapType === 'map') {
			this.gmap.addControl(this.addControl);
		}
		*/
		if ( Ext.isArray(this.controls) ) {
			for (var i = 0; i < this.controls.length; i++) {
				var pos		= this.controls[i].pos;
				var ctrl	= this.controls[i].ctrl;
				this.gmap.addControl(ctrl,pos);
			}
		}
		
		if (typeof this.setCenter === 'object') {
			if (typeof this.setCenter.geoCodeAddr === 'string'){
				this.geoCodeLookup(this.setCenter.geoCodeAddr);
			}else{
				if (this.gmapType === 'map'){
					var point = new GLatLng(this.setCenter.lat,this.setCenter.lng);
					this.gmap.setCenter(point, this.zoomLevel);	
				}
				if (typeof this.setCenter.marker === 'object' && typeof point === 'object'){
					this.addMarker(point,this.setCenter.marker,this.setCenter.marker.clear);
				}
			}
			if (this.gmapType === 'panorama'){
				this.gmap.setLocationAndPOV(
					new GLatLng(this.setCenter.lat,this.setCenter.lng),
						{yaw: this.yaw, pitch: this.pitch, zoom: this.zoom});
			}
		}
		
        var dt = new Ext.util.DelayedTask();
        dt.delay(300, function(){
            this.addMarkers(this.markers);
        }, this);
	},
    onResize : function(w, h){
        if (typeof this.gmap == 'object') {
            this.gmap.checkResize();
        }
		
		DreamOne.windows.map.google.GMapPanel.superclass.onResize.call(this, w, h);
    },
    setSize : function(width, height, animate){
        
        if (typeof this.gmap == 'object') {
            this.gmap.checkResize();
        }
		
		DreamOne.windows.map.google.GMapPanel.superclass.setSize.call(this, width, height, animate);
        
    },
	getMap: function(){
		
		return this.gmap;
		
	},
	addMarkers: function(markers) {
		
		if (Ext.isArray(markers)){
			for (var i = 0; i < markers.length; i++) {
				var mkr_pt = new GLatLng(markers[i].lat,markers[i].lng);
				//this.addMarker(mkr_point,markers[i].marker,false,markers[i].setCenter);
				this.addMarker(mkr_pt,markers[i].marker,markers[i].events);
			}
		}
		
	},
	addMarker: function(point,cfg,events){
		var lMarker = new GMarker(point,cfg);
		if(events){
			if(events.click) GEvent.addListener(lMarker,"click",events.click );
			if(events.dblclick) GEvent.addListener(lMarker,"dblclick",events.dblclick );
			if(events.mousedown) GEvent.addListener(lMarker,"mousedown",events.mousedown );
			if(events.mouseup) GEvent.addListener(lMarker,"mouseup",events.mouseup );
			if(events.mouseover) GEvent.addListener(lMarker,"mouseover",events.mouseover );
			if(events.mouseout) GEvent.addListener(lMarker,"mouseout",events.mouseout );
			if(events.infowindowopen) GEvent.addListener(lMarker,"infowindowopen",events.infowindowopen );
			if(events.infowindowbeforeclose) GEvent.addListener(lMarker,"infowindowbeforeclose",events.infowindowbeforeclose );
			if(events.infowindowclose) GEvent.addListener(lMarker,"infowindowclose",events.infowindowclose );
			if(events.remove) GEvent.addListener(lMarker,"remove",events.remove );
			if(events.dragstart) GEvent.addListener(lMarker,"dragstart",events.dragstart );
			if(events.drag) GEvent.addListener(lMarker,"drag",events.drag );
			if(events.dragend) GEvent.addListener(lMarker,"dragend",events.dragend );
			if(events.visibilitychanged) GEvent.addListener(lMarker,"visibilitychanged",events.visibilitychanged );
		}
		this.gmap.addOverlay(lMarker);
		return lMarker;
	},
	setCenter : function(latlng,zooming) {
		if(zooming) this.zoomLevel = zooming;
		this.gmap.setCenter(latlng, this.zoomLevel);	
	},
	/*
	addMarker: function(point, marker, clear, center){
		
		Ext.applyIf(marker,G_DEFAULT_ICON);
		if (clear === true){
			this.gmap.clearOverlays();
		}
        if (center === true) {
            this.gmap.setCenter(point, this.zoomLevel);
        }
        
		var mark = new GMarker(point,marker);
   		this.gmap.addOverlay(mark);
	},*/
	_latlngLookup: function(addr,callback){
		this.geocoder = new GClientGeocoder();
		this.geocoder.getLatLng(addr, callback.createDelegate(this));
	},
	_geoCodeLookup: function(addr,callback){
		this.geocoder = new GClientGeocoder();
		this.geocoder.getLocation(addr, callback.createDelegate(this));
	}
	/*
	geoCodeLookup : function(addr) {
		this._geoCodeLookup(add,addAddressToMap);
	},
    addAddressToMap : function(response) {
		
		if (!response || response.Status.code != 200) {
			Ext.MessageBox.alert('エラー', 'コード：'+response.Status.code);
  		} else {
    		place = response.Placemark[0];
			addressinfo = place.AddressDetails;
			accuracy = addressinfo.Accuracy;
			if (accuracy === 0) {
				Ext.MessageBox.alert('住所を検索できません', '住所を検索できませんでした');
			}else{
				if (accuracy < 7) {
					Ext.MessageBox.alert('住所の正確さ', '正確な住所を取得できません.<br><br>正確さ '+accuracy+' 段階 (8 = 最高, 1 = 最低)');
				}else{
	        		point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
					if (typeof this.setCenter.marker === 'object' && typeof point === 'object'){
						this.addMarker(point,this.setCenter.marker,this.setCenter.marker.clear,true);
					}
				}
			}
	  	}
	}
	*/
});
Ext.reg('gmappanel',DreamOne.windows.map.google.GMapPanel);
/*
Ext.onReady(function(){
	var panwin = new Ext.Window({
        layout: 'fit',
        closeAction: 'hide',
		title: 'GPanorama Window',
		width:400,
		height:300,
		x: 480,
		y: 60,
        items: {
        xtype: 'gmappanel',
    		gmapType: 'panorama',
    		setCenter: {
    			lat: 42.345573,
    			'long': -71.098326
    		}   
        }
	});
	var mapwin = new Ext.Window({
        layout: 'fit',
		title: 'GMap Window',
        closeAction: 'hide',
		width:400,
		height:400,
		x: 40,
		y: 60,
        items: {
            xtype: 'gmappanel',
            region: 'center',
    		zoomLevel: 14,
    		gmapType: 'map',
    		addControl: new GSmallMapControl(),
    		setCenter: {
    			geoCodeAddr: '4 Yawkey Way, Boston, MA, 02215-3409, USA',
    			marker: {title: 'Fenway Park'}
    		},
    		markers: [{
    			lat: 42.339641,
    			'long': -71.094224,
    			marker: {title: 'Boston Museum of Fine Arts'}
    		},{
    			lat: 42.339419,
    			'long': -71.09077,
    			marker: {title: 'Northeastern University'}
    		}]
        }
	});
	new Ext.Viewport({
		title: 'GMap Panel',
        layout: 'border',
        items: {
            xtype: 'gmappanel',
            region: 'center',
    		zoomLevel: 14,
    		gmapType: 'map',
    		addControl: new GSmallMapControl(),
    		setCenter: {
    			geoCodeAddr: '4 Yawkey Way, Boston, MA, 02215-3409, USA',
    			marker: {title: 'Fenway Park'}
    		},
    		markers: [{
    			lat: 42.339641,
    			'long': -71.094224,
    			marker: {title: 'Boston Museum of Fine Arts'}
    		},{
    			lat: 42.339419,
    			'long': -71.09077,
    			marker: {title: 'Northeastern University'}
    		}],
            tbar: [{
                text: 'Fenway Park StreetView',
                handler: function(){
                    panwin.show();
                }
            },{
                text: 'Fenway Park Map Window',
                handler: function(){
                    mapwin.show();
                }
            }]
        }
	});
});
*/