    var ge;
    
	var la;
  var latitude;
  var longitude;
  var heading;
  var buildings;
  var turning;
  var frameendEL;
  var kmzs;
  var nls;

  
var fileOne = null;
var fileTwo = null;
var fileThree = null;
var fileFour = null;
var fileFive = null;
var fileSix = null;
var fileSeven = null;
var fileEight = null;
var fileNine = null;
var fileTen = null;
var fileEleven = null;
var fileTwelve = null;

	
    google.load("earth", "1");
	google.load("maps", "2");
    
    function init() {
      google.earth.createInstance('map3d', initCallback, failureCallback);
    }
    
    function initCallback(pluginInstance) {
      ge = pluginInstance;
      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);
      ge.getLayerRoot().enableLayerById(ge.LAYER_BUILDINGS, true);
      ge.getLayerRoot().enableLayerById(ge.LAYER_BUILDINGS_LOW_RESOLUTION, true);
	  ge.getOptions().setFlyToSpeed(0.2); 
	  
	  //code for overlay

// Create the ScreenOverlay.
         var screenOverlay = ge.createScreenOverlay('');
 
         // Specify a path to the image and set as the icon.
         var icon = ge.createIcon('');
         icon.setHref('http://ec2-174-129-112-117.compute-1.amazonaws.com/ccborder.png');
         screenOverlay.setIcon(icon);
 
         // Important note: due to a bug in the API, screenXY and overlayXY
         // have opposite meanings than their KML counterparts. This means
         // that in the API, screenXY defines the point on the overlay image
         // that is mapped to a point on the screen, defined by overlayXY.
 
         // This bug will not be fixed until the next major revision of the
         // Earth API, so that applications built upon version 1.x will
         // not break.
 
			// Set the ScreenOverlay's position in the window.
         screenOverlay.getOverlayXY().setXUnits(ge.UNITS_PIXELS);
         screenOverlay.getOverlayXY().setYUnits(ge.UNITS_PIXELS);
         screenOverlay.getOverlayXY().setX(85);
         screenOverlay.getOverlayXY().setY(30);
 
         // Specify the point in the image around which to rotate.
         screenOverlay.getRotationXY().setXUnits(ge.UNITS_FRACTION);
         screenOverlay.getRotationXY().setYUnits(ge.UNITS_FRACTION);
         screenOverlay.getRotationXY().setX(0);
         screenOverlay.getRotationXY().setY(0);
 
			// Set the overlay's size in pixels.
         screenOverlay.getSize().setXUnits(ge.UNITS_PIXELS);
         screenOverlay.getSize().setYUnits(ge.UNITS_PIXELS);
         screenOverlay.getSize().setX(154);
         screenOverlay.getSize().setY(55);
 
         // Rotate the overlay.
         screenOverlay.setRotation(0);
 
         // Add the ScreenOverlay to Earth.
         ge.getFeatures().appendChild(screenOverlay);
//code end for overlay

		fileOne = getNL('http://ec2-174-129-112-117.compute-1.amazonaws.com/bizkmlnew1/aero.kml');
  		fileTwo = getNL('http://ec2-174-129-112-117.compute-1.amazonaws.com/bizkmlnew1/bio.kml');
  		fileThree = getNL('http://ec2-174-129-112-117.compute-1.amazonaws.com/bizkmlnew1/bus.kml');
 		fileFour = getNL('http://ec2-174-129-112-117.compute-1.amazonaws.com/bizkmlnew1/consumer.kml');
        fileFive = getNL('http://ec2-174-129-112-117.compute-1.amazonaws.com/bizkmlnew1/dining.kml');
		fileSix = getNL('http://ec2-174-129-112-117.compute-1.amazonaws.com/bizkmlnew1/it1.kml');
		fileSeven = getNL('http://ec2-174-129-112-117.compute-1.amazonaws.com/bizkmlnew1/manu.kml');
		fileEight = getNL('http://ec2-174-129-112-117.compute-1.amazonaws.com/bizkmlnew1/media.kml');
  		fileNine = getNL('http://ec2-174-129-112-117.compute-1.amazonaws.com/bizkmlnew1/shop.kml');
        fileTen = getNL('http://ec2-174-129-112-117.compute-1.amazonaws.com/bizkmlnew1/sports.kml');
  		fileEleven = getNL('http://ec2-174-129-112-117.compute-1.amazonaws.com/bizkmlnew1/trans.kml');
  		fileTwelve = getNL('http://ec2-174-129-112-117.compute-1.amazonaws.com/bizkmlnew1/whole.kml');
  
        updateOptions();
	  
	  
      //slow motion
	  ge.getOptions().setFlyToSpeed(0.2); 
	  la = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
	  var win = ge.getWindow();
      window.google.earth.addEventListener(win, 'mousedown',stopFly);
      //view
	  SetDefaultView();
	  
	  
	   function finished(object1) {
    if (!object1) {
      // 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);
      return;
    }
    ge.getFeatures().appendChild(object1);
    }
	var url1= 'http://ec2-174-129-112-117.compute-1.amazonaws.com/focus.kmz';
	var url2= 'http://ec2-174-129-112-117.compute-1.amazonaws.com/bizkmlnew/def.kml';
	
	
    
	google.earth.fetchKml(ge, url1, finished);
	google.earth.fetchKml(ge, url2, finished);
    }
	
	function getNL(kmlURL){
  var nl = ge.createNetworkLink("");
  var link = ge.createLink("");
  link.setHref(kmlURL);
  nl.setLink(link);
/*nl.setFlyToView(true);*/
  ge.getGlobe().getFeatures().appendChild(nl);
  return nl;
}


