var _map;
var _geo;
var _dirs;
var _fromMk = null;
var _toMk = null;
var _throughMk;
var _fromIcon;
var _toIcon;
var _throughIcon;
					  
function init()
{
	if (GBrowserIsCompatible()) {
		_map = new GMap2(document.getElementById("map_canvas"));
		_map.setCenter(new GLatLng(35.659354,139.745364), 14);
		_map.addControl(new GLargeMapControl());
		_map.enableScrollWheelZoom();
		_geo = new GClientGeocoder();
		_dirs = new GDirections(_map);
		GEvent.addListener(_dirs, "load", onDirsLoad);
		GEvent.addListener(_dirs, "addoverlay", onDirsAddOverlay);
		
		_fromIcon = new GIcon();
		_fromIcon.image = "http://maps.google.co.jp/mapfiles/ms/icons/red.png";
		_fromIcon.shadow = "http://maps.google.co.jp/mapfiles/ms/icons/msmarker.shadow.png";
		_fromIcon.iconSize = new GSize(32,32);
		_fromIcon.iconAnchor = new GPoint(16,32);
		_fromIcon.shadowSize = new GSize(59,32);
		_toIcon = new GIcon();
		_toIcon.image = "http://maps.google.co.jp/mapfiles/ms/icons/blue.png";
		_toIcon.shadow = "http://maps.google.co.jp/mapfiles/ms/icons/msmarker.shadow.png";
		_toIcon.iconSize=new GSize(32,32);
		_toIcon.iconAnchor=new GPoint(16,32);
		_toIcon.shadowSize=new GSize(59,32);
		_throughIcon = new GIcon();
		_throughIcon.image = "http://maps.google.co.jp/mapfiles/ms/icons/yellow.png";
		_throughIcon.shadow = "http://maps.google.co.jp/mapfiles/ms/icons/msmarker.shadow.png";
		_throughIcon.iconSize=new GSize(32,32);
		_throughIcon.iconAnchor=new GPoint(16,32);
		_throughIcon.shadowSize=new GSize(59,32);
		_throughMk = new Array();
	}

}
function setFrom()
{
	var address = document.fromForm.addr1.value;
	_geo.getLatLng(address, setFromMarker);
	return false;
}
function setTo()
{
	var address = document.toForm.addr2.value;
	_geo.getLatLng(address, setToMarker);
	return false;
}

function setFromMarker(latlng)
{
	if (latlng){
		if (_fromMk != null) {
			_map.removeOverlay(_fromMk);
		}
		_map.setCenter(latlng);
		_fromMk = new GMarker(latlng, {draggable: true, icon: _fromIcon});
		_map.addOverlay(_fromMk);
	} else {
		alert("出発地の住所が特定できませんでした。");
	}
}

function setToMarker(latlng)
{
	if (latlng){
		if (_toMk != null) {
			_map.removeOverlay(_toMk);
		}
		_map.setCenter(latlng);
		_toMk = new GMarker(latlng, {draggable: true, icon: _toIcon});
		_map.addOverlay(_toMk);
	} else {
		alert("目的地の住所が特定できませんでした。");
	}
}

function route()
{
	if (_fromMk == null) {
		alert("出発地を設定してください。");
		return false;
	}

	if (_toMk == null) {
		alert("目的地を設定してください。");
		return false;
	}

	var points = new Array();
	points.push(_toMk.getLatLng());
	
	for (var i = 1; i <= _throughMk.length; i ++) {
		if (_throughMk[i] != undefined && _throughMk[i] != null) {
			points.push(_throughMk[i].getLatLng());
		}
	}

	points.push(_fromMk.getLatLng());
	var ahw = (document.routeForm.hw.checked == true) ? false : true;
	_dirs.loadFromWaypoints(points, {locale: "ja_JP", avoidHighways: ahw});
	return false;
}

function clearRoute()
{
	_dirs.clear();
	return false;
}

function onDirsLoad()
{
	var od = _dirs.getDistance();
	var ot = _dirs.getDuration();

	document.routeForm.dist.value = od.meters / 1000;
	document.routeForm.min.value = Math.round(ot.seconds / 60 + 0.5);
	calcFuel();
}

function onDirsAddOverlay()
{
	var num = _dirs.getNumGeocodes();
	for (var i = 0; i < num; i++) {
		var mk = _dirs.getMarker(i);
		_map.removeOverlay(mk);
	}
}

function selectThrough()
{
	var i = document.throughForm.sel.value;
	if (_throughMk[i] == undefined || _throughMk[i] == null) {
		document.throughForm.btn.value = "追加";
	} else {
		document.throughForm.btn.value = "削除";
	}
}

function through()
{
	var i = document.throughForm.sel.value;
	if (_throughMk[i] == undefined || _throughMk[i] == null) {
		_throughMk[i] = new GMarker(_map.getCenter(), {draggable: true, icon: _throughIcon});
		_map.addOverlay(_throughMk[i]);
		document.throughForm.btn.value = "削除";
	} else {
		_map.removeOverlay(_throughMk[i]);
		_throughMk[i] = null;
		document.throughForm.btn.value = "追加";
	}
	return false;
}

function calcFuel()
{
	var ff = document.fuelForm;
	ff.lt.value = "";
	ff.yen.value = "";
	if (isNaN(document.routeForm.dist.value) || document.routeForm.dist.value == "") {
		return false;
	}
	var km = document.routeForm.dist.value;
	if (!isNaN(ff.cost.value) && ff.cost.value != "") {
		ff.lt.value = Math.round(km * 100 / ff.cost.value + 0.5) / 100;
		if (!isNaN(ff.price.value) && ff.price.value != "") {
			ff.yen.value = Math.round(ff.lt.value * ff.price.value * 100 + 0.5) / 100;
		}
	}
	return false;
}
