//<![CDATA[

//다음 지도 API 관련 클래스
var Map = Class.create({
	// 생성자(지도생성)
	initialize: function(area_obj, w, h, x, y, zoom){
		this.map_obj = null;
		this._marker_arrow = null;
		this.default_zoom = 3;
		this._markers = new Array();
		this._x_input_obj = null;
		this._y_input_obj = null;
		this._map_type_control = null;
		
		if(!zoom) zoom = this.default_zoom;
		else this.default_zoom = zoom;
			
		if(!x || x == 0) x = 127.03173023583358;
		if(!y || y == 0) y = 37.500926305287095;
		
		this.map_obj = new DMap(area_obj, {width:w, height:h} );
		//this.map_obj.setCoordinateType("ktm");
		this.map_obj.setCenter(new DPoint(x, y), zoom);
		
		this.map_obj.enableDragging();
		this.map_obj.enableScrollWheelZoom();
		//this.map_obj.disabledDoubleClickZoom();
   this.enableMapTypeControl();
	},
	
	//지도 위치지정
	setPosition: function(x, y, zoom) {
		if(x==0 || y==0 || !x || !y) {
			alert('위치정보가 없습니다.');
			return;
		}
		if(!zoom) zoom = this.default_zoom;
		else this.default_zoom = zoom;
		
		var pos = new DPoint(x, y);
		this.map_obj.setCenter(pos, zoom);
		if(this._marker_arrow) this._marker_arrow.setPoint(pos)
	},
	
	//마커(업체 아이콘) 생성
	createMarker: function(x, y, iconUrl, content, link_url, marker_key) {
		//마커 그룹지정 (삭제시 그룹삭제 가능..)
		if(!marker_key) marker_key = 'wedding';
		
		//아이콘 및 마크 생성
		var pos = new DPoint(x, y);
		var icon = new DIcon(iconUrl, new DSize(14, 14));
		var info_width = content.length*15
		if(info_width < 100) info_width = 100;
		var iw = new DInfoWindow("<p style='text-align:center; padding-top: 4px; margin-left:0px; color: #000'>"+content+"</p>", {width:info_width, height:20, removable:false});
		var marker = new DMark(pos, {mark:icon, infowindow:iw, infowindow_mouseover:true});
		if(link_url && link_url.strip() != '') marker.setLink(new DLink(link_url, '_blank'));
		//else marker.setLink(new DLink('javascript:alert("연결된 주소가 없습니다.");'));
		
		this.map_obj.addOverlay(marker, marker_key);
		
		//첫번째만 마크 생성
		if(this._marker_arrow == null) {
			this._marker_arrow = new DMark(pos, {mark: new DIcon('/images/common/icon_here.gif', new DSize(34, 47)), offset: new DPoint(-17, -55), clickable: false} );
			this.map_obj.addOverlay(this._marker_arrow, marker_key + 'point');
		}
		
		this._markers.push(marker);
	},
	
	//마커 삭제
	removeMarker: function(marker_key) {
		map_obj.clearOverlays(marker_key);
	},
	
	// 확대, 축소를 위한 컨트롤을 생성합니다.
	addZoomControl: function() {
		var zoom = new DZoomControl();
		zoom.setAlign("right");
		zoom.setValign("top");
		this.map_obj.addControl(zoom);
	},
	
	// 이미지 저장을 위한 컨트롤을 생성합니다.
	addSaveControl: function() {
		// daum map api 에서는 지원 안함!
	},
	
	// 마커 위치 변경 가능토록 이벤트 추가 - 클릭
	enableMarkerChange: function(x_input_id, y_input_id) {
		if(!this._markers[0]) {
			alert("변경할 포인트가 없습니다.")
			return false;
		}
		this._x_input_obj = $(x_input_id);
		this._y_input_obj = $(y_input_id);
		DEvent.addListener(this.map_obj, 'click', this.markerChange.bind(this));
	},
	
	// 마커 위치 변경
	markerChange: function(d_point) {
		var point = d_point;
		//var point = this.map_obj.getTransCoord(d_point, "wgs84", "ktm");
		//alert("Position: " + point.x + ' / ' + point.y);
		if(this._markers[0]) this._markers[0].setPoint(point);
		if(this._marker_arrow) this._marker_arrow.setPoint(point);
		if(this._x_input_obj) this._x_input_obj.value = point.x;
		if(this._y_input_obj) this._y_input_obj.value = point.y;
	},
	
	//지도 타입 변경 컨트롤(지도, 스카이뷰)
	enableMapTypeControl: function() {
		this._map_type_control = new DMapTypeControl();
		this.map_obj.addControl(this._map_type_control);
	}

	
});

	
//]]>
