var lastPanoId = null;         // 最後にいた場所のPanoID
var nowLatLng;
var map;
var streetViewClient;
var streetPanorama;
var moveInterval;

function initMap(lat, lng){
   map = new GMap2(document.getElementById("map"));
   map.disableDragging();
   var ctrlObj = new GMapTypeControl();
   var pos = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(5, 15));
   map.addControl(ctrlObj, pos);
   map.setCenter(new GLatLng(lat, lng), 18);
   initPanorama(lat, lng);
}

function initPanorama(lat, lng){
   streetViewClient = new GStreetviewClient;
   streetPanorama = new GStreetviewPanorama(document.getElementById("pano"));
   nowLatLng = new GLatLng(lat, lng);
   streetViewClient.getNearestPanoramaLatLng(nowLatLng, function(latlng){
	if(latlng == null){
	    document.getElementById("pano").style.display = "none";
            return;
        }
	nowLatLng = latlng;
        streetPanorama.setLocationAndPOV(nowLatLng, {yaw: 180, pitch: 0, zoom: -1}); 


	GEvent.addListener(streetPanorama, 
                                "yawchanged", 
                                function(yaw){
                                    changeYaw(yaw);
                                });       

        // 3秒後に移動
        moveInterval = setInterval(toNextPanorama, 3000);
   })

}

function changeYaw(yaw){
    var gifFile;
    if( yaw >= 315 || yaw < 45  ) gifFile = "/common/image/ue.gif";
    else if( yaw >= 45 && yaw <  135 ) gifFile = "/common/image/migi.gif";
    else if( yaw >= 135 && yaw <  225 ) gifFile = "/common/image/shita.gif";
    else if( yaw >= 225 || yaw < 315 ) gifFile = "/common/image/hidari.gif";
    document.getElementById("girl").innerHTML = '<img src="'+ gifFile +'" alt="gial"/>';
}

// 現在の表示位置から一番近いStreetViewDataを取得し、そこから隣接するパノラマに移動する
function toNextPanorama(){
   clearInterval(moveInterval);
   // 現在の表示位置の緯度経度を取得
   streetViewClient.getNearestPanorama(nowLatLng, function(streetData){

        var neighbourCount = streetData.links.length; // 隣接するパノラマの数

        var nextYaw; // 次に向かう方向
	var nextPanoId; // 次に向かうパノラマのID
	if(neighbourCount == 1){
            // 隣接するパノラマが１つだった場合はそこへ移動
            nextYaw = streetData.links[0].yaw;
	    nextPanoId = streetData.links[0].panoId;
        }else{
           while(1){
               i = Math.floor( Math.random() * neighbourCount );
               if(lastPanoId != streetData.links[i].panoId){
                    nextPanoId = streetData.links[i].panoId;
                    nextYaw = streetData.links[i].yaw;
                    break;
               }
           }
        }
        lastPanoId = streetData.location.panoId;     // 今いた場所を覚えておく
        streetViewClient.getPanoramaById( nextPanoId,
                                          function(nextStreetData){
                                              nowLatLng = nextStreetData.location.latlng;
                                              // 目的地へGO！
                                              //streetPanorama.setLocationAndPOV( nowLatLng, {yaw:nextYaw, pitch:0, zoom:-1});
                                              streetPanorama.followLink(nextYaw);
                                              map.panTo(nowLatLng);
                                              //streetPanorama.setPOV({yaw:nextYaw, pitch:0, zoom:-1});
                                              moveInterval = setInterval(toNextPanorama, 3000);
                                          }
                                         );
   }
   );
}

