google.load("earth", "1");
//google.load("maps", "2.99");  // For JS geocoder

var ge = null;
//var geocoder;
//var url = 'http://experimental-chris.arkive.org/Earth/ARKive Ocean Species/oceans.kml';


function initEarth() {			
	//geocoder = new GClientGeocoder();
	google.earth.createInstance("map3d", initCB, failureCB);
}


function initCB(object) {
			
	li=new Image();
	li.src="/Earth/log____google_plugin_yes.gif";
			
	if (Prototype.Browser.IE6 && speciesKMLFileName === "")
	{
		$('map3d_container').insert({ 'after': "<div style='color: white;'><br/>The Google Earth plugin works best with newer web browsers, such as <a href='http://getfirefox.com'>Firefox</a> or Internet Explorer 8/9, so you may want to upgrade your browser to increase the usability of this feature.</div>"});
	}
			
	ge = object;
		
	//alert("c"); // if you get rid of this then alerts in "finished" function cause 100% CPU loop in IE6.
      
    var options = ge.getOptions();
    options.setFlyToSpeed (0.3);    
    var navControl = ge.getNavigationControl();
    navControl.setVisibility(ge.VISIBILITY_AUTO);
	ge.getWindow().setVisibility(true);
    
	//var url = 'http://develop.arkive.org/EarthAPIExample/GoogleGALayer.kmz';
	//var url = 'http://www.arkive.org/Earth/ARKive Ocean Species/ocean.kml';
	//var url = 'http://experimental-chris.arkive.org/Earth/ARKive Ocean Species/ocean.kml';
	var url;
		
	if (speciesKMLFileName !== "")
		url = "http://www.arkive.org/Earth/species-kml/" + speciesKMLFileName + ".kml";
	else
		url = 'http://www.arkive.org/Earth/ARKive Ocean Species/oceans.kml';
		//alert(url);
	google.earth.fetchKml(ge, url, finished);

}

		
function openVideoBalloon(currentPlaceMark) {

	var b = ge.createHtmlDivBalloon('');
	b.setMaxWidth(800);
	b.setFeature(currentPlaceMark);
	b.setBackgroundColor('#051d43');
	ge.parseKml(currentPlaceMark.getKml())
	var div = document.createElement('div');
			
	div.innerHTML = currentPlaceMark.getBalloonText_().replace("xx wmode","embed wmode");
	b.setContentDiv(div);
	ge.setBalloon(b);

} 


function failureCB(object) {
//alert('An error occurred while loading the plugin. Google may be having technical difficulties. Please try again soon.');
		
	i=new Image();
	li.src="/Earth/log____google_plugin_no.gif";
			
	if (Prototype.Browser.IE6 && speciesKMLFileName === "")
	{
		$('map3d_container').insert({ 'after': "<div style='color: white;'><br/>Please follow the above instructions to install the Google Earth plugin. (You may need administrative privileges on your computer to install the plugin). The Google Earth plugin works best with newer web browsers, such as <a href='http://getfirefox.com'>Firefox</a> or Internet Explorer 7, so you may want to upgrade your browser to increase the usability of this feature.</div>"});
	} else if (speciesKMLFileName === "")
	{
		$('map3d_container').insert({ 'after': "<div style='color: white;'><br/>Please follow the above instructions to install the Google Earth plugin. (You may need administrative privileges on your computer to install the plugin).</div>"});
	} else
	{
		// not doing this - probably just an un-necessary interruption
		//alert('You need to install the Google Earth plugin - instructions will be provided. You may need administrative priveledges on your computer to install the plugin.');
	}
		
}
		

// this is called by the geoLocation API if user submits their location data to us
function moveMap(pos)
{
	var answer = confirm ("Thank you for telling ARKive where you are. Click OK if you would like to see endangered species in your region.")
	if (answer)
	{
		var la = ge.createLookAt('');
			
		la.set(pos.latitude, pos.longitude, 0, ge.ALTITUDE_RELATIVE_TO_GROUND,
		0, 57, 963188);
			
		ge.getView().setAbstractView(la);
	}
	//alert( pos.latitude + ", " + pos.longitude );
			
}


/*
useful for debugging, etc....
		
function traverseKml(node) {
	alert(node.getType() + '; ' + node.getName());
	if(node.getFeatures().hasChildNodes()) {
	var subNodes = node.getFeatures().getChildNodes();
	var length = subNodes.getLength();
	for(var i = 0; i < length; i++) {
		var eachSubNode = subNodes.item(i);
		var nodeType = eachSubNode.getType();
		alert(nodeType);
		var nodeName = eachSubNode.getName();
		alert(nodeName);
		//var nodeID = eachSubNode.getID();
		//alert(nodeID);
		switch(nodeType) {
		case 'KmlFolder' :
		    traverseKml(eachSubNode);
		break;
		case 'KmlPlacemark' :
		    traverseKml(eachSubNode);
		break;
		default :
		    alert('unknown:' + nodeType);
		break;
		}
	}
	}
} 
*/


function finished(object) {
		
	//alert(ge.getPluginVersion());
	//alert(ge.getApiVersion());
		
	if (!object) {
	    alert('bad or NULL kml');
	    return;
	}

	ge.getFeatures().appendChild(object);

	// over-ride the default balloon handling
	google.earth.addEventListener(object, "click", function(event) {
		event.stopPropagation();
		event.preventDefault();
		openVideoBalloon(event.getTarget());
	}); 

	// earth API reference note:
	// void set(double latitude, double longitude, double altitude,
	//         KmlAltitudeModeEnum altitudeMode,
	//         double heading, double tilt, double range);

	// This is experimental - currently only works with Firefox 3.1 beta and Geolocation
	// TODO: T and C update required, etc.? maybe need to comment out these lines before initial launch
	//if (speciesKMLFileName == "" && navigator != null)
	//	if (navigator.geolocation != null)
	//		navigator.geolocation.getCurrentPosition(moveMap);

	if (speciesKMLFileName !== "")
	{
		var kmlav = object.getFeatures().getFirstChild().getAbstractView();
		kmlav.setAltitude(10000);
		ge.getView().setAbstractView(kmlav);
	} else
	{
		var la = ge.createLookAt('');
		la.set(20, -110, 0, ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 0, 11963188);
		ge.getView().setAbstractView(la);
	}

}


//initEarth();
