    Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJiNWFjZDJiMy1hYzk3LTQ2YWUtYWVjYi01MDk1OGVlZGI0NTgiLCJpZCI6MTEwNzEsInNjb3BlcyI6WyJhc3IiLCJnYyJdLCJpYXQiOjE1NTgwMjE1Mjh9.lF6TGAJrX3qMGBt5egdAA7SJYNnaYX17mhURwGg_Kbg';

    //////////////////////////////////////////////////////////////////////////
    // Creating the Viewer
    //////////////////////////////////////////////////////////////////////////

     var viewer = new Cesium.Viewer('cesiumContainer', {
         scene3DOnly: true,
         selectionIndicator: false,
		 timeline: false,
		 animation: false,
		 shadow: false,
		 // // Set default basemap
            imageryProvider : new Cesium.ArcGisMapServerImageryProvider({
    url : 'https://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer',
	enablePickFeatures: false
    }),
		 baseLayerPicker: true
		 
     });
     viewer.clock.shouldAnimate = false;
	 viewer.scene.globe.enableLighting = false;
     viewer.clock.startTime = Cesium.JulianDate.fromIso8601("2019-09-20T16:00:00Z");
     viewer.clock.stopTime = Cesium.JulianDate.fromIso8601("2019-09-20T16:20:00Z");
     viewer.clock.currentTime = Cesium.JulianDate.fromIso8601("2019-09-20T16:00:00Z");
	 document.getElementById("irradianceLegend").style.display = "none"; //defining legends hidden by default
		 
    //////////////////////////////////////////////////////////////////////////
    // Configuring the camera
    //////////////////////////////////////////////////////////////////////////
    // // Create an initial camera view
     var initialPosition = Cesium.Cartesian3.fromDegrees(9.197574, 48.788913, 900);
    var initialOrientation = new Cesium.HeadingPitchRoll.fromDegrees(360, -45, 360);
     var homeCameraView = {
         destination : initialPosition,
         orientation : {
            heading : initialOrientation.heading,
            pitch : initialOrientation.pitch,
            roll : initialOrientation.roll
         }
      };
    // // Set the initial view
     viewer.scene.camera.setView(homeCameraView);
		// set home button to initial view 
	 
	 viewer.homeButton.viewModel.command.beforeExecute.addEventListener(function (e) {
        e.cancel = true;
         viewer.scene.camera.flyTo(homeCameraView);
     }); 
	 viewer.camera.changed.addEventListener(function() {

  var deg = Math.round( Cesium.Math.toDegrees(viewer.camera.heading))
  console.log('Heading:', deg)

  var deg = Math.round( Cesium.Math.toDegrees(viewer.camera.pitch))
  console.log('Pitch:', deg)
  
  var deg = Math.round( Cesium.Math.toDegrees(viewer.camera.roll))
  console.log('Roll:', deg)
});
    //////////////////////////////////////////////////////////////////////////
    // Load 3D Tileset
    //////////////////////////////////////////////////////////////////////////

    var rosensteinviertelBldgSld = viewer.scene.primitives.add(new Cesium.Cesium3DTileset({url: './Source/Data/Bldg/tileset.json'}));
	//viewer.zoomTo(rosensteinviertelBldgSld);
	
	var rosensteinviertelBldgSrf = viewer.scene.primitives.add(new Cesium.Cesium3DTileset({url: './Source/Data/Srf/tileset.json'}));
	rosensteinviertelBldgSld.show = true;
	rosensteinviertelBldgSrf.show = false;
    //////////////////////////////////////////////////////////////////////////
    // Style 3D Tileset
    //////////////////////////////////////////////////////////////////////////
	
	var defaultstyle = new Cesium.Cesium3DTileStyle({
        color : "color('WHITE')",
			show: true
     });

 
    var irradiancestyle = new Cesium.Cesium3DTileStyle({
         color : {
            conditions : [
					["(${Irr_kWh} > 0)  && (${Irr_kWh} < 50)", "color('#0D0887')"],
					["(${Irr_kWh} >= 50)  && (${Irr_kWh} < 100)", "color('#280592')"],
                    ["(${Irr_kWh} >= 100) && (${Irr_kWh} < 150)", "color('#3C049B')"],
                    ["(${Irr_kWh} >= 150) && (${Irr_kWh} < 200)", "color('#4E02A2')"],
                    ["(${Irr_kWh} >= 200) && (${Irr_kWh} < 250)", "color('#6001A6')"],
					["(${Irr_kWh} >= 250) && (${Irr_kWh} < 300)", "color('#7100A8')"],
					["(${Irr_kWh} >= 300) && (${Irr_kWh} < 350)", "color('#8305A7')"],
					["(${Irr_kWh} >= 350) && (${Irr_kWh} < 400)", "color('#920FA3')"],
					["(${Irr_kWh} >= 400) && (${Irr_kWh} < 450)", "color('#A11B9B')"],
					["(${Irr_kWh} >= 450) && (${Irr_kWh} < 500)", "color('#AE2892')"],
					["(${Irr_kWh} >= 500) && (${Irr_kWh} < 550)", "color('#BB3488')"],
					["(${Irr_kWh} >= 550) && (${Irr_kWh} < 600)", "color('#C6417D')"],
					["(${Irr_kWh} >= 600) && (${Irr_kWh} < 650)", "color('#D04D73')"],
					["(${Irr_kWh} >= 650) && (${Irr_kWh} < 700)", "color('#DA5A6A')"],
					["(${Irr_kWh} >= 700) && (${Irr_kWh} < 750)", "color('#E26660')"],
					["(${Irr_kWh} >= 750) && (${Irr_kWh} < 800)", "color('#EA7457')"],
					["(${Irr_kWh} >= 800) && (${Irr_kWh} < 850)", "color('#F1814D')"],
					["(${Irr_kWh} >= 850) && (${Irr_kWh} < 900)", "color('#F79044')"],
					["(${Irr_kWh} >= 900) && (${Irr_kWh} < 950)", "color('#FBA139')"],
					["(${Irr_kWh} >= 950) && (${Irr_kWh} < 1000)", "color('#FDB130')"],
					["(${Irr_kWh} >= 1000) && (${Irr_kWh} < 1050)", "color('#FDC229')"],
					["(${Irr_kWh} >= 1050) && (${Irr_kWh} < 1100)", "color('#FBD324')"],
					["(${Irr_kWh} >= 1100) && (${Irr_kWh} < 1150)", "color('#F6E626')"],
					["(${Irr_kWh} >= 1150) && (${Irr_kWh} < 1200)", "color('#F0F921')"],
					["(${Irr_kWh} >= 1200)", "color('#F0F921')"],
					["true", "color('#000000')"]
             ]
         },
		 show: true
     });
	 
	 
	 var colorstyle1 = document.getElementById('3dbuildings');	 
	 var colorstyle2 = document.getElementById('irradiance');
	 
     function set3DColorStyle() {
        if (colorstyle1.checked) {
			rosensteinviertelBldgSld.show = true;
			rosensteinviertelBldgSrf.show = false;
            rosensteinviertelBldgSld.style = defaultstyle;
			 document.getElementById("irradianceLegend").style.display = "none";
			 
			  } 
		else if (colorstyle2.checked) {
			rosensteinviertelBldgSld.show = false;
			rosensteinviertelBldgSrf.show = true;
             rosensteinviertelBldgSrf.style = irradiancestyle;
			 document.getElementById("irradianceLegend").style.display = "block";
			 
		      }
	
        }
	 
	 colorstyle1.addEventListener('change', set3DColorStyle);
     colorstyle2.addEventListener('change', set3DColorStyle);
	 

    //////////////////////////////////////////////////////////////////////////
    // Custom mouse interaction for highlighting and selecting
    //////////////////////////////////////////////////////////////////////////
