google.load("earth", "1");
var ge = null;
var emptyUrl = sPath + 'empty.kml';
// store the object loaded for the given file... initially none of the objects
// are loaded, so initialize these to null
var currentKmlObjects = {
	'SORAD_1A' : null,
	'SORAD_1B' : null,
	'SORAD_1A_PUB' : null,
	'SORAD_1B_PUB' : null,
	'GROUND_OVRLY_DEMO' : null,
	'PIC_OVRLY_DEMO' : null,
	'pm101' : null,
	'pm102' : null
};

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

function initCallback(object) {
	ge = object;
	ge.getWindow().setVisibility(true);
	// add some layers
	ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
	ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);
	updateOptions(); // adds the regular options
	// alert("creates the initial camera view")
	var la = ge.createLookAt('');
	// la.set(28,-80,1000, ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 0, 180000);
	// //orlando
	la.set(44, -95, 0, ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 0, 12000000);// middle
	// of us
	ge.getView().setAbstractView(la);
	// var eUrl = sPath + 'empty.kml';
	loadKml(emptyUrl, 'SORAD_1A');
	loadKml(emptyUrl, 'SORAD_1B');
	loadKml(emptyUrl, 'SORAD_1A_PUB');
	loadKml(emptyUrl, 'SORAD_1B_PUB');
}// end of initCallback
// *********************************************************************

function loadKml(kmlUrl, option) {
	// fetch the KML
	google.earth.fetchKml(ge, kmlUrl, function(kmlObject) {
		// NOTE: we still have access to the 'file' variable (via JS closures)
		if (kmlObject) {
			// show it on Earth
			currentKmlObjects[option] = kmlObject;
			ge.getFeatures().appendChild(kmlObject);
		} else {
			// bad KML
			currentKmlObjects[option] = null;
			// 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);
		}
	});// end function fetch the kml
}// end function loadkml file

// ****************************************************************************
// This function stops the display of all personal cam alerts
function Stop_Personal_Cam_Alerts() {
	var networkLink = ge.createNetworkLink("");
	networkLink.setDescription("NetworkLink open to fetched content");
	networkLink.setName("Open NetworkLink");
	// networkLink.setFlyToView(true);
	var link = ge.createLink("");
	// if the checkbox for the triggers is checked uncheck it and stop the
	// triggers
	var kmlCheckbox = document.getElementById('kml-SORAD_1A-check');
	if (kmlCheckbox.checked) {
		
		link.setHref(emptyUrl);
		networkLink.setLink(link);
		//alert("Triggers End");
		ge.getFeatures().replaceChild(networkLink,
				currentKmlObjects['SORAD_1A']);
		currentKmlObjects['SORAD_1A'] = networkLink;// save the child to a
		// variable you can access
		// later
		document.getElementById('kml-SORAD_1A-check').checked = '';
		//alert("check box not checked");
	}
}
//This function stops the display of all public cam alerts
function Stop_Public_Cam_Alerts() {
	var networkLink = ge.createNetworkLink("");
	networkLink.setDescription("NetworkLink open to fetched content");
	networkLink.setName("Open NetworkLink");
	// networkLink.setFlyToView(true);
	var link = ge.createLink("");
	// if the checkbox for the triggers is checked uncheck it and stop the
	// triggers
	var kmlCheckbox = document.getElementById('kml-SORAD_1A_PUB-check');
	if (kmlCheckbox.checked) {
		
		link.setHref(emptyUrl);
		networkLink.setLink(link);
		//alert("Public Triggers End");
		ge.getFeatures().replaceChild(networkLink,
				currentKmlObjects['SORAD_1A_PUB']);
		currentKmlObjects['SORAD_1A_PUB'] = networkLink;// save the child to a
		// variable you can access
		// later
		document.getElementById('kml-SORAD_1A_PUB-check').checked = '';
		//alert("public check box not checked");
	}
}

