var ge;
var placemark;
var currentKmlObject = null;
var tourArray = new Array();
var allownewtour = true;
var waittime = 24000;
var popuphold = 10000;
var extrazoomtime = 5500;
var tourstop = new Array();
var tourstopcount = 0;
var tourstopexpect = 0;
var manualpopup = 0;
var idleid = 0;
var google_type = 0; // 0 = earth, 1 = map
var map;
var kmzcount = 0;

// popup html
var illizibasin = '<iframe style="border: 0px;" width="100%" height="300" src="/content/includes/globes/popup/illizibasin.html"></iframe>';

var perchelesarea = '<iframe style="border: 0px;" width="100%" height="300" src="/content/includes/globes/popup/perchelesarea.html"></iframe>';

var barnettshale = '<iframe style="border: 0px;" width="100%" height="300" src="/content/includes/globes/popup/barnettshale.html"></iframe>';

var sichuanbasin = '<iframe style="border: 0px;" width="100%" height="300" src="/content/includes/globes/popup/sichuanbasin.html"></iframe>';

var offshorenigeria = '<iframe style="border: 0px;" width="100%" height="300" src="/content/includes/globes/popup/offshorenigeria.html"></iframe>';

var heilongjiang = '<iframe style="border: 0px;" width="100%" height="300" src="/content/includes/globes/popup/heilongjiang.html"></iframe>';

var wafrica = '<iframe style="border: 0px;" width="100%" height="300" src="/content/includes/globes/popup/wafrica.html"></iframe>';

var tulipanfield = '<iframe style="border: 0px;" width="100%" height="300" src="/content/includes/globes/popup/tulipanfield.html"></iframe>';

var seindia = '<iframe style="border: 0px;" width="100%" height="300" src="/content/includes/globes/popup/seindia.html"></iframe>';

var santosbasin = '<iframe style="border: 0px;" width="100%" height="300" src="/content/includes/globes/popup/santosbasin.html"></iframe>';
	
var gulfofmexico = '<iframe style="border: 0px;" width="100%" height="300" src="/content/includes/globes/popup/gulfofmexico.html"></iframe>';

var blacksea = '<iframe style="border: 0px;" width="100%" height="300" src="/content/includes/globes/popup/blacksea.html"></iframe>';

var aberdeen = '<iframe style="border: 0px;" width="100%" height="300" src="/content/includes/globes/popup/aberdeen.html"></iframe>';

var cairo = '<iframe style="border: 0px;" width="100%" height="300" src="/content/includes/globes/popup/cairo.html"></iframe>';

var calgary = '<iframe style="border: 0px;" width="100%" height="300" src="/content/includes/globes/popup/calgary.html"></iframe>';

var denver = '<iframe style="border: 0px;" width="100%" height="300" src="/content/includes/globes/popup/denver.html"></iframe>';

var houston = '<iframe style="border: 0px;" width="100%" height="300" src="/content/includes/globes/popup/houston.html"></iframe>';

var london = '<iframe style="border: 0px;" width="100%" height="300" src="/content/includes/globes/popup/london.html"></iframe>';

var luanda = '<iframe style="border: 0px;" width="100%" height="300" src="/content/includes/globes/popup/luanda.html"></iframe>';

var moscow = '<iframe style="border: 0px;" width="100%" height="300" src="/content/includes/globes/popup/moscow.html"></iframe>';

var portharcourt = '<iframe style="border: 0px;" width="100%" height="300" src="/content/includes/globes/popup/portharcourt.html"></iframe>';

var portofspain = '<iframe style="border: 0px;" width="100%" height="300" src="/content/includes/globes/popup/portofspain.html"></iframe>';
  	  
google.load("earth", "1");

