var ge;
var placemark;
var currentKmlObject = null;
var tourArray = new Array();
var allownewtour = true;
var waittime = 28000;
var popuphold = 14000;
var extrazoomtime = 2000;
var tourstop = new Array();
var tourstopcount = 0;
var tourstopexpect = 0;
var manualpopup = 0;
var idleid = 0;

// popup html
var content01 = '<iframe style="border: 0px;" width="100%" height="310" src="popup_lg/3C_swamp_survey.html"></iframe>';

var content02 = '<iframe style="border: 0px;" width="100%" height="310" src="popup_lg/system_adaptability.html"></iframe>';

var content03 = '<iframe style="border: 0px;" width="100%" height="310" src="popup_lg/high_productivity.html"></iframe>';

var content04 = '<iframe style="border: 0px;" width="100%" height="310" src="popup_lg/product_development.html"></iframe>';

var content05 = '<iframe style="border: 0px;" width="100%" height="310" src="popup_lg/operational_efficiency.html"></iframe>';

var content06 = '<iframe style="border: 0px;" width="100%" height="310" src="popup_lg/transition_zone_acquisition.html"></iframe>';

var content07 = '<iframe style="border: 0px;" width="100%" height="310" src="popup_lg/wireline_shooting.html"></iframe>';

var content08 = '<iframe style="border: 0px;" width="100%" height="310" src="popup_lg/durable_technology.html"></iframe>';

var content09 = '<iframe style="border: 0px;" width="100%" height="310" src="popup_lg/customer_service.html"></iframe>';

var content10 = '<iframe style="border: 0px;" width="100%" height="310" src="popup_lg/rapid_deployment.html"></iframe>';

var content11 = '<iframe style="border: 0px;" width="100%" height="310" src="popup_lg/HSE_efficiency.html"></iframe>';

var content12 = '<iframe style="border: 0px;" width="100%" height="310" src="popup_lg/superior_imaging.html"></iframe>';

var content13 = '<iframe style="border: 0px;" width="100%" height="310" src="popup_lg/design_flexibility.html"></iframe>';

var content14 = '<iframe style="border: 0px;" width="100%" height="310" src="popup_lg/optimizing_productivity.html"></iframe>';

var content15 = '<iframe style="border: 0px;" width="100%" height="310" src="popup_lg/arctic_operations.html"></iframe>';

var content16 = '<iframe style="border: 0px;" width="100%" height="310" src="popup_lg/system_integration.html"></iframe>';

var content17 = '<iframe style="border: 0px;" width="100%" height="310" src="popup_lg/new_testers.html"></iframe>';

//var content18 = '<iframe style="border: 0px;" width="100%" height="310" src="popup_lg/project_x.html"></iframe>';

var content19 = '<iframe style="border: 0px;" width="100%" height="310" src="popup_lg/remote_location_support.html"></iframe>';

var content20 = '<iframe style="border: 0px;" width="100%" height="310" src="popup_lg/new_vibrator.html"></iframe>';

var content21 = '<iframe style="border: 0px;" width="100%" height="240" src="popup_lg/houston.html"></iframe>';

var content22 = '<iframe style="border: 0px;" width="100%" height="240" src="popup_lg/beijing.html"></iframe>';

//var content23 = '<iframe style="border: 0px;" width="100%" height="230" src="popup_lg/norwich.html"></iframe>';

var content24 = '<iframe style="border: 0px;" width="100%" height="230" src="popup_lg/dubai.html"></iframe>';

var content25 = '<iframe style="border: 0px;" width="100%" height="230" src="popup_lg/voorschoten.html"></iframe>';

var content26 = '<iframe style="border: 0px;" width="100%" height="230" src="popup_lg/calgary.html"></iframe>';

var content27 = '<iframe style="border: 0px;" width="100%" height="230" src="popup_lg/moscow.html"></iframe>';

//var content28 = '<iframe style="border: 0px;" width="100%" height="230" src="popup_lg/jordan.html"></iframe>';

var content29 = '<iframe style="border: 0px;" width="100%" height="230" src="popup_lg/richardson.html"></iframe>';

var content30 = '<iframe style="border: 0px;" width="100%" height="230" src="popup_lg/edinburgh.html"></iframe>';

var content31 = '<iframe style="border: 0px;" width="100%" height="230" src="popup_lg/india.html"></iframe>';

var content32 = '<iframe style="border: 0px;" width="100%" height="230" src="popup_lg/zhouzhuo.html"></iframe>';

var content33 = '<iframe style="border: 0px;" width="100%" height="230" src="popup_lg/stafford.html"></iframe>';

