//各パラメーター
var mapId = 'gmap';
var svmapId = 'gsvmap';
var map;
var currentMarker;
var myPano;
var panoClient;
var myPOV = null;
var currentYaw = 0;
var currentPitch = 0;
var timer;

function load(address)
{
	if (!GBrowserIsCompatible()) {
		return;
	}

	geocoder = new GClientGeocoder();
	geocoder.getLatLng(address, function(latlng) {
		if (!latlng) {
			var msg = "経緯度が取得できませんでした。"
			document.getElementById(svmapId).innerHTML = msg;
			return;
		}
		else {
			//Google Mapsの表示
			showGoogleMaps(latlng);
		}
	});
}

function showGoogleMaps(latlng)
{
	//ストリートビュークライアントの作成
	panoClient = new GStreetviewClient();
	//座標
	//var latlng = new GLatLng(35.692333, 139.656898);

	//地図表示コンテナの指定、ドラッグ可能カーソルの指定
	map = new GMap2(document.getElementById(mapId), {draggableCursor: 'crosshair'});
	map.setCenter(latlng, 17);
	map.addMapType(G_PHYSICAL_MAP);
	map.addControl(new GHierarchicalMapTypeControl());
	map.addControl(new GLargeMapControl());
	currentMarker = new GMarker(latlng);
	map.addOverlay(currentMarker);
	
	//ストリートビュー表示コンテナの指定
	myPano = new GStreetviewPanorama(document.getElementById(svmapId));
	//ストリートビュー位置指定
	myPano.setLocationAndPOV(latlng, {yaw: 0, pitch: 0});

	//地図の上をクリックした時に、その場所のストリートビューを表示
	GEvent.addListener(map, "click", function(overlay,latlng) {
		//クリックした位置の座標に最も近いストリートビュー座標を取得
		panoClient.getNearestPanorama(latlng, showPanoData);
		svOverlay = new GStreetviewOverlay();
		map.addOverlay(svOverlay);
	});
	// initializedイベントを追加（その位置に移動し、初期化された時発生）
	GEvent.addListener(myPano, "initialized", move);
	//yawchangedイベントを追加（yawを変更した時発生）
	GEvent.addListener(myPano, "yawchanged", onYawChange);
	//ストリートビュークライアントにて、最も近いSV位置を取得し、showPanoDatheへコールバック
	panoClient.getNearestPanorama(latlng, showPanoData);
	svOverlay = new GStreetviewOverlay();
	map.addOverlay(svOverlay);
}


//ストリートビュー指定座標取得
function showPanoData(svData) {
	//取得座標がコード600ならばSV未対応地域
	//そうでないならば、座標データをhtmlへ格納し、地図に反映
	if (svData.code == 600) {
	} else {
		//var html = "<p><strong>"+svData.location.description+"<\/strong><br/>"+svData.location.latlng+"<\/p>";
		//map.setCenter(svData.location.latlng);
		//map.openInfoWindowHtml(svData.location.latlng, html);
		currentMarker.setPoint(svData.location.latlng);
		myPano.setLocationAndPOV(svData.location.latlng, myPOV);
	}
}

//ストリートビューへ座標セット
function move(location) {
	//var html = "<p><strong>"+location.description+"<\/strong><br/>"+location.latlng+"<\/p>";
	//map.setCenter(location.latlng);
	//map.openInfoWindowHtml(location.latlng, html);
	currentMarker.setPoint(location.latlng);
	poslat = String(location.lat);
	poslng = String(location.lng);
}

//ストリートビューのYawを変更
function onYawChange(newYaw) {
	if (newYaw < 0) {
		newYaw += 360;
	}
	var pov = myPano.getPOV();
	yawangle = pov.yaw.toFixed(0);
	pitchangle = pov.pitch.toFixed(0);
	zoomvalue = pov.zoom;
}

function animate0() {
	myPano.followLink(0);
}

function animatestart0() {
	clearInterval(timer);
	timer = window.setInterval(animate0, 2000);
}

function animate180() {
	myPano.followLink(180);
}

function animatestart180() {
	clearInterval(timer);
	timer = window.setInterval(animate180, 2000);
}

function rotate() {
	currentYaw += 5;
	myPano.panTo({yaw:currentYaw, pitch:currentPitch});
	if((currentYaw%360) == 0)
	{
		clearInterval(timer);
	}
}

function rotatestart() {
	clearInterval(timer);
	timer = window.setInterval(rotate, 200);
}

function animatestop() {
	clearInterval(timer);
}

function unload()
{
	GUnload();
}