//Style the overlay
var nameOverlay = document.createElement('div');
viewer.container.appendChild(nameOverlay);
nameOverlay.className = 'backdrop';
nameOverlay.style.display = 'none';
nameOverlay.style.position = 'absolute';
nameOverlay.style.bottom = '0';
nameOverlay.style.left = '0';
nameOverlay.style['pointer-events'] = 'none';
nameOverlay.style.padding = '4px';
nameOverlay.style.backgroundColor = 'black';
nameOverlay.style.fontFamily = 'Fira Sans, sans-serif';
nameOverlay.style.whiteSpace = 'pre-wrap';

//Selecting a Building
var Pickers_3DTile_Activated = true;
// Information about the currently highlighted feature
function active3DTilePicker() {
    var highlighted = {
        feature: undefined,
        originalColor: new Cesium.Color()
    };
    // Information about the currently selected feature
    var selected = {
        feature: undefined,
        originalColor: new Cesium.Color()
    };

    // An entity object which will hold info about the currently selected feature for infobox display
    var selectedEntity = new Cesium.Entity();

    // Get default left click handler for when a feature is not picked on left click
    var clickHandler = viewer.screenSpaceEventHandler.getInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
    // Color a feature yellow on hover.
    viewer.screenSpaceEventHandler.setInputAction(function onMouseMove(movement) {
        if (Pickers_3DTile_Activated) {
            // If a feature was previously highlighted, undo the highlight
            if (Cesium.defined(highlighted.feature)) {
                highlighted.feature.color = highlighted.originalColor;
                highlighted.feature = undefined;
            }
            // Pick a new feature
            var picked3DtileFeature = viewer.scene.pick(movement.endPosition);
            if (!Cesium.defined(picked3DtileFeature)) {
                nameOverlay.style.display = 'none';
                return;
            }
            // A feature was picked, so show it's overlay content
            nameOverlay.style.display = 'block';
            nameOverlay.style.bottom = viewer.canvas.clientHeight - movement.endPosition.y + 'px';
            nameOverlay.style.left = movement.endPosition.x + 'px';
            var name = picked3DtileFeature.getProperty('PolygonID');
			var name2 = picked3DtileFeature.getProperty('Irr_kWh');
			if (picked3DtileFeature.getProperty('Feature') === "Surface") {

            if (!Cesium.defined(name)) {
                name = picked3DtileFeature.getProperty('PolygonID');
            }
            nameOverlay.textContent = name + '\n' +  name2 + ' ' + 'kWh/(m².a)'; }
			
			else
			{
				if (!Cesium.defined(name)) {
                name = picked3DtileFeature.getProperty('Type');
            }
            nameOverlay.textContent = name ; }
				
            // Highlight the feature if it's not already selected.
            if (picked3DtileFeature !== selected.feature) {
                highlighted.feature = picked3DtileFeature;
                Cesium.Color.clone(picked3DtileFeature.color, highlighted.originalColor);
                picked3DtileFeature.color = Cesium.Color.GREY;
            }
        }
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

    // Color a feature on selection and show metadata in the InfoBox.
    viewer.screenSpaceEventHandler.setInputAction(function onLeftClick(movement) {

        if (Pickers_3DTile_Activated) {
            // If a feature was previously selected, undo the highlight
            if (Cesium.defined(selected.feature)) {
                selected.feature.color = selected.originalColor;
                selected.feature = undefined;
                var options = null;
            }
            // Pick a new feature
            var picked3DtileFeature = viewer.scene.pick(movement.position);
            if (!Cesium.defined(picked3DtileFeature)) {
                clickHandler(movement);
                return;
            }
            // Select the feature if it's not already selected
            if (selected.feature === picked3DtileFeature) {
                return;
            }
            selected.feature = picked3DtileFeature;
            // Save the selected feature's original color
            if (picked3DtileFeature === highlighted.feature) {
                Cesium.Color.clone(highlighted.originalColor, selected.originalColor);
                highlighted.feature = undefined;
            } else {
                Cesium.Color.clone(picked3DtileFeature.color, selected.originalColor);
            }
            // Highlight newly selected feature
            picked3DtileFeature.color = Cesium.Color.AQUA;
            // Set feature infobox description
            var featureName = "Building Information";
            selectedEntity.name = featureName;
            selectedEntity.description = 'Loading <div class="cesium-infoBox-loading"></div>';
			if (picked3DtileFeature.getProperty('Feature') === "Surface") {
            viewer.selectedEntity = selectedEntity;
            selectedEntity.description =
                '<table class="cesium-infoBox-defaultTable"><tbody>' +
                '<tr><th>Surface ID</th><td>' + picked3DtileFeature.getProperty('PolygonID') + '</td></tr>' +
				'<tr><th>Building ID</th><td>' + picked3DtileFeature.getProperty('bldgID') + '</td></tr>' +
				'<tr><th>Surface Area (m²)</th><td>' + picked3DtileFeature.getProperty('Area') + '</td></tr>' +
		        '<tr><th>Irradiance (kWh/(m².a))</th><td>' + picked3DtileFeature.getProperty('Irr_kWh') + '</td></tr>' +
               	'</tbody></table>';

 
                                
        }
		
		else 
			{
            viewer.selectedEntity = selectedEntity;
            selectedEntity.description =
                '<table class="cesium-infoBox-defaultTable"><tbody>' +
                '<tr><th>Building ID</th><td>' + picked3DtileFeature.getProperty('gml_id') + '</td></tr>' +
				'<tr><th>Building Parent ID</th><td>' + picked3DtileFeature.getProperty('gml_parent_id') + '</td></tr>' +
				'<tr><th>Height (m)</th><td>' + picked3DtileFeature.getProperty('citygml_measured_height') + '</td></tr>' +
			   	'</tbody></table>';

 
                                
        }
		}
			
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
}

active3DTilePicker();
