var mapCtrl = new MapController;
var onStartPanListener = null;
var onEndPanListener = null;
var onStartZoomListener = null;
var onEndZoomListener = null;

function MapController() 
{
	this.YOKOHAMA = new VELatLong(35.465755645, 139.6222186);
}

mapCtrl.initMap = function(width, height) {
	this.map = new VEMap('myMap');
	//LoadMap
	// 緯度経度，ズームレベル(1～19)，マップスタイル, 地図のfix, 2Dか3Dか, 切り替えボタンを表示するか
	// バッファサイズ, マップオプションの指定
	this.map.LoadMap(this.YOKOHAMA, 16, 
	 VEMapStyle.Road, false, VEMapMode.Mode2D, false, 1);
	this.map.Resize(width, height);
	this.map.HideDashboard();
	this.map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);
	
	if (onStartPanListener != null) {
		this.map.AttachEvent("onstartpan", onStartPanListener);
	}
	if (onEndPanListener != null) {
		this.map.AttachEvent("onendpan", onEndPanListener);
	}
	if (onStartZoomListener != null) {
		this.map.AttachEvent("onstartzoom", onStartZoomListener);
	}
	if (onEndZoomListener != null) {
		this.map.AttachEvent("onendzoom", onEndZoomListener);
	}
}

mapCtrl.moveMap = function(lat, lon) {
	var latLong = new VELatLong(parseFloat(lat), parseFloat(lon));
	this.map.SetCenter(latLong);
}
         
mapCtrl.addPin = function(lat, lon) {
	pinPoint = new VELatLong(lat, lon);
	//pinPixel = map.LatLongToPixel(new VELatLong(lat, lon));
	this.map.AddPushpin(pinPoint);
}

mapCtrl.getLatLongToPixel = function(lat, lon) {
	var pixel = this.map.LatLongToPixel(new VELatLong(lat, lon));
	return pixel;
}

mapCtrl.getPixelToLatLong = function(posX, posY) {
	var latlong = this.map.PixelToLatLong(new VEPixel(posX, posY));
	return latlong;
}

/**
 * @brief 中心位置を(x, y)座標へ移動します
 */
 /*
mapCtrl.moveByPixel = function(x, y) {
	//this.map.Pan(x, y);
	var latLong = this.map.PixelToLatLong(new VEPixel(x, y));
	this.map.PanToLatLong(latLong);
}
*/

/**
 * @brief 中心位置をx,yの変量だけ移動します
 * @param[in] horX 水平方向変量
 * @param[in] verY 垂直方向変量
 */
mapCtrl.moveByPixelDiff = function(horX, verY)
{
	this.map.Pan(horX, verY);
}

/**
 * @brief ズームレベルを変更して、移動します
 * @param[in] changeLev ズーム変更量(差分。-1なら現在のズームレベルから-1する)
 * @param[in] posX X座標
 * @param[in] posY Y座標
 */
mapCtrl.changeZoomLevelAndMove = function(changeLev, posX, posY)
{
	var zoom = this.map.GetZoomLevel() + changeLev;
	if (zoom < 15 || zoom > 19) { return; }
	var latLong = this.map.PixelToLatLong(new VEPixel(posX, posY));
	this.map.SetCenterAndZoom(latLong, zoom);
}

/**
 * @brief 現在のズームレベルを返します
 * @return ズームレベル
 */
mapCtrl.getZoomLevel = function() 
{
	return this.map.GetZoomLevel();
}

mapCtrl.setStartPanListener = function(listener)
{
	if (typeof listener == "function") {
		if (this.map != undefined) {
			this.map.AttachEvent("onstartpan", listener);
		} else {
			onStartPanListener = listener;
		}
	}
}

mapCtrl.setEndPanListener = function(listener)
{
	if (typeof listener == "function") {
		if (this.map != undefined) {
			this.map.AttachEvent("onendpan", listener);
		} else {
			onEndPanListener = listener;
		}
	}
}

mapCtrl.setEndZoomListener = function(listener)
{
	if (typeof listener == "function") {
		if (this.map != undefined) {
			this.map.AttachEvent("onendzoom", listener);
		} else {
			onEndZoomListener = listener;
		}
	}
}

mapCtrl.setStartZoomListener = function(listener)
{
	if (typeof listener == "function") {
		if (this.map != undefined) {
			this.map.AttachEvent("onstartzoom", listener);
		} else {
			onStartZoomListener = listener;
		}
	}
}