// *******************************************************************************
// **This function activates and deactivates the update of personal triggered cams 
function SORAD_1A() {
	var On_Url = sPath + uName + '/DBcams_trigger.kml';
	var Off_Url = sPath + uName + '/DemoTriggerCams.kml';
	var networkLink = ge.createNetworkLink("");
	networkLink.setDescription("NetworkLink open to fetched content");
	networkLink.setName("Open NetworkLink");
	// networkLink.setFlyToView(true);
	var link = ge.createLink("");
	// if the checkbox is checked, fetch the KML and show it on Earth
	var kmlCheckbox = document.getElementById('kml-SORAD_1A-check');
	if (!kmlCheckbox.checked) {
		// alert("check box not checked");
				// Turn off the refreshing of the triggers
				// so that user can click on icon
				// The first time this function actually sets
				// up the the default network link
		link.setHref(Off_Url);
		networkLink.setLink(link);
		alert("Personal Alerts Off");
		ge.getFeatures().replaceChild(networkLink,
				currentKmlObjects['SORAD_1A']);
		currentKmlObjects['SORAD_1A'] = networkLink;// save the child to a
													// variable you can access
													// later
	} else {
		// alert("Welcome debugger:\n\nLet us get started");
		//Stop the public cam alerts
		Stop_Function('SORAD_1A_PUB');
		// Set up the refreshing trigger
		link.setHref(On_Url);
		networkLink.setLink(link);
		ge.getFeatures().replaceChild(networkLink,
				currentKmlObjects['SORAD_1A']);
		currentKmlObjects['SORAD_1A'] = networkLink;// save the child to a
		// variable you can access
		// later
		alert("Personal Alerts On");
	}// end else statement
}// end of SORAD_1A
// *******************************************************************************

// **This function activates and deactivates the update of public triggered cams
function SORAD_1A_PUB() {
	var pub_On_Url = sPath + 'public/DBcams_trigger.kml';
	var pub_Off_Url = sPath + 'public/DemoTriggerCams.kml';
	var networkLink = ge.createNetworkLink("");
	networkLink.setDescription("NetworkLink open to fetched content");
	networkLink.setName("Open NetworkLink");
	// networkLink.setFlyToView(true);
	var link = ge.createLink("");
	// if the checkbox is checked, fetch the KML and show it on Earth
	var kmlCheckbox = document.getElementById('kml-SORAD_1A_PUB-check');
	if (!kmlCheckbox.checked) {
		// alert("check box not checked");
		// Turn off the refreshing of the triggers so that user can click on
		// icon
		// The first time this function actually sets up the the default network
		// link
		link.setHref(pub_Off_Url);
		networkLink.setLink(link);
		alert("Public Motion Alerts Off");
		ge.getFeatures().replaceChild(networkLink,
				currentKmlObjects['SORAD_1A_PUB']);
		currentKmlObjects['SORAD_1A_PUB'] = networkLink;// save the child to a
														// variable you can
														// access later
	} else {
		// alert("Welcome debugger:\n\nLet us get started");
		//Stop the personal cam alerts
		Stop_Function('SORAD_1A');
		// Set up the refreshing trigger
		link.setHref(pub_On_Url);
		networkLink.setLink(link);
		ge.getFeatures().replaceChild(networkLink,
				currentKmlObjects['SORAD_1A_PUB']);
		currentKmlObjects['SORAD_1A_PUB'] = networkLink;// save the child to a
		// variable you can
		// access later
		alert("Public Motion Alerts On");
	}// end else statement
}// end of SORAD_1A_PUB
// *******************************************************************************

