var ge;
var tour;
var fullScreenState = false;
var noFullScreenIcon = null;
var fullScreenIcon = null;
var T3DLoadWidth = '100px';
var T3DLoadHeight = '73px';
var defaultText = "<h1>Informatie over appartement:<\/h1> Geen appartement aangeklikt";
var arrayPrijs = new Array('€130.000 v.o.n.','€150.000 v.o.n.');
var arrayWoonOpp = new Array('80 m²','95 m²');
var arrayTerrasOpp = new Array('10 m²','9 m²'); 
var arrayPlattegrond = new Array('ja','nee');

var arrayURL = window.location.pathname.split( '/' );

var arrayLengte = arrayURL.length;
var NummerInArray = arrayLengte-1;
var paginaNaam = arrayURL[NummerInArray];

if (paginaNaam != 'fullscreen.php') {
	var T3DLoadPosY = '510px';
	var T3DLoadPosX = '535px';
}
else {
	var T3DLoadPosY = '46%';
	var T3DLoadPosX = '53%'
}

// store the object loaded for the given file... initially none of the objects
// are loaded, so initialize these to null
var currentKmlObjects = {
	'1': null,
	'2': null,
	'3': null,
	'4': null,
	'5': null,
	'6': null,
	'7': null
};

google.load("earth", "1");

function init() {
	google.earth.createInstance('map3d', initCallback, failureCallback);
}    

function initCallback(instance) {
	ge = instance;
	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_TERRAIN, false);
	
	if (paginaNaam != 'fullscreen.php') {
		createFullScreenIcon();
	}
	
	google.earth.addEventListener(ge.getWindow(), "click", handleMouseClick);
	
	var screenOverlay = ge.createScreenOverlay('');
	
	// Set the visibility to false while we construct the overlay.
	screenOverlay.setVisibility(false);

	var icon = ge.createIcon('');
	icon.setHref('http://www.team3d.nl/bonhof/images/powered.png');  // Loads an gilded picture frame.
	screenOverlay.setIcon(icon);

	// Position the overlay.  ScreenXY(0,0) is mapped to OverlayXY(0,0)
	var screenXY = screenOverlay.getScreenXY();
	screenXY.setXUnits(ge.UNITS_FRACTION);
	screenXY.setYUnits(ge.UNITS_FRACTION);
	screenXY.setX(1);
	screenXY.setY(1);

	var overlayXY = screenOverlay.getOverlayXY();
	overlayXY.setXUnits(ge.UNITS_FRACTION);
	overlayXY.setYUnits(ge.UNITS_FRACTION);
	overlayXY.setX(0.99);
	overlayXY.setY(0.15);

	// Set object's size in fractions of the 3d view window.  By setting
	// to (1, 1), this image will cover the entire Earth window.
	var overlaySize = screenOverlay.getSize()
	overlaySize.setXUnits(ge.UNITS_PIXELS);
	overlaySize.setYUnits(ge.UNITS_PIXELS);
	overlaySize.setX(108);
	overlaySize.setY(20);

	screenOverlay.setVisibility(true);

	ge.getFeatures().appendChild(screenOverlay);

		
	// fly to Santa Cruz
	var la = ge.createLookAt('');
	la.set(52.22129835471309,6.076453787351133,21.48647715007233,ge.ALTITUDE_RELATIVE_TO_GROUND,-16.66037705614187,77.99889122461543,0);
	//<latitude>,<longitude>,<altitude>,<ge.ALTITUDE_RELATIVE_TO_GROUND>,<heading>,<straight-down tilt>,<range (inverse of zoom>		
																																																							
	ge.getView().setAbstractView(la);
	
	var StillLoading = 0;
	var FinishedLoading = 0; 
	
	setInterval(function() {
		var streamingPercent = ge.getStreamingPercent();
			if (streamingPercent == 100) {
				if (FinishedLoading == 0) {
				var T3DL = document.getElementById('T3Dloadbar');
				T3DL.style.visibility='hidden';
				var T3DLoad = document.getElementById('T3DLoad');
				document.body.removeChild(T3DLoad);
				 FinishedLoading = 1;
				}
			} else {
				if (StillLoading == 0) {
				var T3DLoad = document.createElement('iframe');
				T3DLoad.id='T3DLoad';
				T3DLoad.style.position='absolute';
				T3DLoad.style.width=T3DLoadWidth;
				T3DLoad.style.height=T3DLoadHeight;
				T3DLoad.style.top=T3DLoadPosY;
				T3DLoad.style.left=T3DLoadPosX;
				T3DLoad.style.zIndex='999';
				T3DLoad.setAttribute('frameborder','0');
//				T3DLoad.setAttribute('src','javascript:false;');
				document.body.appendChild(T3DLoad);
				var T3DL = document.getElementById('T3Dloadbar');
				T3DL.style.visibility='visible';
					StillLoading = 1;
					FinishedLoading = 0;
				}
			}
		}, 500); 

	// if the page loaded with checkboxes checked, load the appropriate
	// KML files
	loadKml('1');
		
	if (document.getElementById('kml-4-check').checked) {
		loadKml('4');
	}
}

function failureCallback(errorCode) {
}

