
Ext.namespace("Ext.hansub.camping");

Ext.hansub.camping.CampingMapPanel = function(config) {
	
	this.config = config;
	
	Ext.hansub.camping.CampingMapPanel.superclass.constructor.call(this, {
		id:this.config.id,
		//title:this.config.address,
		//region:this.config.region,
		autoWidth:false,
		layout:'fit',
		autoWidth :true,
		height:300,

		//margins: '10 10 10 10',
		closable:true,
		split: true,
		border:true,
		defaults: {autoScroll:true}
        
	});
		
	
  // this.on('render', this.makeMap, this);
};

Ext.extend(Ext.hansub.camping.CampingMapPanel, Ext.Panel, {

	
	init : function(){
		
		if (!this.config.x || this.config.x == '0' || this.config.x == 'unconfirmed') {
			
			this.makeMapPosition()
		}
		else {
			this.makeMap(this.config.x, this.config.y);
		}

		
	},
	makeMapPosition : function(){
		
		Ext.Ajax.request({
			
			url: './src/api/XmlInterface.php',
			params:{
				'class':'Camping',
				'method':'getCoordinates',
				'camping_list_uid':this.config.camping_list_uid
			},
			
			scope:this,
			success:function(response, options){
				
				var xmlDoc = response.responseXML;
				
				var pointElements = xmlDoc.getElementsByTagName("point");
				
				var x = pointElements[0].childNodes[0].text;
				var y = pointElements[0].childNodes[1].text;
				
				
				this.saveXY(x,y);
								
				this.makeMap(x,y);
				
			},
			failure:function(response, options){
				Ext.MessageBox.alert('ERROR', 'Chart 호출시 오류가 발생하였습니다.')
			}
			            	
					
		});

		
	},
	
	saveXY:function(x,y){
		
		Ext.Ajax.request({
			
			url: './src/api/Interface.php',
			params:{
				'class':'Camping',
				'method':'setCoordinates',
				'camping_list_uid':this.config.camping_list_uid,
				'x':x,
				'y':y
			},
			
			scope:this,
			success:function(response, options){
				
			},
			failure:function(response, options){
				
			}
			            	
					
		});
		
	},
	
	
	makeMap : function(x,y){
		
		size = this.getSize();
		opts = {width:size.width, height:size.height, mapMode:2};

		mapObj = new NMap(document.getElementById(this.getId()),opts);
		

					
		mapObj.setCenterAndZoom(new NPoint(x,y),1);
		
			
		zoom = new NZoomControl();
		
		zoom.setAlign("right");
		zoom.setValign("top");
		mapObj.addControl(zoom);
		
		
		mapBtns = new NMapBtns();
		mapBtns.setAlign("right");
		mapBtns.setValign("top");
		mapObj.addControl(mapBtns);
		
		mapObj.addOverlay(this.createMarker(new NPoint(x,y),1,"캠핑장"));
			
	
	},
	
	createMarker:function(pos, count, content) {
		//var iconUrl = './images/icons/flag_red.gif';
		var iconUrl = './images/info/campmark.png';
		var marker = new NMark(pos, new NIcon(iconUrl, new NSize(80, 48)));
		return marker;
	}
	
	
});

