// load google earth api
google.load("earth", "1");

// reset ge object
var ge = null;
var conf = {};

// init function to create the google earth instance
function init() {
  google.earth.createInstance(containerId, initCB, failureCB);
}

// innit the callback object, gets called after the creation of the google earth instance
function initCB(object) {
  ge = object;
  ge.getWindow().setVisibility(true);
	updateOptions(); // update settings
	loadKml(); // load kml file
}

function failureCB(object) {
	// add error output if google earth creation fails
}
 
// update display options from conf array
function updateOptions(uid) {
  var options = ge.getOptions();
  options.setStatusBarVisibility(conf.statusBarVisibility);
  options.setGridVisibility(conf.gridVisibility);
  options.setOverviewMapVisibility(conf.overviewMapVisibility);
  options.setScaleLegendVisibility(conf.scaleLegendVisibility);
  options.setAtmosphereVisibility(conf.atmosphereVisibility);
  options.setMouseNavigationEnabled(conf.mouseNavigationEnabled);
  ge.getNavigationControl().setVisibility(conf.navigationControlVisibility);
}

function loadKml() {
	// load kml / kmz
	google.earth.fetchKml(ge, conf.kml, finishedKmlLoading); 
}

function finishedKmlLoading(object) {
  if (!object) {
    alert('bad or NULL kml');
    return;
  }
  ge.getFeatures().appendChild(object);
	
	// create initial lookat
	createLookAt();
	
	// load additional content
	additionalContent();
} 

function createLookAt() {
	  var la = ge.createLookAt('');
  // void set(double latitude, double longitude, double altitude, KmlAltitudeModeEnum altitudeMode, double heading, double tilt, double range);
  la.set(conf.latitude, conf.longitude, conf.altitude, ge.ALTITUDE_MODE_RELATIVE_TO_GROUND, conf.heading, conf.tilt, conf.range);
  ge.getView().setAbstractView(la);
}

// init
google.setOnLoadCallback(init);