// This function activates and de-activates the screen overlay features
function SORAD_1B() {
	var On_Url = sPath + uName + '/DemoTriggerCams_ScreenOverlay.kml';
	var Off_Url = emptyUrl;
	var networkLink = ge.createNetworkLink("");
	networkLink.setDescription("NetworkLink open to fetched content");
	networkLink.setName("Open NetworkLink");
	// networkLink.setFlyToView(true);
	var link = ge.createLink("");
	// if the checkbox is checked, fetch the KML and show it on Earth
	var kmlCheckbox = document.getElementById('kml-SORAD_1B-check');
	if (!kmlCheckbox.checked) {
		// alert("check box not checked");
				// Turn off the refreshing of the triggers
				// so that user can click on icons
				// The first time this function actually
				// sets up the the default networklink
		link.setHref(Off_Url);
		networkLink.setLink(link);
		// alert("Overlay Off");
		ge.getFeatures().replaceChild(networkLink,
				currentKmlObjects['SORAD_1B']);
		currentKmlObjects['SORAD_1B'] = networkLink;// save the child to a
													// variable you can access
													// later
	} else {
		// alert("Welcome debugger:\n\nLet us get started");
		// Set up the repeating motion alert
		link.setHref(On_Url);
		networkLink.setLink(link);
		ge.getFeatures().replaceChild(networkLink,
				currentKmlObjects['SORAD_1B']);
		currentKmlObjects['SORAD_1B'] = networkLink;// save the child to a
													// variable you can access
													// later
		// alert("Overlay On");
	}// end else statement
}// end of SORAD_1B
// **********************************************************************************
//This function activates and de-activates the public screen overlay features
function SORAD_1B_PUB() {
	var pub_On_Url = sPath + 'public/DemoTriggerCams_ScreenOverlay.kml';
	var pub_Off_Url = emptyUrl;
	var networkLink = ge.createNetworkLink("");
	networkLink.setDescription("NetworkLink open to fetched content");
	networkLink.setName("Open NetworkLink");
	// networkLink.setFlyToView(true);
	var link = ge.createLink("");
	// if the checkbox is checked, fetch the KML and show it on Earth
	var kmlCheckbox = document.getElementById('kml-SORAD_1B_PUB-check');
	if (!kmlCheckbox.checked) {
		// alert("check box not checked");
		// Turn off the refreshing of the triggers so that user can click on
		// icons
		// The first time this function actually sets up the the default network
		// link
		link.setHref(pub_Off_Url);
		networkLink.setLink(link);
		// alert("Overlay Off");
		ge.getFeatures().replaceChild(networkLink,
				currentKmlObjects['SORAD_1B_PUB']);
		currentKmlObjects['SORAD_1B_PUB'] = networkLink;// save the child to a
		// variable you can
		// access later
	} else {
		// alert("Welcome debugger:\n\nLet us get started");
		// Set up the refreshing trigger
		link.setHref(pub_On_Url);
		networkLink.setLink(link);
		ge.getFeatures().replaceChild(networkLink,
				currentKmlObjects['SORAD_1B_PUB']);
		currentKmlObjects['SORAD_1B_PUB'] = networkLink;// save the child to a
		// variable you can
		// access later
		// alert("Overlay On");
	}// end else statement
}

//This function displays the ground overlay tracking demo
function GROUND_OVRLY_DEMO() {
	// if the checkbox is checked, fetch the KML and show it on Earth, but if not then make sure the effects are taken off
	var kmlCheckbox = document.getElementById('kml-GROUND_OVRLY_DEMO-check');
	if (!kmlCheckbox.checked) {
		//alert("check box not checked");
		//kill the network link and the placemark;
		ge.getFeatures().removeChild(currentKmlObjects['GROUND_OVRLY_DEMO']);
		currentKmlObjects['GROUND_OVRLY_DEMO'] = null;
		ge.getFeatures().removeChild(currentKmlObjects['pm101']);
		currentKmlObjects['pm101'] = null;
	} else {
		//alert("Welcome Lui:\n\nLet us get started");
		var la = ge.createLookAt('');
		la.set(64.84615117684812, -147.7229993574787, 1000,
				ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 0, 400);
		ge.getView().setAbstractView(la);

		//alert("Ready to create placemark");

		var placemark = ge.createPlacemark('');
		placemark.setName("Click Me");
		ge.getFeatures().appendChild(placemark);
		currentKmlObjects['pm101'] = placemark;

		// Create style map for placemark
		var normal = ge.createIcon('');
		normal.setHref('http://maps.google.com/mapfiles/kml/paddle/red-circle.png');
		var iconNormal = ge.createStyle('');
		iconNormal.getIconStyle().setIcon(normal);
		var highlight = ge.createIcon('');
		highlight.setHref('http://maps.google.com/mapfiles/kml/paddle/red-circle.png');
		var iconHighlight = ge.createStyle('');
		iconHighlight.getIconStyle().setIcon(highlight);
		var styleMap = ge.createStyleMap('');
		styleMap.setNormalStyle(iconNormal);
		styleMap.setHighlightStyle(iconHighlight);
		placemark.setStyleSelector(styleMap);

		// Create point
		//var la = ge.createLookAt('');
		la.set(64.84615117684812, -147.7229993574787, 0,
				ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 0, 2000);
		ge.getView().setAbstractView(la);
		var point = ge.createPoint('');
		point.setLatitude(la.getLatitude());
		point.setLongitude(la.getLongitude());
		placemark.setGeometry(point);

		//alert("Now to add listener");
		//this is the listener event
		var networkLink = ge.createNetworkLink("");
		var On_Url = sPath + 'OverlayDemo1_1/FDNM.kmz';
		var link = ge.createLink("");
		networkLink.setDescription("NetworkLink open to fetched content");
		networkLink.setName("Open NetworkLink");
		networkLink.setFlyToView(true);
		ge.getFeatures().appendChild(networkLink);
		currentKmlObjects['GROUND_OVRLY_DEMO'] = networkLink;
		var off = 1;
		if (window.placemark) {
			//alert('so now there is a placemark?');
		} else {//listener
			window.geEventListener = function(event) {
				function overlayit() {

					link.setHref(On_Url);
					networkLink.setLink(link);
					//alert("did it" + off);
				}
				function killit() {
					link.setHref(emptyUrl);
					networkLink.setLink(link);
					//alert("kill it" + off);
				}

				if (off) {
					overlayit();//click on event
					off = 0;
				} else {
					killit();//click off event
					off = 1;
				}

				// Prevent default balloon from popping up for marker placemarks
				event.preventDefault();
			}

			google.earth.addEventListener(placemark, "click",
					window.geEventListener);

		}// end else

	}// end else
}// end of GROUND_OVRLY_DEMO

