  function addSampleButton(caption, clickHandler) {
        var btn = document.createElement('input');
        btn.type = 'button';
        btn.value = caption;
        
        if (btn.attachEvent)
          btn.attachEvent('onclick', clickHandler);
        else
          btn.addEventListener('click', clickHandler, false);

        // add the button to the Sample UI
        document.getElementById('sample-ui').appendChild(btn);
      }
      
      function addSampleUIHtml(html) {
        document.getElementById('sample-ui').innerHTML += html;
      }
  var ge;
    // store the object loaded for the given file... initially none of the objects
    // are loaded, so initialize these to null
    var currentKmlObjects = {
      'mo-impacts': null,
      'bc-climate-champions': null,
      'fco-campaigns': null
    };
  google.load("earth", "1");


  function init() {
    google.earth.createInstance('map3d', initCallback, failureCallback);
      addSampleUIHtml(
        '<h2>Tick boxes to view Google Earth layers</h2>' +
        '<input type="checkbox" id="kml-mo-impacts-check" onclick="toggleKml(\'mo-impacts\');"/ checked="checked"><label for="kml-mo-impacts-check">Climate Change Impacts</label><br/>' +
        '<input type="checkbox" id="kml-fco-campaigns-check" onclick="toggleKml(\'fco-campaigns\');"/><label for="kml-fco-campaigns-check">FCO Climate Change Work</label><br/>' +
        '<input type="checkbox" id="kml-bc-climate-champions-check" onclick="toggleKml(\'bc-climate-champions\');"/><label for="kml-bc-climate-champions-check">British Council Climate Champions</label><br/>' 
      );
      
  }
  
  function initCallback(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(0, 0,
        0, // altitude
        ge.ALTITUDE_RELATIVE_TO_GROUND,
        0, // heading
        0, // straight-down tilt
        20000000 // range (inverse of zoom)
        );
      ge.getView().setAbstractView(la);
    
      // if the page loaded with checkboxes checked, load the appropriate
      // KML files
      if (document.getElementById('kml-mo-impacts-check').checked)
        loadKml('mo-impacts');
    
      if (document.getElementById('kml-bc-climate-champions-check').checked)
        loadKml('bc-climate-champions');
    
      if (document.getElementById('kml-fco-campaigns-check').checked)
        loadKml('fco-campaigns');
    
      document.getElementById('installed-plugin-version').innerHTML =
        ge.getPluginVersion().toString();
    }
    
    function failureCallback(errorCode) {
    }
    
    function toggleKml(file) {
      // remove the old KML object if it exists
      if (currentKmlObjects[file]) {
        ge.getFeatures().removeChild(currentKmlObjects[file]);
        currentKmlObject = null;
      }
    
      // if the checkbox is checked, fetch the KML and show it on Earth
      var kmlCheckbox = document.getElementById('kml-' + file + '-check');
      if (kmlCheckbox.checked)
        loadKml(file);
    }
    
    function loadKml(file) {
     	var kmlUrl = 'http://www.fco.gov.uk/' + file + '.kml';
    
      // fetch the KML
      google.earth.fetchKml(ge, kmlUrl, function(kmlObject) {
        // NOTE: we still have access to the 'file' variable (via JS closures)
    
        if (kmlObject) {
          // show it on Earth
          currentKmlObjects[file] = kmlObject;
          ge.getFeatures().appendChild(kmlObject);
        } else {
          // bad KML
          currentKmlObjects[file] = null;
    
          // wrap alerts in API callbacks and event handlers
          // in a setTimeout to prevent deadlock in some browsers
          setTimeout(function() {
            alert('Bad or null KML.');
          }, 0);
    
          // uncheck the box
          document.getElementById('kml-' + file + '-check').checked = '';
        }
      });
    }
    google.setOnLoadCallback(init);