var content34 = '<iframe style="border: 0px;" width="100%" height="230" src="popup_lg/geotek.html"></iframe>';

google.load("earth", "1");

// init
// place the google earth instance into the HTML
function init() {
  var content = document.getElementById('globecontent');
  var inputHTML = '';
  content.innerHTML = inputHTML;
	document.getElementById('loading').style.display = 'inline';
  google.earth.createInstance('globecontent', initCB, failureCB);
}

// finishFetchKml
// called after the kml is loaded, checks it and adds it to the earth
function finishFetchKml(kmlObject) {
  // check if the KML was fetched properly
  if (kmlObject) {
    // add the fetched KML to Earth
    currentKmlObject = kmlObject;
    ge.getFeatures().appendChild(currentKmlObject);
    getPlacemarks();
    ge.getOptions().setFlyToSpeed(0.1);
    setTimeout("document.getElementById('loading').style.display = 'none';", 5000);
    // start the initial idle timeout
		idleid = setTimeout("showtour();", 6*60*1000);
  } else {
    alert('ERROR: The KML or KMZ file is invalid or cannot be found.');
  }
}

// initCB
// initializes the earth's attributes and loads any KML/KMZ files needed
function initCB(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, false);
	ge.getLayerRoot().enableLayerById(ge.LAYER_TERRAIN, true);
	
	// set the movement functions
	google.earth.addEventListener(ge.getView(), 'viewchangebegin', stoptour);
  
  google.earth.addEventListener(ge.getView(), 'viewchangeend', function() {
    if (allownewtour) idleid = setTimeout("showtour();", 6*60*1000);
  });
	
	// load kmz
	google.earth.fetchKml(ge, "http://www.inovageo.com/resources/LandGlobe.kmz", finishFetchKml);
	//google.earth.fetchKml(ge, "http://axiom.sybilmedia.com/inova_land/LandGlobe.kmz", finishFetchKml);
  var pluginVersion = ge.getPluginVersion().toString();
  //document.getElementById('installed-plugin-version').innerHTML = 'Version: ' +  pluginVersion;
}

// failureCB
// failure function for the initialization, currently empty
function failureCB(errorCode) {}

google.setOnLoadCallback(init);


// stoptour
// called any time the user interacts with the globe. if the tour is going it stops the tour.
// it also kills the idle timeout. the idle timeout is reset everytime the earth stops moving.
function stoptour(force) {
	clearTimeout(idleid);
	tourstopcount++;
	if ((tourstopcount != tourstopexpect)||force) {
		clearTimeout(manualpopup);
  	for (var i = 0; i < tourstop.length; i++) clearTimeout(tourstop[i]);
  	allownewtour = true;
  	tourstopcount = 0;
  }
}

// showpopup
// the actual function that shows the popup with content
function showpopup(placemarkid) {
	// create the balloon object
	var balloon = ge.createHtmlDivBalloon('');
  balloon.setFeature(tourArray[placemarkid]);
  balloon.setCloseButtonEnabled(false);
  balloon.setBackgroundColor("#000000");

  // create the <div>
  var div = document.createElement('DIV');
  div.style.fontFamily = "Arial";
  div.style.fontSize = "12px";
  div.style.lineHeight = "14px";
  div.style.color = "#FFFFFF";
  div.style.background = "#000000";
  div.style.border = "0 none";
  //div.style.padding = "12px";
  div.style.padding = "0px 0px 10px 0px";  
  div.style.margin = "5px 0px 0px 5px";
  div.style.width = "330px";
  
  eval("div.innerHTML = content" + ((placemarkid+1) > 9 ? (placemarkid+1) : "0" + (placemarkid+1)) + ";");
	
	var divcheck = new String(div.innerHTML);
	if (divcheck.length > 10) {
		balloon.setContentDiv(div);
	  ge.setBalloon(balloon);
	}
}