// init
// place the google earth instance into the HTML
function init() {
  var content = document.getElementById('content');
  var inputHTML = '';
  content.innerHTML = inputHTML;
	document.getElementById("loading").style.display = 'inline';
	google_type = 0;
  google.earth.createInstance('content', 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
    kmzcount++;
    currentKmlObject = kmlObject;
    ge.getFeatures().appendChild(currentKmlObject);
    getPlacemarks();
    ge.getOptions().setFlyToSpeed(0.1);
    setTimeout("document.getElementById('loading').style.display = 'none';", 5000);
  } 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();", 3*60*1000);
  });
	
	// load kmz
	google.earth.fetchKml(ge, "http://www.iongeo.com/content/includes/globes/DPwSpans.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
// actually a pre-popup that is blank. This is because of the know google earth plugin scrollbar issue.
// without this function, the scrollbar position will be remembered from popup to popup and each new popup
// window will not have the scrollbar at the top.
/*
function showpopup(placemarkid) {
  var balloon = ge.createHtmlDivBalloon('');
  balloon.setFeature(tourArray[placemarkid]);
  balloon.setCloseButtonEnabled(false);
  balloon.setBackgroundColor("#000000");
  
  var div = document.createElement('DIV');
  div.innerHTML = "<img src='images/spacer.gif' width='255' height='300'/>";
  
  balloon.setContentDiv(div);
  ge.setBalloon(balloon);
  setTimeout("showpopuppost(" + placemarkid + ");", 500);
}
*/
// showpopuppost
// 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 = "285px";
  
  switch (placemarkid) {
  	case 0:		// aberdeen
  		  div.innerHTML = aberdeen;
		  break;
  	case 1:		// cairo
		  div.innerHTML = cairo;
		  break;
		case 2:		// calgary
		  div.innerHTML = calgary;
		  break;
		case 3:		// denver
		  div.innerHTML = denver;
		  break;
		case 4:		// london
		  div.innerHTML = london;
		  break;
		case 5:		// luanda
		  div.innerHTML = luanda;
		  break;
		case 6:		// moscow
		  div.innerHTML = moscow;
		  break;
		case 7:		// port harcourt
		  div.innerHTML = portharcourt;
		  break;
		case 8:		// port of spain
		  div.innerHTML = portofspain;
		  break;
		case 9:		// houston
		  div.innerHTML = houston;
		  break;
		case 10:	// illizi basin, angola
		  div.innerHTML = illizibasin;
		  break;
  	case 11:	// percheles area
  	  div.innerHTML = perchelesarea;
		  break;
		case 12:	// barnett shale, texas
  	  div.innerHTML = barnettshale;
		  break;
		case 13:	// sichuan basin, china
  	  div.innerHTML = sichuanbasin;
		  break;
		case 14:	// offshore nigeria
			div.innerHTML = offshorenigeria;
			break;
		case 15:	// heilongjiang province, china
			div.innerHTML = heilongjiang;
			break;
		case 16:	// west africa
			div.innerHTML = wafrica;
			break;
  	case 17:	// tulipan field, norway
		  div.innerHTML = tulipanfield;
		  break;
		case 18:	// south east india
  	  div.innerHTML = seindia;
		  break;
		case 19:	// santos basin
  	  div.innerHTML = santosbasin;
		  break;
		case 20:	// gulf of mexico
			div.innerHTML = gulfofmexico;
			break;
		case 21:	// black sea
  	  div.innerHTML = blacksea;
		  break;
		default:
			div.innerHTML = '';
			break;
	}
	
	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
				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;
					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&&!google_type) {
		allownewtour = false;
		tourstop[tourstop.length] = setTimeout("ge.setBalloon(null); tourstopcount = 0; tourstopexpect = 1; ge.getView().setAbstractView(tourArray[10].getAbstractView())",0);
		tourstop[tourstop.length] = setTimeout("showpopup(10)",waittime*1 - popuphold);
		tourstop[tourstop.length] = setTimeout("ge.setBalloon(null); tourstopexpect = 2; ge.getView().setAbstractView(tourArray[11].getAbstractView())",waittime*1);
		tourstop[tourstop.length] = setTimeout("showpopup(11)",waittime*2 - popuphold);
		tourstop[tourstop.length] = setTimeout("ge.setBalloon(null); tourstopexpect = 3; ge.getView().setAbstractView(tourArray[12].getAbstractView())",waittime*2);
		tourstop[tourstop.length] = setTimeout("showpopup(12)",waittime*3 - popuphold);
		tourstop[tourstop.length] = setTimeout("ge.setBalloon(null); tourstopexpect = 4; ge.getView().setAbstractView(tourArray[13].getAbstractView())",waittime*3);
		tourstop[tourstop.length] = setTimeout("showpopup(13)",waittime*4 - popuphold);
		tourstop[tourstop.length] = setTimeout("ge.setBalloon(null); tourstopexpect = 5; ge.getView().setAbstractView(tourArray[14].getAbstractView())",waittime*4);
		tourstop[tourstop.length] = setTimeout("showpopup(14)",waittime*5 - popuphold);
		tourstop[tourstop.length] = setTimeout("ge.setBalloon(null); tourstopexpect = 6; ge.getView().setAbstractView(tourArray[15].getAbstractView())",waittime*5);
		tourstop[tourstop.length] = setTimeout("showpopup(15)",waittime*6 - popuphold);
		tourstop[tourstop.length] = setTimeout("ge.setBalloon(null); tourstopexpect = 7; ge.getView().setAbstractView(tourArray[16].getAbstractView())",waittime*6);
		tourstop[tourstop.length] = setTimeout("showpopup(16)",waittime*7 - popuphold);
		tourstop[tourstop.length] = setTimeout("ge.setBalloon(null); tourstopexpect = 8; ge.getView().setAbstractView(tourArray[17].getAbstractView())",waittime*7);
		tourstop[tourstop.length] = setTimeout("showpopup(17)",waittime*8 - popuphold);
		tourstop[tourstop.length] = setTimeout("ge.setBalloon(null); tourstopexpect = 9; ge.getView().setAbstractView(tourArray[18].getAbstractView())",waittime*8);
		tourstop[tourstop.length] = setTimeout("showpopup(18)",waittime*9 - popuphold);
		tourstop[tourstop.length] = setTimeout("ge.setBalloon(null); tourstopexpect = 10; ge.getView().setAbstractView(tourArray[19].getAbstractView())",waittime*9);
		tourstop[tourstop.length] = setTimeout("showpopup(19)",waittime*10 - popuphold);
		tourstop[tourstop.length] = setTimeout("ge.setBalloon(null); tourstopexpect = 11; ge.getView().setAbstractView(tourArray[20].getAbstractView())",waittime*10);
		tourstop[tourstop.length] = setTimeout("showpopup(20)",waittime*11 - popuphold);
		tourstop[tourstop.length] = setTimeout("ge.setBalloon(null); tourstopexpect = 12; ge.getView().setAbstractView(tourArray[21].getAbstractView()); allownewtour = true; setTimeout('showtour();', waittime);",waittime*11);
		tourstop[tourstop.length] = setTimeout("showpopup(21)",waittime*12 - popuphold);
	}
}