function toggleKml(file) {
	// remove the old KML object if it exists
	if (currentKmlObjects[file]) {
		ge.getFeatures().removeChild(currentKmlObjects[file]);
		currentKmlObject = null;
		
		if (file == 4) {
			document.getElementById('kml-5-check').disabled = true;
			document.getElementById('kml-6-check').disabled = true;
		}
	}

	// if the checkbox is checked, fetch the KML and show it on Earth
	var kmlCheckbox = document.getElementById('kml-' + file + '-check');
	if (kmlCheckbox.checked) {
		if (file == 4) {
			document.getElementById('kml-5-check').disabled = false;
			document.getElementById('kml-6-check').disabled = false;
		}
		loadKml(file);
	}
}

function loadKml(file) {
	var kmlUrl = 'http://www.team3d.nl/bonhof/model'+ file +'/doc.kml';    
	// fetch the KML
	
	if (file == 4) {
		document.getElementById('kml-5-check').disabled = false;
		document.getElementById('kml-6-check').disabled = false;
	}
	
	google.earth.fetchKml(ge, kmlUrl, function(kmlObject) {
		// NOTE: we still have access to the 'file' variable (via JS closures)

		if (kmlObject) {
			currentKmlObjects[file] = kmlObject;
			ge.getFeatures().appendChild(kmlObject);
			
			walkKmlDom(kmlObject, function() {
				if (this.getType() == 'KmlTour') {
					 tour = this;
					 ge.getTourPlayer().setTour(tour); 
					 ge.getTourPlayer().play();
					 return false;
				}
		 });

		} else {
			currentKmlObjects[file] = null;
			
			setTimeout(function() {
				alert('Geen model gevonden.');
			}, 0);

			document.getElementById('kml-' + file + '-check').checked = '';
		}
		
		var StillLoading = 0;
		var FinishedLoading = 0;
		setInterval(function() {
		var streamingPercent = ge.getStreamingPercent();
			if (streamingPercent == 100) {
				if (FinishedLoading == 0 && laden == 'ja') {
				var T3DL = document.getElementById('T3Dloadbar');
				T3DL.style.visibility='hidden';
				var T3DLoad = document.getElementById('T3DLoad');
				document.body.removeChild(T3DLoad);
					FinishedLoading = 1;
				}
			} else {
				if (StillLoading == 0 && laden == 'ja') {
					var T3DLoad = document.createElement('iframe');
					T3DLoad.id='T3DLoad';
					T3DLoad.style.position='absolute';
					T3DLoad.style.width=T3DLoadWidth;
					T3DLoad.style.height=T3DLoadHeight;
					T3DLoad.style.top=T3DLoadPosY;
					T3DLoad.style.left=T3DLoadPosX;
					T3DLoad.style.zIndex='999';
					T3DLoad.setAttribute('frameborder','0');
					document.body.appendChild(T3DLoad);
					var T3DL = document.getElementById('T3Dloadbar');
					T3DL.style.visibility='visible';
					StillLoading = 1;
					FinishedLoading = 0;							
				}
			}
		}, 500);
		
	});
}
	
function FlyToOrigin() {
		var la = ge.createLookAt('');
		la.set(52.22129835471309,6.076453787351133,21.48647715007233,ge.ALTITUDE_RELATIVE_TO_GROUND,-16.66037705614187,77.99889122461543,0);
		ge.getView().setAbstractView(la);
}


function handleMouseClick(event){
	var INSET_PIXELS_X = document.getElementById("map3d").offsetWidth - event.getClientX();
	var INSET_PIXELS_Y = event.getClientY();
	if (INSET_PIXELS_X < 32){
	if (INSET_PIXELS_Y < 32){ makeFullScreen();}
	}
}

function makeFullScreen(){
	window.open('fullscreen.php');
}

function createFullScreenIcon() {
	var icon = ge.createIcon('');
	icon.setHref('http://www.team3d.nl/test/bramhaar/images/ToFullscreen.png');
	// Screenoverlay maken
	fullScreenIcon = ge.createScreenOverlay('');
	fullScreenIcon.setDrawOrder(60);
	fullScreenIcon.setIcon(icon);
	// Linkerbovenhoek van icoon als referentie gebruiken
	fullScreenIcon.getScreenXY().setXUnits(ge.UNITS_FRACTION);
	fullScreenIcon.getScreenXY().setYUnits(ge.UNITS_FRACTION);
	fullScreenIcon.getScreenXY().setX(1);
	fullScreenIcon.getScreenXY().setY(1);
	// Plaats icoontje in rechterbovenhoek
	fullScreenIcon.getOverlayXY().setXUnits(ge.UNITS_INSET_PIXELS);
	fullScreenIcon.getOverlayXY().setYUnits(ge.UNITS_INSET_PIXELS);
	fullScreenIcon.getOverlayXY().setX(2);
	fullScreenIcon.getOverlayXY().setY(4);
	// Grootte van icoontje
	fullScreenIcon.getSize().setXUnits(ge.UNITS_PIXELS);
	fullScreenIcon.getSize().setYUnits(ge.UNITS_PIXELS);
	fullScreenIcon.getSize().setY(32);
	fullScreenIcon.getSize().setX(32);
	// Voeg icoontje toe aan GE
	ge.getFeatures().appendChild(fullScreenIcon);
} 