// getPlacemarks
// iterate through the kmz file and retrieve all placemarks, set them in an array
function getPlacemarks() {
	if (currentKmlObject) {
		var placemark = currentKmlObject.getFeatures().getFirstChild();
		var pmloc = 0;
		placemark = placemark.getFeatures().getFirstChild();
		while(placemark) {
			if (placemark.getType() == "KmlPlacemark") {
				var tourpoint = placemark;
				tourArray[tourArray.length] = tourpoint;
				// assign the click event for this placemark
				/*
				google.earth.addEventListener(placemark, 'click', function(event) {
					eval("showpopup(" + pmloc + ");");
			  });
				*/
				switch (pmloc) {
					case 0:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(0);
					  });
						break;
					case 1:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(1);
					  });
						break;
					case 2:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(2);
					  });
						break;
					case 3:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(3);
					  });
						break;
					case 4:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(4);
					  });
						break;
					case 5:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(5);
					  });
						break;
					case 6:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(6);
					  });
						break;
					case 7:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(7);
					  });
						break;
					case 8:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(8);
					  });
						break;
					case 9:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(9);
					  });
						break;
					case 10:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(10);
					  });
						break;
					case 11:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(11);
					  });
						break;
					case 12:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(12);
					  });
						break;
					case 13:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(13);
					  });
						break;
					case 14:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(14);
					  });
						break;
					case 15:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(15);
					  });
						break;
					case 16:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(16);
					  });
						break;
					case 17:
						//google.earth.addEventListener(placemark, 'click', function(event) {
						//	showpopup(17);
					  //});
						break;
					case 18:
						//google.earth.addEventListener(placemark, 'click', function(event) {
						//	showpopup(18);
					  //});
						break;
					case 19:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(19);
					  });
						break;
					case 20:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(20);
					  });
						break;
					case 21:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(21);
					  });
						break;
					case 22:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(22);
					  });
						break;
					case 23:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(23);
					  });
						break;
					case 24:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(24);
					  });
						break;
					case 25:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(25);
					  });
						break;
					case 26:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(26);
					  });
						break;
					case 27:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(27);
					  });
						break;
					case 28:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(28);
					  });
						break;
					case 29:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(29);
					  });
						break;
					case 30:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(30);
					  });
						break;
					case 31:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(31);
					  });
						break;
					case 32:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(32);
					  });
						break;
					case 33:
						google.earth.addEventListener(placemark, 'click', function(event) {
							showpopup(33);
					  });
						break;
					default:
						break;
				}
				pmloc++;
			}
			placemark = placemark.getNextSibling();
		}
	}
}

// showtour
// starts the tour. this is a manual tour and not a KML tour so that we have the most control
function showtour() {
	if ((tourArray.length > 0)&&allownewtour) {
		allownewtour = false;
		tourstop[tourstop.length] = setTimeout("ge.setBalloon(null); tourstopcount = 0; tourstopexpect = 1; ge.getView().setAbstractView(tourArray[1].getAbstractView())",0);
		tourstop[tourstop.length] = setTimeout("showpopup(1)",waittime*1 - popuphold + extrazoomtime);
		tourstop[tourstop.length] = setTimeout("ge.setBalloon(null); tourstopexpect = 2; ge.getView().setAbstractView(tourArray[5].getAbstractView())",waittime*1);
		tourstop[tourstop.length] = setTimeout("showpopup(5)",waittime*2 - popuphold + extrazoomtime);
		tourstop[tourstop.length] = setTimeout("ge.setBalloon(null); tourstopexpect = 3; ge.getView().setAbstractView(tourArray[7].getAbstractView())",waittime*2);
		tourstop[tourstop.length] = setTimeout("showpopup(7)",waittime*3 - popuphold + extrazoomtime);
		tourstop[tourstop.length] = setTimeout("ge.setBalloon(null); tourstopexpect = 4; ge.getView().setAbstractView(tourArray[10].getAbstractView())",waittime*3);
		tourstop[tourstop.length] = setTimeout("showpopup(10)",waittime*4 - popuphold + extrazoomtime);
		tourstop[tourstop.length] = setTimeout("ge.setBalloon(null); tourstopexpect = 5; ge.getView().setAbstractView(tourArray[13].getAbstractView())",waittime*4);
		tourstop[tourstop.length] = setTimeout("showpopup(13)",waittime*5 - popuphold + extrazoomtime);
		tourstop[tourstop.length] = setTimeout("ge.setBalloon(null); tourstopexpect = 6; ge.getView().setAbstractView(tourArray[16].getAbstractView()); allownewtour = true; setTimeout('showtour();', waittime);",waittime*5);
		tourstop[tourstop.length] = setTimeout("showpopup(16)",waittime*6 - popuphold + extrazoomtime);
	}
}

// prepMove
// common code for each of the show functions
function prepMove() {
	stoptour(true);
	tourstopcount = 0; tourstopexpect = 1;
	ge.setBalloon(null);
}

// show functions for each area. these could actually be consolidated into 1 function
// but it's easier to read and understand this way. additionally, it would be easier to
// change given any changes in the KMZ file.

function showPushpin(pinnum, extrawaittime) {
	if (isNaN(extrawaittime)) extrawaittime = 0;
	prepMove();
	ge.getView().setAbstractView(tourArray[pinnum - 1].getAbstractView());
	manualpopup = setTimeout("showpopup(" + (pinnum - 1) + ");", waittime - popuphold + extrazoomtime + extrawaittime);
}