// 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 showAberdeen() {
	if (!google_type) {
		prepMove();
		ge.getView().setAbstractView(tourArray[0].getAbstractView());
		manualpopup = setTimeout("showpopup(0);", waittime - popuphold + extrazoomtime);
	}
}

function showCalgary() {
	if (!google_type) {
		prepMove();
		ge.getView().setAbstractView(tourArray[2].getAbstractView());
		manualpopup = setTimeout("showpopup(2);", waittime - popuphold + extrazoomtime);
	}
}
    
function showDenver() {
	if (!google_type) {
		prepMove();
		ge.getView().setAbstractView(tourArray[3].getAbstractView());
		manualpopup = setTimeout("showpopup(3);", waittime - popuphold + extrazoomtime);
	}
}

function showHouston() {
	if (!google_type) {
		prepMove();
		ge.getView().setAbstractView(tourArray[9].getAbstractView());
		manualpopup = setTimeout("showpopup(9);", waittime - popuphold + extrazoomtime);
	}
}

function showLondon() {
	if (!google_type) {
		prepMove();
		ge.getView().setAbstractView(tourArray[4].getAbstractView());
		manualpopup = setTimeout("showpopup(4);", waittime - popuphold + extrazoomtime);
	}
}

function showLuanda() {
	if (!google_type) {
		prepMove();
		ge.getView().setAbstractView(tourArray[5].getAbstractView());
		manualpopup = setTimeout("showpopup(5);", waittime - popuphold + extrazoomtime);
	}
}

function showMoscow() {
	if (!google_type) {
		prepMove();
		ge.getView().setAbstractView(tourArray[6].getAbstractView());
		manualpopup = setTimeout("showpopup(6);", waittime - popuphold + extrazoomtime);
	}
}