//This function displays the picture overlay tracking demo
function PIC_OVRLY_DEMO() {
	// if the checkbox is checked, fetch the KML and show it on Earth, but if not then make sure the effects are taken off
	var kmlCheckbox = document.getElementById('kml-PIC_OVRLY_DEMO-check');
	if (!kmlCheckbox.checked) {
		//alert("check box not checked");
		//kill the network link and the placemark;
		ge.getFeatures().removeChild(currentKmlObjects['PIC_OVRLY_DEMO']);
		currentKmlObjects['PIC_OVRLY_DEMO'] = null;
		ge.getFeatures().removeChild(currentKmlObjects['pm102']);
		currentKmlObjects['pm102'] = null;
	} else {
		//alert("Welcome Lui:\n\nLet us get started");
		var la = ge.createLookAt('');

		//alert("Ready to create placemark");
		var placemark = ge.createPlacemark('');
		placemark.setName("Please Click Me");
		ge.getFeatures().appendChild(placemark);
		currentKmlObjects['pm102'] = placemark;

		// Create style map for placemark
		var normal = ge.createIcon('');
		normal.setHref('http://maps.google.com/mapfiles/kml/paddle/blu-circle.png');
		var iconNormal = ge.createStyle('');
		iconNormal.getIconStyle().setIcon(normal);
		var highlight = ge.createIcon('');
		highlight.setHref('http://maps.google.com/mapfiles/kml/paddle/red-circle.png');
		var iconHighlight = ge.createStyle('');
		iconHighlight.getIconStyle().setIcon(highlight);
		var styleMap = ge.createStyleMap('');
		styleMap.setNormalStyle(iconNormal);
		styleMap.setHighlightStyle(iconHighlight);
		placemark.setStyleSelector(styleMap);

		// Create point
		//var la = ge.createLookAt('');
		la.set(38, -100.1, 0,
				ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 0, 3120000);
		ge.getView().setAbstractView(la);
		var point = ge.createPoint('');
		point.setLatitude(la.getLatitude());
		point.setLongitude(la.getLongitude());
		placemark.setGeometry(point);

		//alert("Now to add listener");
		//this is the listener event
		var networkLink = ge.createNetworkLink("");
		var On_Url = sPath + 'OverlayDemo1_1/WPOT.kmz';
		//var On_Url = sPath + 'OverlayDemo1_1/WPO.kml';
		//var On_Url = sPath + 'Demo2P/WPO.kmz';
		var link = ge.createLink("");
		networkLink.setDescription("NetworkLink open to fetched content");
		networkLink.setName("Open NetworkLink");
		networkLink.setFlyToView(true);
		ge.getFeatures().appendChild(networkLink);
		currentKmlObjects['PIC_OVRLY_DEMO'] = networkLink;
		var off = 1;
		if (window.placemark) {
			//alert('so now there is a placemark?');
		} else {//listener
			window.geEventListener = function(event) {
				function overlayit() {

					link.setHref(On_Url);
					networkLink.setLink(link);
					networkLink.setOpacity(0.7);
					ge.getLayerRoot().enableLayerById(ge.LAYER_BUILDINGS, true);
					//alert("did it" + off);
				}
				function killit() {
					link.setHref(emptyUrl);
					networkLink.setLink(link);
					ge.getLayerRoot().enableLayerById(ge.LAYER_BUILDINGS, false);
					//alert("kill it" + off);
				}

				if (off) {
					overlayit();//click on event
					off = 0;
				} else {
					killit();//click off event
					off = 1;
				}

				// Prevent default balloon from popping up for marker placemarks
				event.preventDefault();
			}

			google.earth.addEventListener(placemark, "click",
					window.geEventListener);

		}// end else

	}// end else
}// end of PIC_OVRLY_DEMO

