var googleEarthObj;
googleEarthObj = {

	ge: null,

	addEvent: function (el, evType, handle) { 
		if(el.addEventListener) {
			el.addEventListener(evType, handle, false); 
		}
		else if (el.attachEvent) { 
			el["e" + evType + handle] = handle; 
			el[evType + handle] = function () {
				el["e" + evType + handle](window.event);
			} 
			el.attachEvent("on" + evType, el[evType + handle]); 
		}
	},


	loaded: false,
	loadPlugin: function () {
		if (googleEarthObj.loaded !== false) {
			return;
		}
		google.load("earth", "1");
		googleEarthObj.loaded = true;
	},

	fetchKmlUrl: "",
	kmlLat: 56.203234,
	kmlLng: 10.287393,
	kmlAlt: 2930,
	init: function (elemId, kmlUrl) {
		googleEarthObj.fetchKmlUrl = kmlUrl;
		google.earth.createInstance(elemId, googleEarthObj.initCallback, googleEarthObj.failureCallback);
	},

	initCallback: function (pluginInstance) {
		googleEarthObj.ge = pluginInstance;
		//alert("Google Earth plugin loaded "+ googleEarthObj.getVersion());
		googleEarthObj.ge.getWindow().setVisibility(true);
		// Add a navigation control
		googleEarthObj.ge.getNavigationControl().setVisibility(googleEarthObj.ge.VISIBILITY_AUTO);
		// Add some layers
		googleEarthObj.ge.getLayerRoot().enableLayerById(googleEarthObj.ge.LAYER_BORDERS, true);
		googleEarthObj.ge.getLayerRoot().enableLayerById(googleEarthObj.ge.LAYER_ROADS, true);
		//alert(googleEarthObj.fetchKmlUrl);
		if (googleEarthObj.fetchKmlUrl !== "") {
			googleEarthObj.fetchKml(googleEarthObj.fetchKmlUrl);
		}
	},

	getVersion: function () {
		if (googleEarthObj.ge !== null) {
			return googleEarthObj.ge.getPluginVersion().toString();
		}
	},

	failureCallback: function (errorCode) {
		//alert("Indlęsning fejlede: " + errorCode);
	},

	setView: function (lat, lng, alt) {
		googleEarthObj.kmlLat = lat;
		googleEarthObj.kmlLng = lng;
		googleEarthObj.kmlAlt = alt;
	},

	fetchKml: function (kmlUrl) {
		google.earth.fetchKml(googleEarthObj.ge, kmlUrl, googleEarthObj.finishFetchKmlCallback);
	},

	finishFetchKmlCallback: function (kmlObject) {
		if (kmlObject) {
			currentKmlObject = kmlObject;
			googleEarthObj.ge.getFeatures().appendChild(currentKmlObject);
		}
		var la = googleEarthObj.ge.createLookAt('');
		la.set(googleEarthObj.kmlLat, googleEarthObj.kmlLng, googleEarthObj.kmlAlt, googleEarthObj.ge.ALTITUDE_RELATIVE_TO_GROUND, 1, 30, 190);
		googleEarthObj.ge.getView().setAbstractView(la);

	}

};
