	//define globals
	var ge;
	var placemark;
	var content;
	var name;
	var thisPlace;
	var roads = true;
	var legend = true;
	var pm_data = new Array(6);
	for (h=0;h<pm_data.length;h++){
		pm_data[h] = new Array();
		}
	var tour_data = new Array();
	var layer_data = new Array();
	//var kmlPath = 'http://192.168.2.209/ufiles/kml.kml';
	//var kmlPath2 = 'http://192.168.2.209/ufiles/layers.kml';
	var kmlPath = 'http://map.greenspacealliance.org/ufiles/kml.kml';
	var kmlPath2 = 'http://map.greenspacealliance.org/ufiles/layers.kml';
	var gex = new GEarthExtensions(ge);
	
	//load earth	
	google.load("earth", "1");
	
	function init() {
		google.earth.createInstance('map3d', initCB, failureCB);
	}
	
	function initCB(instance) {
		ge = instance;
		ge.getWindow().setVisibility(true);
		ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);
		ge.getLayerRoot().enableLayerById(ge.LAYER_TERRAIN, true);
		ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);
		 
		 google.earth.fetchKml(ge, kmlPath, function(kmlObject) { 
			if (kmlObject){
				ge.getFeatures().appendChild(kmlObject);
				}
			if (kmlObject.getAbstractView() !== null) {
				ge.getView().setAbstractView(kmlObject.getAbstractView());
			}
		}); 
		
		google.earth.fetchKml(ge, kmlPath2, function(kmlObject2) { 
			if (kmlObject2){
				ge.getFeatures().appendChild(kmlObject2);
				buildMenus();
				buildLayers();
				setInitMenu()
				}
		}); 
	}
	
	function failureCB(errorCode) {
	}
	
	//Startup the GEPlugin
	google.setOnLoadCallback(init);
	
	// <------------------------------------->
	// Functions KML DOM manipulation
	// <------------------------------------->
	function getBalloonType(content) {
		var bgColor = "#ffffff";
		if(content.indexOf('<div id="historical">')>-1) {
			bgColor = "#5f3816";
		}
		if(content.indexOf('<div id="ecological">')>-1) {
			bgColor = "#5f6944";
		}
		if(content.indexOf('<div id="recreational">')>-1) {
			bgColor = "#627079";
		}
		if(content.indexOf('<div id="trail">')>-1) {
			bgColor = "#5e5d4e";
		}
		if(content.indexOf('<div id="projects">')>-1) {
			bgColor = "#5f6944";
		}
		return bgColor;
	}
	
	function popBalloon(i,j) {
		clearBalloon();
		//delay poping the new balloon to ensure correct placement.
		setTimeout ( function(){
		//get the clicked on feature object in DOM
		placemark = pm_data[i][j];
		content = placemark.getDescription();
		name = placemark.getName();
		bgColor = getBalloonType(content);
		var p = ge.createHtmlStringBalloon('');
			p.setFeature(placemark);
			p.setBackgroundColor(bgColor);
			p.setMaxWidth(400);
			p.setContentString(content);
			ge.setBalloon(p);	
		}, 100);
	}
	
	function clearBalloon() {
		//closes open balloon
		ge.setBalloon(null);
		thisPlace = null;
	}
	
	function getPlacemarks() {
		// Find all the placemarks.
		//console.log('placemarks called');
		gex.dom.walk({
			rootObject: ge,
			visitCallback: function() {
				//console.log('walkling the dom');
				if ('getType' in this && this.getType() == 'KmlPlacemark'){
					pmType=String(this.getStyleUrl());
					pmType=pmType.split("#");
					pmType=pmType[1];
					//console.log('placemark found. Style = ' + pmType);
					switch (pmType){
						case 'msn_access':
							pm_data[0].push(this);
							//console.log('access point added');
							break;
						case 'msn_ecological':
							pm_data[1].push(this);
							//console.log('eco point added');
							break;
						case 'msn_historical':
							pm_data[2].push(this);
							//console.log('historical point added');
							break;
						case 'msn_recreational':
							pm_data[3].push(this);
							//console.log('recreational point added');
							break;
						case 'msn_projects':
							pm_data[4].push(this);
							//console.log('project point added');
							break;
						case 'sn_trails':
							pm_data[5].push(this);
							//console.log('shape layer added');
							break;
						case 'sn_watershed':
							pm_data[5].push(this);
							//console.log('shape layer added');
							break;
						case 'sn_land3':
							pm_data[5].push(this);
							//console.log('shape layer added');
							break;
						case 'sn_creek':
							pm_data[5].push(this);
							//console.log('shape layer added');
							break;
					}
				}
			}
			
		});
		
		for (i=0;i<pm_data.length;i++){
			for (j=0;j<pm_data[i].length;j++){
				if (i == (pm_data.length-1)){
					//alert ('layer function called');
					placemark = pm_data[i][j];
					google.earth.addEventListener(placemark, 'click', function(event) {
					event.preventDefault();
					});
				} else {
				//console.log ('placemark added');
				placemark = pm_data[i][j];
				google.earth.addEventListener(placemark, 'click', function(event) {
					event.preventDefault();
					clearBalloon();
					setTimeout ( function(){
						placemark = event.getTarget	();
						//alert('I think I am ' + event.getTarget	());
						content = placemark.getDescription();
						name = placemark.getName();	
						bgColor = getBalloonType(content);
						var balloon = ge.createHtmlStringBalloon('');
						balloon.setFeature(placemark);
						balloon.setBackgroundColor(bgColor);
						balloon.setMaxWidth(400); // this line if missing screws up clicked on placemark width balloon.
						balloon.setContentString(content);
						ge.setBalloon(balloon);
					},100);
				});
				
				}
			}
		}
		return pm_data;
	}
	
	function testAlert(n){
		alert('function called ' + n);	
	}

	function getTours() {
		// Find all the tours.
		gex.dom.walk({
			rootObject: ge,
			visitCallback: function() {
				if ('getType' in this && this.getType() == 'KmlTour')
					tour_data.push(this);
			}
		});
		return tour_data;
	}
	
	function getLayers() {
		// Find all the tours.
		gex.dom.walk({
			rootObject: ge,
			visitCallback: function() {
				if ('getType' in this && this.getType() == 'KmlScreenOverlay')
					layer_data.push(this);
			}
		});
		return layer_data;
	}
	
	
	function playTour(index) {
		// Play the nth tour
		if (index < tour_data.length) {
			//show hide necessary stuff to prep for tour.
			closeAll();
				
			ge.getTourPlayer().setTour(tour_data[index]);
			ge.getTourPlayer().play();
		}
	}
	
	function closeAll() {
		var viewTypeNames = new Array ("trails","environmental","historical","rec","projects")
		
		for (i=0;i<viewTypeNames.length;i++) {
			closeMenu(viewTypeNames[i]);
			for (j=0;j<pm_data[i].length;j++) {				
					pm_data[i][j].setVisibility(0);				
			}
		}
	}
	
	function toggleLayerGroup(i,group) {
		toggleMenu(group);
		if (group=='layers'){
			//to toggle all on or off, we would need to set a global variable to keep track and set simple functions based on that.
			toggleLegend();
			toggleRoads();
		} else {
			for (j=0;j<pm_data[i].length;j++) {
				var state = pm_data[i][j].getVisibility();
				if (state) {
					pm_data[i][j].setVisibility(0);
				} else {
					pm_data[i][j].setVisibility(1);
				}
			}
		}
	}
	
	function toggleLayer(i, j) {
		if (j < pm_data[i].length) {
		  var state = pm_data[i][j].getVisibility();
			if (state) {
					pm_data[i][j].setVisibility(0);
					setIcon(0,j);
			} else {
				pm_data[i][j].setVisibility(1);
				setIcon(1,j);
			}
		}
	}
	
	function toggleLegend() {
		var state = layer_data[0].getVisibility();
		if (state) {
			layer_data[0].setVisibility(0);
			setIcon(0,'legend');
		} else {
			layer_data[0].setVisibility(1);
			setIcon(1,'legend');
		}
	}
	
	function toggleRoads() {
		if (roads) {
		  	ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, false);
		  	setIcon(0,'roads');
		  	roads = false;
		} else {
			ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);
			setIcon(1,'roads');
			roads = true;
		}
	}