//***********************************************************************************
function ZOOM_OUT_MAP() {// This function simply resets the view of google earth
	var la = ge.createLookAt('');
	// la.set(28,-80,1000, ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 0, 180000);
	// //orlando
	la.set(44, -95, 0, ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 0, 12000000);// middle
	// of us
	ge.getView().setAbstractView(la);
	document.getElementById('kml-ZOOM_OUT_MAP-check').checked = '';
}

function Stop_Function(ID) {//This function stops any SORAD function
	var networkLink = ge.createNetworkLink("");
	networkLink.setDescription("NetworkLink open to fetched content");
	networkLink.setName("Open NetworkLink");
	// networkLink.setFlyToView(true);
	var link = ge.createLink("");
	// if the checkbox for the triggers is checked uncheck it and stop the
	// triggers
	//var kmlCheckbox = document.getElementById('kml-' + ID + '-check');
	//if (kmlCheckbox.checked) {
		link.setHref(emptyUrl);
		networkLink.setLink(link);
		//alert("End Function");
		ge.getFeatures().replaceChild(networkLink,
				currentKmlObjects[ID]);
		currentKmlObjects[ID] = networkLink;// save the child to a
		// variable you can access later

		document.getElementById('kml-' + ID + '-check').checked = '';
		//alert("check box unchecked");
	//}
}

function CLEAR_ALL_CAMS() {// This function clears all map activity
	//kill the network links and the placemarks;
	//need to add if uName !='' dont do the mycams
	
	if (uName !='') {
		Stop_Function('SORAD_1A');
		Stop_Function('SORAD_1B');
	}
	Stop_Function('SORAD_1A_PUB');
	Stop_Function('SORAD_1B_PUB');

	var kmlCheckbox = document.getElementById('kml-GROUND_OVRLY_DEMO-check');
	if (kmlCheckbox.checked) {
		//alert("check box not checked");
		//kill the network link and the placemark;
		ge.getFeatures().removeChild(currentKmlObjects['GROUND_OVRLY_DEMO']);
		currentKmlObjects['GROUND_OVRLY_DEMO'] = null;
		ge.getFeatures().removeChild(currentKmlObjects['pm101']);
		currentKmlObjects['pm101'] = null;
		document.getElementById('kml-GROUND_OVRLY_DEMO-check').checked = '';
	}
	var kmlCheckbox = document.getElementById('kml-PIC_OVRLY_DEMO-check');
	if (kmlCheckbox.checked) {
		//alert("check box not checked hhh");
		//kill the network link and the placemark;
		ge.getFeatures().removeChild(currentKmlObjects['PIC_OVRLY_DEMO']);
		currentKmlObjects['PIC_OVRLY_DEMO'] = null;
		ge.getFeatures().removeChild(currentKmlObjects['pm102']);
		currentKmlObjects['pm102'] = null;
		document.getElementById('kml-PIC_OVRLY_DEMO-check').checked = '';
	}
	//alert("End Function");
	document.getElementById('kml-CLEAR_ALL_CAMS-check').checked = '';
	ge.getLayerRoot().enableLayerById(ge.LAYER_BUILDINGS, false);
}
//****************************************************************************************

function updateOptions() {// This functions adds the users options
	var options = ge.getOptions();
	var form = document.options;
	options.setStatusBarVisibility(form.statusbar.checked);
	options.setGridVisibility(form.grid.checked);
	options.setOverviewMapVisibility(form.overview.checked);
	options.setScaleLegendVisibility(form.scaleLegend.checked);
	options.setAtmosphereVisibility(form.atmosphere.checked);
	options.setMouseNavigationEnabled(form.mouseNav.checked);
	if (form.nav.checked) {
		ge.getNavigationControl().setVisibility(ge.VISIBILITY_SHOW);
	} else {
		ge.getNavigationControl().setVisibility(ge.VISIBILITY_HIDE);
	}
}// End updateOptions function
function failureCallback(object) {
}

/* Notes
 * realtime track kml's must be in kmz format or they just won't work at all
 */ 
