function JbMap(id, default_zoom, enableLocate)
{
	this.map = new GMap2(document.getElementById(id));
	this.current_zoom = default_zoom;
	this.overlay = null;
	
	this.map.addControl(new JbCustomMapControl(), new GControlPosition(G_ANCHOR_TOP_LEFT));   
	this.map.addControl(new JbCustomMapControl2(enableLocate), new GControlPosition(G_ANCHOR_TOP_LEFT));   
	
	this.zoomIn = function()
	{
		this.map.zoomIn();
		this.current_zoom = this.map.getZoom();
	}
	
	this.zoomOut = function()
	{
		this.map.zoomOut();
		this.current_zoom = this.map.getZoom();
	}
	
	this.changeMapType = function(type)
	{
		if (this.map.isLoaded())
		{
			this.map.setMapType(type);
			return true;
		}
		else
			return false;
	}
	
	this.addOverlay = function(icon, width, height, lat, lng, html, popupWidth)
	{
		var _icon = new GIcon(G_DEFAULT_ICON);
		_icon.image = icon;
		_icon.iconSize = new GSize(width, height);
		var marker = new GMarker(new GLatLng(lat, lng), {icon:_icon});
		marker.bindInfoWindowHtml(html, {maxWidth:popupWidth});
		this.map.addOverlay(marker);
	}
	
	this.setCenter = function(lat, lng)
	{
		this.map.setCenter(new GLatLng(lat, lng), this.current_zoom);
	}
	
	this.isLoaded = function()
	{
		return this.map.isLoaded();
	}
	
	this.locate = function(_address) 
	{
		var reg = /(.*)[\(（].*[\)）]/;
		var address = reg.exec(_address);
		if (address == null)
			address = _address;
		else
			address = address[1];

		var geocoder = new GClientGeocoder();
		geocoder.getLatLng("台灣" + address,	function(point) 
			{
				if (!point) 
					alert("無此地址");
				else 
					jbCreateOverlay(point);
			});
	}

	this.createOverlay = function(point)
	{
		this.map.setCenter(point, this.current_zoom);
		if (this.overlay == null)
		{
			this.overlay = new GMarker(point, {draggable: true});
			this.map.addOverlay(this.overlay);
		}
		else
			this.overlay.setPoint(point);
	}
	
	this.getLat = function()
	{
		if (this.overlay != null)
			return this.overlay.getPoint().lat();
		else
			return 0;
	}
	
	this.getLng = function()
	{
		if (this.overlay != null)
			return this.overlay.getPoint().lng();
		else
			return 0;
	}
}

function jbCreateOverlay(point)
{
	jbMap.createOverlay(point);
}

var jbMap;

