	/**
	 * Helper function for element.addEventListener/attachEvent
	 */
	function addDomListener(element, eventName, listener) {
	  if (element.addEventListener)
		element.addEventListener(eventName, listener, false);
	  else if (element.attachEvent)
		element.attachEvent('on' + eventName, listener);
	}

	/**
	 * Sets up listeners on 'resume tour' buttons in tour balloons.
	 */
	function checkResumeButton() {
	  var resumeButton = document.getElementById('resume-button');
	  if (resumeButton && !resumeButton.getAttribute('hasResumeListener')) {
		addDomListener(resumeButton, 'click', function(evt) {
		  ge.getTourPlayer().play();
		  
		  if (evt.preventDefault) {
			evt.preventDefault();
			evt.stopPropagation();
		  }
		  return false;
		});
		
		
		resumeButton.setAttribute('hasResumeListener', true);
	  }
	}


		// Watch for resume tour clicks
	  	window.setInterval(checkResumeButton, 100);
  
         var ge;
         var tour;
         google.load("earth", "1");

         function init() {
            google.earth.createInstance('map3d_tkacs', initCB, failureCB);
         }

         function initCB(instance) {
            ge = instance;
            ge.getWindow().setVisibility(true);
            ge.getNavigationControl().setVisibility(ge.VISIBILITY_SHOW);
			
			// add some layers
    	    ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
			ge.getLayerRoot().enableLayerById(ge.LAYER_BUILDINGS_LOW_RESOLUTION, true);
			ge.getLayerRoot().enableLayerById(ge.LAYER_BUILDINGS, true);
	        ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, false);

            var href = 'http://stage.archivejournal.net/wp-content/uploads/kml/walters_studentwork_romancesCasket_tkacs.kml';
            google.earth.fetchKml(ge, href, fetchCallback);

            function fetchCallback(fetchedKml) {
               // Alert if no KML was found at the specified URL.
               if (!fetchedKml) {
                  setTimeout(function() {
                     alert('Bad or null KML');
                  }, 0);
                  return;
               }

               // Add the fetched KML into this Earth instance.
               ge.getFeatures().appendChild(fetchedKml);

               // Walk through the KML to find the tour object; assign to variable 'tour.'
               walkKmlDom(fetchedKml, function() {
                  if (this.getType() == 'KmlTour') {
                     tour = this;
                     return false;
                  }
               });
				if (!tour) {
					alert('No tour found!');
					return;
				}    
				ge.getTourPlayer().setTour(tour);
            }
			document.getElementById('installed-plugin-version').innerHTML =
     	    ge.getPluginVersion().toString();
         }

         function failureCB(errorCode) {
         }

         // Tour control functions
         function enterTour() {
            if (!tour) {
               alert('No tour found!');
               return;
            }
            ge.getTourPlayer().setTour(tour);
         }
         function playTour() {
            ge.getTourPlayer().play();
         }
         function pauseTour() {
            ge.getTourPlayer().pause();
         }
         function resetTour() {
            //ge.getTourPlayer().reset();
			ge.getTourPlayer().setTour(null);
			enterTour();
         }
         function exitTour() {
            ge.getTourPlayer().setTour(null);
         }

         google.setOnLoadCallback(init);
