var position = null;
// 
var panorama = null;
// 
var map = null;
//
var zoomLevel = 18;
//
var pointer = null;

var nextPanorama = null;
var currentPanorama = null;
var lastPanorama = null;

function load() {
	if(typeof initialPosition != "undefined" && initialPosition != null){
		position = initialPosition;
	}else{
		position = new GLatLng(35.003277, 135.774991);
	}
	
	if(typeof initialZoomLevel != "undefined" && initialZoomLevel != null){
		zoomLevel = initialZoomLevel;
	}
	
	// Google Street Viewに渡すオプョンを生成（この例では初期位置のみ）。
	if(typeof initialPov != "undefined" && initialPov != null){
		panoramaOptions = {latlng:position, pov:initialPov};
	}else{
		panoramaOptions = {latlng:position};
	}
	
	// Google Street Viewを表示するGStreetviewPanoramaオブジェクトの生成。
	//   第1引数: Google Street Viewを埋め込むdivタグのid。
	//   第2引数: Google Street Viewに渡すオプションを生成（この例では初期位置のみ）。
	panorama = new GStreetviewPanorama(
		document.getElementById("streetviewer"), panoramaOptions
		);

	// エラー処理のためにリスナーを追加（handleNoFlashに関しては後で定義）。	
	GEvent.addListener(panorama, "error", handleNoFlash);
	GEvent.addListener(panorama, "initialized", handleInitialization);
	
	//
	if(GBrowserIsCompatible()){
		var mapElement = document.getElementById("map");
		map = new GMap2(mapElement);
		map.setCenter(position, zoomLevel);
		
		var allowIcon = new GIcon();
		allowIcon.image = "http://maps.google.co.jp/intl/ja_jp/mapfiles/arrow.png";
		allowIcon.shadow = "http://maps.google.co.jp/intl/ja_jp/mapfiles/arrowshadow.png";
		allowIcon.iconSize = new GSize(39, 34);
		allowIcon.shadowSize = new GSize(39, 34);
		allowIcon.iconAnchor = new GPoint(10, 27);
		pointer = new GMarker(position, {icon:allowIcon});
		map.addOverlay(pointer);
	}
}

// Flash Playerを検出できない場合の処理用関数。
function handleNoFlash(errorCode) {
	if (errorCode == FLASH_UNAVAILABLE) {
		alert("Error: Flash Playerをインストールして下さい。");
		return;
	}
}

function handleInitialization(location) {
	position = location.latlng;
	if(!position.equals(currentPanorama.latlng)){
		currentPanorama = location;
		nextPanorama = null;
	}
}

function walk(){
	if(panorama != null){
		var client = new GStreetviewClient();
	
		if(nextPanorama != null){
			panorama.followLink(nextPanorama.yaw);
			lastPanorama = currentPanorama;
			client.getPanoramaById(nextPanorama.panoId, processReturnedData);
		}else{
			client.getNearestPanorama(position, processReturnedData);
		}
	}
}

// 
function processReturnedData(panoramaData) {
	if (panoramaData.code != 200) {
		GLog.write("Error(" + panoramaData.code + "):データを取得できませんでした。");
		return;
	}
	
	//
	currentPanorama = panoramaData.location;
	if(map != null && nextPanorama != null){
		pointer.setLatLng(currentPanorama.latlng);
		map.panTo(currentPanorama.latlng, zoomLevel);
	}
	
	var nextPanoramas = new Array();
	for(var linkIndex = 0; linkIndex < panoramaData.links.length; linkIndex++) {
		var link = panoramaData.links[linkIndex];
		if(lastPanorama == null || link.panoId != lastPanorama.panoId || panoramaData.links.length == 1){
			nextPanoramas.push(link);
		}
	}
	
	nextPanorama = nextPanoramas[Math.floor(Math.random() * nextPanoramas.length)];
	
	var pov = panorama.getPOV();
	pov.yaw = nextPanorama.yaw;
	panorama.panTo(pov);
}

setInterval("walk()", 5000);
