google.load("jquery", "1");
google.load("maps", "2.x");
google.load("earth", "1");


// The default position of Camera when reset the game
var orgLookAt;


var IGE_geHelpers;
var IGE_ge;
var IGE_id= "earth";
var IGE_checkInstalledTimeout;


function IGE_showEarth(opt_cb) {
	if(!isReset)
		mainNavControl(false);
	FDC_hideFdiv("game");
	 IGE_removeErrorIframe(1);
	
	
	if ($("#" + IGE_id).length == 0) {
	
		$(FDC_mainContent).append(jQuery("<div></div>").attr("id", IGE_id));
		var cssObj = {position: "absolute", left: 0, top: 0, zIndex: 50, width: 980, height: 644, overflow: "hidden"};
		//var cssObj = {position: "absolute", left: 0, top: 0, zIndex: 50, width: 980, height: 640};
		$("#"+IGE_id).css(cssObj);
		
	}
	
	if (!IGE_ge) {
		
		google.earth.createInstance(
		IGE_id,
		function(ge) {
			FDC_openFdiv('gameMainOverlay', {aCallback: function(){
				
				thisMovie('gameMainOverlay').gotoLoading();
				clearTimeout(IGE_checkInstalledTimeout);
				IGE_removeErrorIframe(0);
				
				IGE_ge = ge;
				IGE_ge.getWindow().setVisibility(true);
				IGE_ge.getLayerRoot().enableLayerById(IGE_ge.LAYER_BUILDINGS, true);
				IGE_ge.getNavigationControl().setVisibility(IGE_ge.VISIBILITY_SHOW);
				var navControl = IGE_ge.getNavigationControl();
				var screenXY = navControl.getScreenXY();
				  screenXY.setXUnits(IGE_ge.UNITS_PIXELS);
				  screenXY.setYUnits(IGE_ge.UNITS_INSET_PIXELS);  
				//IGE_ge.getOptions().setStatusBarVisibility(true);
				IGE_geHelpers = new GEHelpers(IGE_ge);
				
				
				
				disableMouse();
				//LookAt object 
				var la = IGE_ge.createLookAt(''); 
				la.set(33.829632250442046 , 
							118.34008049761679 , 
							20000000, 
							IGE_ge.ALTITUDE_RELATIVE_TO_GROUND, 
							0, 
							0, 
							0 );
				orgLookAt = la;
	
				flyToLocation(la, true);
				
				loadCityXML(loadRouteXML);
				if(opt_cb){
					//FDC_hideFdiv('gameModeTest');
					notShowSelection = true;
				}
				FDC_showFdiv('gameMainOverlay');
			}});
			//alert($("#"+IGE_id)[0].innerHTML);
		},
		function() {
			//<a href="javascript:window.google.earth.installViaStub();void(0);" name="install"><img src="../images/installGEPlugin.jpg" /></a>
			//clearTimeout(IGE_checkInstalledTimeout);
			//IGE_checkInstalledTimeout = setTimeout("IGE_doCheckInstalledLoop("+opt_cb+")", 3000);
			if(!google.earth.isInstalled())
				$('#earth').html('<img src="http://www.nike.com.hk/local/running10k_2008/images/images/GoogleEarthPlugIn.jpg" alt="" width="980" height="644" border="0" usemap="#Map"><map name="Map"><area shape="rect" coords="327,183,654,238" href="javascript:window.google.earth.installViaStub();void(0);"></map>');
			IGE_doCheckInstalledLoop(opt_cb);
			
			});
		
	}
	else {
		IGE_ge.getWindow().setVisibility(true);
		mainNavControl(true);
		var cssObj = {left: 0};
		$("#"+IGE_id).css(cssObj);
	}
	//FDC_openFdiv("loadingOverlay","{isTransparent:true}");
}


function IGE_removeErrorIframe(remain) {
	
	var geIframes = $("#" + IGE_id + " div div iframe");
	var geErrorIframes = [];
	
	for (var i = 0; i < geIframes.length; i++) {
		var src = geIframes[i].src;
		if (src && src.indexOf("http://www.google.com/earth/plugin/error.html") != -1) {
			geErrorIframes.push(geIframes[i]);
		}
	}
	//geErrorIframes.reverse();
	
	for (var i = 0; i < geErrorIframes.length - remain; i++) {
		$(geErrorIframes[i]).parent().parent().remove();
	}
	
}


function IGE_doCheckInstalledLoop(opt_cb) {
	
    if (google.earth.isInstalled()) {
    	$('#earth').html('');
    	clearTimeout(IGE_checkInstalledTimeout);
		IGE_removeErrorIframe(0);
    	IGE_showEarth(opt_cb);

      
    }
    else {
      IGE_checkInstalledTimeout = setTimeout("IGE_doCheckInstalledLoop("+opt_cb+")", 1000);
    }

}


function IGE_hideEarth() {
	//alert("hide google ")
	FDC_showFdiv("game");
	//alert('hide test 0');
	mainNavControl(false);
	resetGame({hide: true});
	//alert('hide test 1');
	IGE_ge.getWindow().setVisibility(false);
	//alert('hide test 2');
	var cssObj = {left: -9999};
	//alert('hide test 3');
	$("#"+IGE_id).css(cssObj);
	//alert('hide test 4');
}

/**
	*	Move the camera to the position of LookAt object without the zoom effect 
	*	i.e. move to the point immediately
	*  @param {KMLlookAt} lookAt The position to fly to
	*/
function flyToLocation(lookAt, opt){

	if(opt){
		var oldFlyToSpeed = IGE_ge.getOptions().getFlyToSpeed();
		IGE_ge.getOptions().setFlyToSpeed(1000);
		IGE_ge.getView().setAbstractView(lookAt);
		IGE_ge.getOptions().setFlyToSpeed(oldFlyToSpeed);
	}else{
		IGE_ge.getOptions().setFlyToSpeed(1);
		IGE_ge.getView().setAbstractView(lookAt);
	}
}

function countDown(value){
	disableMouse();
	if(value == 4){
		preloadCityPlacemark({from: 'countdown value 2'});
	}

	
	if(value != 0){
		//alert('count Down repeat');
		$('#status').html('Last '+value+' seconds before start');
		value--;
		setTimeout('countDown('+value+')', 1000);
		
	}else{
		//alert('Run game');

		$('#status').html('');
		startGame();
		thisMovie('gameInfo').enableCityBtn();
		thisMovie('gameInfo').voiceOut('start' ,0);
	}
}


function loadSwfOverlay(){
	//alert('LoadSwfOverlay');
	if(!notShowSelection){
		if(thisMovie('gameMainOverlay')){
			FDC_closeFdiv('gameMainOverlay');
		}
		FDC_openFdiv('gameMainOverlay', {aCallback: function(){thisMovie('gameMainOverlay').gotoChangeMode();}});
	}else{
		notShowSelection = false;
		FDC_showFdiv('gameModeTest');
	}
}