function updateOptions() {
  fileOne.setVisibility(document.options.file1.checked);
  fileTwo.setVisibility(document.options.file2.checked);
  fileThree.setVisibility(document.options.file3.checked);
  fileFour.setVisibility(document.options.file4.checked);
  fileFive.setVisibility(document.options.file5.checked);
  fileSix.setVisibility(document.options.file6.checked);
  fileSeven.setVisibility(document.options.file7.checked);
  fileEight.setVisibility(document.options.file8.checked);
  fileNine.setVisibility(document.options.file9.checked);
  fileTen.setVisibility(document.options.file10.checked);
  fileEleven.setVisibility(document.options.file11.checked);
  fileTwelve.setVisibility(document.options.file12.checked);

}
    
    function failureCallback(errorCode) {
    }
    	function SetDefaultView(){
				
 
				var camera = ge.getView().copyAsCamera(ge.ALTITUDE_RELATIVE_TO_GROUND);
 				camera.setLatitude( 33.914061);
				camera.setLongitude(-118.390079);
 				camera.setAltitude(700);
				camera.setTilt(45);
 				camera.setRoll(0.00);
				ge.getView().setAbstractView(camera);
				
		
				
			}
	//to fly to
	 function createPlacemark(lat,lng){
  var placemark = ge.createPlacemark('');
  var point = ge.createPoint('');
  point.setLatitude(lat);
  point.setLongitude(lng);
  placemark.setGeometry(point);
  var icon = ge.createIcon('');
  icon.setHref('http://maps.google.com/mapfiles/kml/paddle/red-circle.png');
  var style = ge.createStyle('');
  style.getIconStyle().setIcon(icon);placemark.setStyleSelector(style);
  ge.getFeatures().appendChild(placemark);
  }
  function za(lat,lng,head,range,altitude,tilt){
    turning = false;
    latitude = lat;
    longitude = lng;
    heading = head;
    la.set(lat,lng,0,ge.ALTITUDE_RELATIVE_TO_GROUND,heading,tilt,range);
    if (!frameendEL){
    toggleFrameendEL();
    }
    ge.getOptions().setFlyToSpeed(0.2);
    ge.getView().setAbstractView(la);
    turning = false;
    
    }
  
  function toggleFrameendEL(){
  if (!frameendEL){
  window.google.earth.addEventListener(ge, 'frameend', zoomAround);
  frameendEL = true;
  }else{
  google.earth.removeEventListener(ge, 'frameend', zoomAround);
  frameendEL = false;
  }
  
  }
  function zoomAround(){
              
    latemp = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
    
    if ((latemp.getLatitude().toFixed(4) == latitude.toFixed(4) && latemp.getLongitude().toFixed(4) == longitude.toFixed(4))|turning){
    turn();
	ge.getOptions().setFlyToSpeed(0.2);
    turning = true;
   
    }
     
 
 
 
 
 
  }
  
  function turn(){
   ge.getOptions().setFlyToSpeed(1000);
    heading+=.5;
    if (heading > 360){
    heading -= 360;}   
 
    la.setHeading(heading);
    la.setLatitude(latitude);
    la.setLongitude(longitude);
    ge.getView().setAbstractView(la);    
  }
 
  function buttonClick(lat,lng,head,range,altitude,tilt) {
    za(lat,lng,head,range,altitude,tilt);
	//createPlacemark(lat,lng);
  }
  function stopFly() {
    if(frameendEL){
      toggleFrameendEL();
    }
    ge.getOptions().setFlyToSpeed(1);
    turning = false;
  }
  
 function butclick() {
      var geocodeLocation = document.getElementById('id_search').value;
    
      var geocoder = new google.maps.ClientGeocoder();
      geocoder.getLatLng(geocodeLocation, function(point) {
        if (point) {
          var lookAt = ge.createLookAt('');
          lookAt.set(point.y, point.x, 0, ge.ALTITUDE_RELATIVE_TO_GROUND,
                     0, 45 , 150);
          ge.getView().setAbstractView(lookAt);
		  ge.getOptions().setFlyToSpeed(0.2); 
        }
      });
    }
 
