// JavaScript Document
    var ge;
    var placemark;
    
    google.load("earth", "1");
	google.load("maps", "2");
    
    function init() {
      // Create two buttons that will create and delete the balloon.
      var earthplugin = document.getElementById('earthplugin');
	  var inputHTML = '<input type="button" value="Micro-Website" onclick="changeBalloon()" />';
	  inputHTML += '<input type="button" value="Interaktives Panorama" onclick="changeBalloon1()" />';
	  inputHTML += '<input type="button" value="Blick zur Tramuntana" onclick="tiltSMX()" />';
	  inputHTML += '<input type="button" value="3D Geb&auml;ude laden" onclick="create3dModel()" />';
      earthplugin.innerHTML = inputHTML;
    
      google.earth.createInstance('earthplugin', initCB, failureCB);
    }
    
    function initCB(instance) {
      ge = instance;
      ge.getWindow().setVisibility(true);
    
    
      // add a navigation control
      //ge.getNavigationControl().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_TERRAIN, true);
    
      // create the placemark
      placemark = ge.createPlacemark('');
    
      var point = ge.createPoint('');
      point.setLatitude(39.568325);
      point.setLongitude(2.64493055555556);
      placemark.setGeometry(point);
    
      // add the placemark to the earth DOM
      ge.getFeatures().appendChild(placemark);
    
      // look at the placemark we created
      var lookAt = ge.createLookAt('');
      lookAt.set(39.568325,2.64493055555556,
              0, // altitude
              ge.ALTITUDE_RELATIVE_TO_GROUND,
              0, // heading
              0, // straight-down tilt
              5000 // range (inverse of zoom)
              );
      ge.getView().setAbstractView(lookAt);
	  
			  // listen to the click event
			  google.earth.addEventListener(placemark, 'click', changeBalloon);
    
      var pluginVersion = ge.getPluginVersion().toString();
      document.getElementById('installed-plugin-version').innerHTML = 'Version: ' +
                                                                       pluginVersion;
    }
	
	function tiltSMX() {
		go = function(count) {
		var idealTilt = 80;  // all angles in the API are in degrees.
		var c0 = 0.50;
		var lookAt = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
	
		var tilt = lookAt.getTilt();
		tilt = c0 * tilt + (1 - c0) * idealTilt;
		lookAt.setTilt(tilt);
		ge.getView().setAbstractView(lookAt);
	
		if (count < 60) {
		  setTimeout('go(' + (count+1) + ')', 33);
		}
	  };
	  
	  go(0);
	}
    
    function failureCB(errorCode) {
      //alert(errorCode);
    }
    
	
	function changeBalloon() {
	  if(!balloon){
		  var balloon = ge.createHtmlStringBalloon('');
		  ge.setBalloon(balloon);
	  }
	  // Change the context of the current balloon.
	  var balloon = ge.getBalloon();
	  if (balloon) {
		// Pigeon Rank
		var content =
				'Zeigen Sie eine Micro-Website oder Ihr Logo<br/>' +
				'<img src="http://www.softmax.es/SOFTMAX.ES.png" alt="Microsite" width="471" height="370" border="0" usemap="#microsite" />' +
				'<map name="microsite" id="microsite"><area shape="rect" coords="226,60,277,82" href="http://www.softmax.es/service/sitemap.html" alt="Sitemap" />' +
				'<area shape="rect" coords="285,60,333,81" href="http://www.softmax.es/kontakt.html" alt="Kontakt" />' +
				'<area shape="rect" coords="342,60,408,81" href="http://www.softmax.es/service/impressum.html" alt="Impressum" /></map>';
		balloon.setFeature(placemark); // optional
		balloon.setMaxWidth(800);
	
		if (balloon.getType() == 'GEHtmlStringBalloon') {
		  balloon.setContentString(content);
		} else if (balloon.getType() == 'GEHtmlDivBalloon') {
		  var div = document.createElement('DIV');
		  div.innerHTML = content;
		  balloon.setContentDiv(div);
		} else if (balloon.getType() == 'GEFeatureBalloon') {
		  placemark.setDescription(content);
		}
	  }
	}
	
	function changeBalloon1() {
	  if(!balloon){
		  var balloon = ge.createHtmlStringBalloon('');
		  ge.setBalloon(balloon);
	  }
	  // Change the context of the current balloon.
	  var balloon = ge.getBalloon();
	  if (balloon) {
		// Pigeon Rank
		var content = 'Klicken Sie in das Bild und bewegen Sie sich durch den Raum (104 KB werden geladen)' + 
				'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' +
			'id="Main" width="480" height="300"'+
			'codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">'+
			'<param name="movie" value="http://www.softmax.es/Main.swf" />'+
			'<param name="quality" value="high" />'+
			'<param name="bgcolor" value="#000000" />'+
			'<param name="allowScriptAccess" value="sameDomain" />'+
			'<embed src="http://www.softmax.es/Main.swf" quality="high" bgcolor="#000000"'+
				'width="480" height="300" name="Main" align="middle"'+
				'play="true"'+
				'loop="false"'+
				'quality="high"'+
				'allowScriptAccess="sameDomain"'+
				'type="application/x-shockwave-flash"'+
				'pluginspage="http://www.adobe.com/go/getflashplayer">'+
			'</embed>'+
	'</object>';
		balloon.setFeature(placemark); // optional
		balloon.setMaxWidth(800);
	
		if (balloon.getType() == 'GEHtmlStringBalloon') {
		  balloon.setContentString(content);
		} else if (balloon.getType() == 'GEHtmlDivBalloon') {
		  var div = document.createElement('DIV');
		  div.innerHTML = content;
		  balloon.setContentDiv(div);
		} else if (balloon.getType() == 'GEFeatureBalloon') {
		  placemark.setDescription(content);
		}
	  }
	}
	
	
	function create3dModel() {
	  // Create a 3D model, initialize it from a Collada file, and place it
	  // in the world.
	
	  placemark = ge.createPlacemark('');
	  placemark.setName('model');
	  model = ge.createModel('');
	  ge.getFeatures().appendChild(placemark);
	  loc = ge.createLocation('');
	  model.setLocation(loc);
	  link = ge.createLink('');
	
	  // A textured model created in Sketchup and exported as Collada.
	  var href = window.location.href;
	  var modelPath = 'http://www.softmax.es/dae/Lonja_early_PLAZIERT.dae';
	  link.setHref(modelPath);
	  model.setLink(link);
	
	  lookAt = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
	  loc.setLatitude(lookAt.getLatitude());
	  loc.setLongitude(lookAt.getLongitude());
	
	  placemark.setGeometry(model);
	
	  lookAt.setRange(300);
	  lookAt.setTilt(45);
	  ge.getView().setAbstractView(lookAt);
	}
	

    
    google.setOnLoadCallback(init);

