MapSelector = function(oConfig)
{
	this.init(oConfig);
};

MapSelector.prototype = {
	
	map:	null,
	marker:	null,
	lat:	null,
	lng:	null,
	mapsearch:	null,
	mapcontainer: null,
	region: null,
	
	init: function(oConfig)
	{
		this.lat	= YAHOO.util.Dom.get("lat");
		this.lng	= YAHOO.util.Dom.get("lng");
		this.region	= YAHOO.util.Dom.get("mapselector");
		this.mapsearch	= YAHOO.util.Dom.get("mapsearch");
		
		// address search input
		var elMapText	= document.createElement('input');
		elMapText.id	= 'maptext';
		elMapText.display= 'block';
		elMapText.type	= 'text';
		elMapText.value	= '';
		
		// plot by address link
		var locateBtn	= document.createElement('span');
		locateBtn.id	= 'locateBtn';
		locateBtn.name	= 'locateBtn';
		locateBtn.innerHTML	= '地図に表示';
		
		// copy from address input
		var elAddrCopy	= document.createElement('span');
		elAddrCopy.id	= 'addrcopy';
		elAddrCopy.name	= 'addrcopy';
		elAddrCopy.innerHTML	= '住所をコピー';
		
		// insert map search controls in <div id="mapsearch></div>
		this.mapsearch.appendChild(elAddrCopy);
		this.mapsearch.appendChild(elMapText);
		this.mapsearch.appendChild(locateBtn);
		
		// map container
		this.mapcontainer	= YAHOO.util.Dom.get("mapcontainer");
		YAHOO.util.Dom.setStyle(this.mapcontainer, 'height', oConfig.h + 'px');
		YAHOO.util.Dom.setStyle(this.mapcontainer, 'width', oConfig.w + 'px');
		
		if (GBrowserIsCompatible())
		{
			this.map = new GMap2(this.mapcontainer);
			this.map.addControl(new GLargeMapControl());
			this.map.addControl(new GMapTypeControl());
			var center = new GLatLng(oConfig.lat, oConfig.lng);
			this.map.setCenter(center, 15);
			
			this.marker = new GMarker(center, {
						draggable : true
					});
			this.map.addOverlay(this.marker);
			this.lat.value = center.lat().toFixed(5);
			this.lng.value = center.lng().toFixed(5);
	
			GEvent.bind(
				this.marker,
				"dragend",
				this,
				this.handlerMakerDrag);
	
			GEvent.bind(
				this.map,
				"moveend",
				this,
				this.handlerMapDrag);
			
			YAHOO.util.Event.addListener(
				'locateBtn',
				'click',
				this.plotMap,
				this
			);
			
			YAHOO.util.Event.addListener(
				'addrcopy',
				'click',
				this.copyAddress,
				this
			);
			
			YAHOO.util.Event.addListener(
				'map-0',
				'click',
				this.hideMap,
				this
			);
			YAHOO.util.Event.addListener(
				'map-1',
				'click',
				this.showMap,
				this
			);
			
			if (oConfig.showMap === 0)
			{
				this.hideMap(null, this);
			}
		}
	},
	
	copyAddress: function(ev, me)
	{
		var src	= YAHOO.util.Dom.get('address');
		var target	= YAHOO.util.Dom.get('maptext');
		target.value	= src.value;
	},
	
	hideMap: function(ev, me)
	{
		YAHOO.util.Dom.setStyle(
			me.region,
			'display',
			'none');
	},
	
	showMap: function(ev, me)
	{
		YAHOO.util.Dom.setStyle(
			me.region,
			'display',
			'block');
	},
	
	handlerMakerDrag: function()
	{
		point	= this.marker.getPoint();
		this.map.panTo(point);
		this.lat.value	= point.lat().toFixed(5);
		this.lng.value	= point.lng().toFixed(5);
	},
	
	handlerMapDrag: function()
	{
		this.map.clearOverlays();
		var center = this.map.getCenter();
		this.marker = new GMarker(center, {
					draggable : true
				});
		this.map.addOverlay(this.marker);
		this.lat.value = center.lat().toFixed(5);
		this.lng.value = center.lng().toFixed(5);

		GEvent.bind(
			this.marker,
			"dragend",
			this,
			this.handlerMakerDrag);
	},
	
	plotMap: function(ev, me)
	{
		geocoder = new GClientGeocoder();
		address	= YAHOO.util.Dom.get('maptext').value;
		if (geocoder)
		{
			geocoder.getLatLng(
				address,
				function(point)
				{
					if (!point)
					{
						alert("「" + address + "」は見つかりませんでした。");
					}
					else
					{
						YAHOO.util.Dom.get("lat").value = point.lat().toFixed(5);
						YAHOO.util.Dom.get("lng").value = point.lng().toFixed(5);
						me.map.clearOverlays();
						me.map.setCenter(point, 14);
						me.marker = new GMarker(point, {
									draggable : true
								});
						me.map.addOverlay(me.marker);
					}
				}
			);
		}
	}
};
