var panorama;
var currentHeading = 80;
var STARTING_HEADING = 80;
var currentPitch = 0;
var STARTING_PITCH = 0;
var timer;
var currentZoom = 1;
var zoomingIn = true;
var latlng;
var SPIRAL_TIMEOUT = 300;
var isRotating = true;

function load() {
  panorama = new google.maps.StreetViewPanorama(document.getElementById("pano"));
  //latlng =  new google.maps.LatLng(49.248079, -123.071279);
  latlng =  new google.maps.LatLng(49.248079, -123.071279);
  panorama.setPosition(latlng);
  panorama.setPov({heading: currentHeading, pitch: currentPitch, zoom: currentZoom});
  startRotate();
}

function spiral() {
  currentHeading += 2;
  panorama.setPov({heading:currentHeading, pitch:currentPitch, zoom: currentZoom});
}

function stopAndZoom() {
  clearInterval(timer);
  zoomingIn = true;
  timer = window.setInterval(zoom, 500);
}

function stopRotate() {
  isRotating = false;
  panorama.setPov({heading: STARTING_HEADING, pitch: STARTING_PITCH, zoom: currentZoom});
  clearInterval(timer);
}

function startRotate() {
  isRotating = true;
  clearInterval(timer);
  timer = window.setInterval(spiral, SPIRAL_TIMEOUT);
}

function toggleRotate(obj) {
  if (isRotating) {
    stopRotate();
    obj.value = "Start Auto-Rotating";
  } else {
    startRotate();
    obj.value = "Stop Auto-Rotating";
  }
}

function zoom() {
  if (zoomingIn) {
    currentZoom++;
  } else {
    currentZoom--;
  }

  panorama.setPov({heading: currentHeading, pitch: currentPitch, zoom: currentZoom});
  if (currentZoom == 2) {
    zoomingIn = false;
  }
  if (currentZoom == 0) {
    startRotate();
  }
}

