var locationDetails;

/*
	Moves the camera to a given location

	@param 	lng		Longitude position of the location
	@param 	lat		Latitude position of the location
	@param 	alt		Alititude of position above see level
	@param 	head	Heading/direction the camrera should point. Measured in deg from Due North
	@param 	tlt		Angle of tilt on the camera. Measured clockwise from 90 deg horizontal to plane
	@param	range	Distance of camera from position
*/


function showLocation( la )
{
	
	//close open balloon
	ge.setBalloon(null);
	
	/*
	We need to loop through and delete locations that are not the current
	locations as without this duplicates are created
	*/
	
	var features = ge.getGlobe().getFeatures().getChildNodes();
	
	for(var i = 0; i < features.getLength(); i++) 
	{
	  
	  var feat = features.item(i);
		
		if (feat.getType() == 'KmlPlacemark')
		{
			
			//alert(i + ' ' + la.position);
	
			if (i != la.position)
			{
			
				//alert(feat.getName() + ' ' + feat.getType() + ' ' + la.position + ' ' + i);
	  			ge.getFeatures().removeChild(features.item(i));
	  		
			}
	  
		}
	  
	}	
	
	
	//	Create LookAt instance in the GEPlugin Instance
	var LookAtInstance = ge.createLookAt('');
  	
  	//	Set the GeoPosition properties too change the View pane too
  	LookAtInstance.set(la.latitude, la.longitude, la.altitude, ge.ALTITUDE_RELATIVE_TO_GROUND, la.heading, la.tilt, la.range);
  	
  	//	Get the current view in the GEPlugin instance and set the View
  	ge.getView().setAbstractView(LookAtInstance);    
  		
  		//	la.set(37.77976, -122.418307, 25, ge.ALTITUDE_RELATIVE_TO_GROUND, 
    //     180, 60, 500);
    
    //alert( la.latitude  + " " + la.longitude);
    
    var placemark = ge.createPlacemark('');
		placemark.setName( la.name );
		placemark.setDescription( la.description );
				
		// Placemark/Point
		var point = ge.createPoint('');
		var lookAt = ge.getView().copyAsLookAt( ge.ALTITUDE_RELATIVE_TO_GROUND );
		point.setLatitude( la.latitude );
		point.setLongitude( la.longitude );
		placemark.setGeometry(point);
		
		
		// style stuff
		// get the style kml
		var features = ge.getGlobe().getFeatures().getChildNodes();
		
		var styleKml = features.item(0);
	
		// Placemark/Style
		var style = ge.createStyle('');
		placemark.setStyleUrl(la.styleUrl);

		// Placemark/Style/IconStyle
		var icon = ge.createIcon('');
		style.getIconStyle().setIcon(icon);
				
		// add the placemark to Earth
		ge.getFeatures().appendChild( placemark );
	 
		google.earth.addEventListener(placemark, 'click', function(event) {
  		
		  // Prevent the default balloon from appearing
		  event.preventDefault(); 
		
		// create a new html balloon
		  var balloon = ge.createHtmlStringBalloon('');
		  // pass in our description
		  
		  locationDetails = '<div id="location_box" style="width:450px">' + placemark.getDescription() + '</div>';
	
		  balloon.setContentString(locationDetails);
		  balloon.setFeature(placemark); // optional
		  balloon.setMaxWidth(450);		  
		  ge.setBalloon(balloon);
		  
		  $('#location_box').pngFix();
					
		  // wrap alerts in API callbacks and event handlers
		  // in a setTimeout to prevent deadlock in some browsers
		  setTimeout(function() {
			
		  }, 0);

		
		});
		

}

/*
	changes the screen overlay for a title

	@param 	lng		Longitude position of the location
	@param 	lat		Latitude position of the location
	@param 	alt		Alititude of position above see level
	@param 	head	Heading/direction the camrera should point. Measured in deg from Due North
	@param 	tlt		Angle of tilt on the camera. Measured clockwise from 90 deg horizontal to plane
	@param	range	Distance of camera from position
*/


