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

// GoogleMapsの初期化
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);
                           });

        GEvent.addListener(streetPanorama, 
                           "initialized", 
                           function(location){
                               panoramaInitialized(location);
                           });


    })
}

function changeYaw(yaw){
    var gifFile;
    if( yaw > 360 ) yaw -= 360;
    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"/>';
}

function panoramaInitialized(location){
    map.panTo(location.latlng);
}

