
/************************************/
/********** GOOGLE EARTH ************/
/************************************/

var GOOGLEEARTHS = new Array();

function GoogleEarth(id, kmlfile){
	this.id = id;
	this.kmlfile = kmlfile;
	GOOGLEEARTHS[0] = this;
}

function LoadGoogleEarths(){
	var numOfEarths = GOOGLEEARTHS.length;
	if(numOfEarths > 0){
		//var i;
		//for(i=0; i<numOfEarths; i++){
			var aEarth = getObj(GOOGLEEARTHS[0].id);
			if(aEarth){
				google.earth.createInstance(GOOGLEEARTHS[0].id, initCallback, failureCallback);
			}
		//}
	}
}

var ge = null;

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);

	// in this sample we will attempt
	// to fetch a  KML file and show it

	function finished(object) {
		if (!object) {
		// 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(object);
		
		/*var la = ge.createLookAt('');
		la.set(53.914598, -122.762604, 25, ge.ALTITUDE_RELATIVE_TO_GROUND, 180, 60, 500);
		ge.getView().setAbstractView(la);*/
	}

	// fetch the KML
	var url = GOOGLEEARTHS[0].kmlfile;
	if(url != ""){
		//var url = 'http://sketchup.google.com/' +
		//        '3dwarehouse/download?mid=28b27372e2016cca82bddec656c63017&rtyp=k2';

		//alert(url);
		//google.earth.fetchKml(ge, url, finished);
		//alert(2);
		//document.getElementById('installed-plugin-version').innerHTML = ge.getPluginVersion().toString();
		//alert(url);
		var link = ge.createLink('');
		//var href = 'http://code.google.com/' + 'apis/earth/documentation/samples/kml_example.kml'
		link.setHref(url);

		var networkLink = ge.createNetworkLink('');
		networkLink.set(link, true, true); // Sets the link, refreshVisibility, and flyToView

		ge.getFeatures().appendChild(networkLink);
		
		
	}
}

function failureCallback(errorCode) {
	//alert(errorCode);
}

addSafeEventListener("load", LoadGoogleEarths);