function showPortHarcourt() {
	if (!google_type) {
		prepMove();
		ge.getView().setAbstractView(tourArray[7].getAbstractView());
		manualpopup = setTimeout("showpopup(7);", waittime - popuphold + extrazoomtime);
	}
}

function showPortOfSpain() {
	if (!google_type) {
		prepMove();
		ge.getView().setAbstractView(tourArray[8].getAbstractView());
		manualpopup = setTimeout("showpopup(8);", waittime - popuphold + extrazoomtime);
	}
}

function showCairo() {
	if (!google_type) {
		prepMove();
		ge.getView().setAbstractView(tourArray[1].getAbstractView());
		manualpopup = setTimeout("showpopup(1);", waittime - popuphold + extrazoomtime);
	}
}

function showProject1() {
	if (!google_type) {
		prepMove();
		ge.getView().setAbstractView(tourArray[10].getAbstractView());
		manualpopup = setTimeout("showpopup(10);", waittime - popuphold);
	}
}

function showProject2() {
	if (!google_type) {
		prepMove();
		ge.getView().setAbstractView(tourArray[11].getAbstractView());
		manualpopup = setTimeout("showpopup(11);", waittime - popuphold);
	}
}

function showProject3() {
	if (!google_type) {
		prepMove();
		ge.getView().setAbstractView(tourArray[12].getAbstractView());
		manualpopup = setTimeout("showpopup(12);", waittime - popuphold);
	}
}

function showProject4() {
	if (!google_type) {
		prepMove();
		ge.getView().setAbstractView(tourArray[13].getAbstractView());
		manualpopup = setTimeout("showpopup(13);", waittime - popuphold);
	}
}

function showProject5() {
	if (!google_type) {
		prepMove();
		ge.getView().setAbstractView(tourArray[14].getAbstractView());
		manualpopup = setTimeout("showpopup(14);", waittime - popuphold);
	}
}

function showProject6() {
	if (!google_type) {
		prepMove();
		ge.getView().setAbstractView(tourArray[15].getAbstractView());
		manualpopup = setTimeout("showpopup(15);", waittime - popuphold);
	}
}

function showProject7() {
	if (!google_type) {
		prepMove();
		ge.getView().setAbstractView(tourArray[16].getAbstractView());
		manualpopup = setTimeout("showpopup(16);", waittime - popuphold);
	}
}

function showProject8() {
	if (!google_type) {
		prepMove();
		ge.getView().setAbstractView(tourArray[17].getAbstractView());
		manualpopup = setTimeout("showpopup(17);", waittime - popuphold);
	}
}

function showProject9() {
	if (!google_type) {
		prepMove();
		ge.getView().setAbstractView(tourArray[18].getAbstractView());
		manualpopup = setTimeout("showpopup(18);", waittime - popuphold);
	}
}

function showProject10() {
	if (!google_type) {
		prepMove();
		ge.getView().setAbstractView(tourArray[19].getAbstractView());
		manualpopup = setTimeout("showpopup(19);", waittime - popuphold);
	}
}

function showProject11() {
	if (!google_type) {
		prepMove();
		ge.getView().setAbstractView(tourArray[20].getAbstractView());
		manualpopup = setTimeout("showpopup(20);", waittime - popuphold);
	}
}

function showProject12() {
	if (!google_type) {
		prepMove();
		ge.getView().setAbstractView(tourArray[21].getAbstractView());
		manualpopup = setTimeout("showpopup(21);", waittime - popuphold);
	}
}

function googleToggle() {
	stoptour();
	if (GBrowserIsCompatible()) {
		if (google_type != 1) {
			document.getElementById("togglelink").innerText = 'GOOGLE EARTH VERSION';
			var geoXml = new GGeoXml("http://www.iongeo.com/content/includes/globes/DPwSPANS.kmz");
	    map = new GMap2(document.getElementById("content"));
	    //var la = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
		  //alert(la.getLatitude());
		  //alert(la.getLongitude());
	    map.setCenter(new GLatLng(29.337166,-93.106176), 5); 
	    map.addControl(new GLargeMapControl());
	    map.addControl(new GMapTypeControl());
	    map.addOverlay(geoXml);
	    google_type = 1;
	  } else {
	  	location.href = "/content/includes/globes/data_processing.asp";
	  }
	}
	else alert("browser not compatible");
}