function changeTitle( f )
{

	var features = ge.getGlobe().getFeatures().getChildNodes();
	
	for(var i = 0; i < features.getLength(); i++) 
	{
	  
	  var feat = features.item(i);
		
	  ge.getFeatures().removeChild(feat);
	  
	  //alert(feat.getType() + '; ' + feat.getName());
	  
	}

	// create a new overlay
	var screenOverlay = ge.createScreenOverlay('');
	screenOverlay.setVisibility(false);
	screenOverlay.setIcon(ge.createIcon(''));
	screenOverlay.getIcon().setHref(f.iconHref);
		
	var screenXY = screenOverlay.getScreenXY();

	screenXY.setXUnits(GetUnits(ge, f.screenXUnits));
	screenXY.setYUnits(GetUnits(ge, f.screenYUnits));
	screenXY.setX(f.screenX);
	screenXY.setY(f.screenY);
	
	var overlayXY = screenOverlay.getOverlayXY();
	overlayXY.setXUnits(GetUnits(ge, f.overlayXUnits));
	overlayXY.setYUnits(GetUnits(ge, f.overlayYUnits));
	overlayXY.setX(f.overlayX);
	overlayXY.setY(f.overlayY);
		
	var rotationXY = screenOverlay.getRotationXY();
	rotationXY.setXUnits(GetUnits(ge, f.rotationXUnits));
	rotationXY.setYUnits(GetUnits(ge, f.rotationYUnits));
	rotationXY.setX(f.rotationX);
	rotationXY.setY(f.rotationY);

	screenOverlay.setVisibility(true);
	
	ge.getFeatures().appendChild(screenOverlay);
	
}

/**
* GetUnits()
* 
* passes back a unit constant
* 
* @author						Neil Young neil.young@substance001.com
* @version						0.9
* @todo							???
* 
* @params object				ge (Google Earth plugin)
* @params string				unit
* 
*/

function GetUnits(ge, stringUnits)
{
	
	switch(stringUnits)
	{
		
		case 'fraction':
		
			return ge.UNITS_FRACTION;
		
		break;
		
		case 'pixels':
		
			return ge.UNITS_PIXELS;
		
		break;
		
		case 'insetpixels':
		
			return ge.UNITS_INSETPIXELS;
		
		break;
		
		default:
		
			return false;
			
		break;
		
	}	
	
}

/*
	Triggered once the KML is loaded into the GEPlugin instance

*/
function finished(object)
{
  /*if (!object) {
    alert('bad or NULL kml');
    return;
  }
  
  ge.getFeatures().appendChild(object);
  var la = ge.createLookAt('');
  la.set(37.77976, -122.418307, 25, ge.ALTITUDE_RELATIVE_TO_GROUND, 
         180, 60, 500);
  ge.getView().setAbstractView(la);      */   
}

function loadSomeKML()
{

	var url = 'http://sketchup.google.com/' + 
          '3dwarehouse/download?mid=28b27372e2016cca82bddec656c63017&rtyp=k2';
	google.earth.fetchKml(ge, url, finished);
	
	
}

/**
* GetUnits()
* 
* passes back a unit constant
* 
* @author						Neil Young neil.young@substance001.com
* @version						0.9
* @todo							???
* 
* @params string				title (video title)
* @params string				url (YouTube video embed url)
* 
*/

function WatchVideo(title, url)
{

	//alert(locationDetails);
	
	// create a new html balloon
	//var balloon = ge.createHtmlStringBalloon('');
	
	// pass in our description
	/*balloon.setContentString('<div style="width:450px; text-align:center"><h3 style="font-family:sans-serif; font-size:14px; font-weight:bold">' + title + '</h3></div><div style="width:450px; height:300px; text-align:center"><object width="400" height="300"><param name="movie" '
      + 'value="' + url + '"/>'
      + '<param name="allowFullScreen" value="true"/>'
      + '<embed src="' + url + '" '
      + 'type="application/x-shockwave-flash" allowfullscreen="true" '
      + 'width="400" height="300"></embed></object></div>');*/
	
	$('#location_box').html('<div id="location_box" style="width:450px; text-align:center"><h3 style="font-family:sans-serif; font-size:14px; font-weight:bold">' + title + '</h3></div><div style="width:450px; height:300px; text-align:center"><object width="400" height="300"><param name="movie" '
      + 'value="' + url + '"/>'
      + '<param name="allowFullScreen" value="true"/>'
      + '<embed src="' + url + '" '
      + 'type="application/x-shockwave-flash" allowfullscreen="true" '
      + "width='400' height='300'></embed></object><div style='width:450px; text-align:center'><a style='color:#000; text-decoration:underline' href='#' onclick=\"$('#location_box').html(locationDetails);\">Back to " + title + "</a></div></div>");
	
	$('#location_box').pngFix();
	
	//balloon.setMaxWidth(450);		  
	//ge.setBalloon(balloon);
					
	// wrap alerts in API callbacks and event handlers
	// in a setTimeout to prevent deadlock in some browsers
	//setTimeout(function() {
			
	//}, 0);
	
}