var ge;
google.load("earth", "1");

function initEarth() {
    // Create two buttons that will start/stop animation.
  if (document.getElementById('earth_canvas')){
    google.earth.createInstance('earth_canvas', initCB, failureCB);
  }
}

function failureCB(errorCode) {
}

var currentKmlObject = null;


function fetchKmlFromServer(kmlUrl) {
  // remove the old KML object if it exists
  if (currentKmlObject) {
    ge.getFeatures().removeChild(currentKmlObject);
    currentKmlObject = null;
  }

  google.earth.fetchKml(ge, kmlUrl, finishFetchKml);
}

function initCB(instance) {
  ge = instance;
  ge.getWindow().setVisibility(true);

  // add a navigation control
  ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);

  // add some layers
  ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
  ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);

  // fly to Santa Cruz
  var la = ge.createLookAt('');
  la.set(20, 19,
          0, // altitude
          ge.ALTITUDE_RELATIVE_TO_GROUND,
          0, // heading
          0, // straight-down tilt
          9900000 // range (inverse of zoom)
  );
  ge.getView().setAbstractView(la);


  fetchKmlFromServer('http://www.thisissolar.com/kml/TISV1.kmz');
}


function finishFetchKml(kmlObject) {
  // check if the KML was fetched properly
  if (kmlObject) {
    // add the fetched KML to Earth
    currentKmlObject = kmlObject;
    ge.getFeatures().appendChild(currentKmlObject);
  } else {
    alert('Bad KML');
  }
}

google.setOnLoadCallback(initEarth);

