
function openGoogleEarth(headerTxt) {	

	// execution code:
	
	var $win = $("#googleMapWin");
	// map div is not within the popup window div because google has display issues otherwise
	var $map = $("#map");
	var geoCoder;// = new GClientGeocoder();
	var map;
		
	window.mapState = {
		points: []
		,skipped: 0
		,add: function(point)
		{
			if (point)
				this.points.push(point);
			
			else
				this.skipped++;
				
			//if (this.data.length == this.skipped + this.points.length)
				//this.centerMarkers();
		}
	}
	
	// open the window's initial state
	$win.animate({
		opacity:"show",
		left: "190px",
		top: "200px",
		width: "600px",
		height: "400px"
	}, 700);
	
	// display the contents of the window (with a delay to let the previous animation finish)
	displayContents.delay(700);
	
	// register the google maps script (lazy loads the script if it isn't already loaded)
	Behaviors.registerScripts([SCRIPTS.GoogleMaps, SCRIPTS.GoogleEarth], loadMap)
		
	// private functions:
	
	function close(){
		// ensure that any map popups are closed
		map.closeInfoWindow();
		
		// hide map
		$map.hide();
		
		$win.children().fadeOut();
		$("#mapNoPlugin").fadeOut();
		
		// empty the contents of the window but delay it so that the fade isnt cut short
		$win.empty.delay(500, $win);
		
		// delay the window animation so that the fade isnt cut short
		$win.animate.delay(400, $win, {
			opacity: "hide",
			left: "390px",
			top: "300px",
			width:"200px",
			height:"200px"		
		});	
		
		// single to the flash object that it should be re-activated
		document.noFlash.activateFlash();
				
	}
	
	function loadData()
	{
		if (!geoCoder)
			geoCoder = new GClientGeocoder();
		
		// count used to indicate how many items have been successfully geocoded or skipped from geocoding
		
		// load jason data
		$.getJSON("/webservices/parsonsService.svc/GetAllProjects", function(data){
			if (data)
			{
				mapState.data = data;
				getNext();
				//setTimeout(function() { alert(geoDelay + " " + nextAddress); }, 10000);
			}
			
		});	
	}
  
	var geoDelay = 0;
	var nextAddress = 0;
	function getNext() {
		if (nextAddress < mapState.data.length) {
		  var callIndex = nextAddress;
          setTimeout(function() { loadAddress(mapState.data[callIndex], getNext); }, geoDelay);
          nextAddress++;
        } 
	}
	
	// use the geo coder to get actual lat/long information
	function loadAddress(address, next) {
		geoCoder.getLocations(address.location, function(response)
	    {
	    	var point = response.Placemark[0].Point;
	    	if (response.Status.code == "200") {
		    	if (!point)
		    		mapState.add();
		    				
				function createContentDiv(){		    	
				    // create a new element to be used as the markers info html
					var $el = $("<div class='gmap-info'></div>");	
									    
					if (address.thumbnailImage)
				    	$el.append("<img src='" + address.largeThumbnailImage.split(",")[0] + "'>")
				    
					$el.append("<h5>" + /*address.title || */address.location + "</h5>");
					$el.append("<p>" + address.homePageDescription || "" + "</p>");	
								    	
					if (address.url)
						$el.append("<nobr><a href='" + address.url.split(",")[0] + "' title='Read more about this location'>Read more</a></nobr>");					    	
						
					if (map.ge)
						$el.css("padding-left", "10px");
												
					return $el.get(0);
				}   
				 							
				if (map.ge)
				{	
					var geIcon = map.ge.createIcon('');
					geIcon.setHref('http://maps.google.com/mapfiles/kml/paddle/red-circle.png');
					
					var geStyle = map.ge.createStyle('');
					geStyle.getIconStyle().setIcon(geIcon);
					
					var gePoint = map.ge.createPoint('');
					gePoint.setLatLng(point.coordinates[1], point.coordinates[0]);
					
					var gePlacemark = map.ge.createPlacemark('');
					gePlacemark.setName(address.title);
					gePlacemark.setGeometry(gePoint);
					gePlacemark.setStyleSelector(geStyle);
					map.ge.getFeatures().appendChild(gePlacemark);					
					
					google.earth.addEventListener(gePlacemark, "click", function(e){
						e.preventDefault();
						map.ge.setBalloon(null);
						
						var geBalloon = map.ge.createHtmlDivBalloon('');
						geBalloon.setFeature(gePlacemark);
						geBalloon.setContentDiv(createContentDiv());
						geBalloon.setMinWidth(245);
						map.ge.setBalloon(geBalloon);
					});
					
					
				}
				else
				{
					var latlng = new GLatLng(point.coordinates[1], point.coordinates[0]);
					var marker = new GMarker(latlng, {
					    title: address.title || address.location
				    });
				
					// store data and point information for later retrieval
			    	marker.data = address;
					marker.bindInfoWindow(createContentDiv());
					map.addOverlay(marker);
				}
					
				mapState.add(point);
			}	
			else {
				if (response.Status.code == "620") {
					geoDelay += 50;
					nextAddress--;
				}
			}
			next();  														     
		});	
	}
	
	function loadMap()
	{
		// if map hasnt been previously loaded then do it now
		if (!window.gmap)
		{
			map = new GMap2($map.get(0), {
			    mapTypes: [G_NORMAL_MAP, G_SATELLITE_3D_MAP]
		    });
		    
		    window.gmap = map;	
		    
			map.getEarthInstance(function(obj)
		    {
		    	// if there is an instance, associate it with the map
		    	if (obj)
		    	{
		    		map.ge = obj;
		    		//map.ge.getNavigationControl().setVisibility(map.ge.VISIBILITY_AUTO);
		    		// load data with a delay to help keep everything smooth
					loadData.delay(500);
		    	}
		    	// otherwise show "View in Google Maps" option
		    	else
		    	{
		    		$("#mapNoPlugin").fadeIn();
		    		$("#viewInGoogleMaps")
			    		.click(function(e){
			    			$("#mapNoPlugin").hide()
			    			map.setMapType(G_NORMAL_MAP);
			    			map.addControl(new GLargeMapControl());
			    			
			    			// load data with a delay to help keep everything smooth
							loadData();
			    			e.preventDefault();
			    		});	
		    	}	
		    });
		    
		    // set the map type to google earth
		    map.setMapType(G_SATELLITE_3D_MAP);		
		}
		// otherwise re-use the existing map
		else
		{
			map = window.gmap;	
			// if google earth isnt installed and Google Maps hasnt yet been choosen then give the user the option to view in google maps
			if (!map.ge && map.getCurrentMapType() == G_SATELLITE_3D_MAP)
				setTimeout('$("#mapNoPlugin").fadeIn()', 400);
		}
		
		// set default view for map
		map.setCenter(new GLatLng(39, -99), 4);
		map.setZoom(2);
		
		setTimeout(function() {
			if (map.ge) {
				
				map.ge.getOptions().setFlyToSpeed(0.5);
				var lookAt = map.ge.getView().copyAsCamera(map.ge.ALTITUDE_RELATIVE_TO_GROUND);
		      	lookAt.setAltitude(5000000);
				map.ge.getView().setAbstractView(lookAt);
			}
			else {
				map.setZoom(3);
			}
		}, 2000);

		
	}
		
	function displayContents()
	{
		headerTxt = headerTxt || "";
		
		$("<h4>" + headerTxt + "<span>&nbsp;</span></h4>")
			.appendTo($win)
			.hide()
			.fadeIn()
			.children("span")
				.hover(function(){
					$(this).css("background-position", "0 -39px");
				}, function(){
					$(this).css("background-position", "0 0");
				})
				.click(close);			
	
		$map.show();
	}
}
