
/**
*	The last key being pressed in the keyboard event
*/
var lastKey= "";

/**
*	The current key being pressed in the keyboard event
*/
var curKey ="";

/**
*	The number of times that correct key being pressed within 0.5 second
*/
var Xspeed = 0;

/**
*	The default speed of each mode, it will being updated when changeMode function is called
*		Keyboard mode: 0
*		Sport Band mode: 6
*/
var defaultSpeed = 0;

/**
*	The distance travel by member who play sport band mode
*/
var maxDistance = 0;

/**
*	The duration time of last travel by member who play sport band mode
*/

var durationTime = 0;

/**
*	The game mode of the user choosed, by deafault is zero
*/
var gameMode = -1;




var allowUpdate = true;
var allowFocus = true;

/**
*	Change the game mode of Google Earth
*	@param {number} mode: enum of game mdoe, 0: Sport Band, 1: Keyboard
*	@param {distance} distance: if it is SB mode, the distance travel in this running
*	@param {time} time: if it is SB mode, the duration time in this running
*/
function changeMode(mode, distance, time){
	//alert('Change mode to '+mode);
	//alert('Change Mode: Distance and time: ' + mode + '   ' +distance + '     '+	time);
	
	gameMode = mode;
	//alert('GameMode is  '+gameMode);
	
	if(thisMovie('gameModeTest')){
		reqGameMode();
	}else{
		setTimeout("reqGameMode()", 1000);
	}
	//alert(mode);
	if(gameMode == 0){
		//alert(distance);
		maxDistance = distance*1000;
		durationTime = time/1000;
	}

	switch(gameMode){
		case 0: defaultSpeed = 10; break;
		case 1: defaultSpeed = 0; break;
		case 2: defaultSpeed = 0; break;
	}
		//setTimeout('setGame('+mode+','+distance+','+time+')', 1000);
	overlayTest();
	rotateEarth();
}

/**
*	Call by keyboard event in keyborad mode.
*	@param the number of times correct key being pressed within 0.5 second
*/
function updateXspeed(){
	Xspeed++;
}

/**
*	Being Called every 0.5 second during the game to update the speed of the gameObj (DS_simulator obj)
*	@param {number} DS_simulator.options.speed: Speed of DS_simulator obj
*/
function updateSpeed(){

	if(allowUpdate){
		allowUpdate = false;
		if(DS_simulator){
			IGE_ge.getWindow().blur();
			if(gameMode == 1)
				Xspeed = Xspeed * 1.4;
			DS_simulator.options.speed = Xspeed*0.5;
		}

		playTime ++;

		if(playTime % 2 == 0)
			updateDisplay();

		Xspeed=defaultSpeed;
		setTimeout("setUpdate()", 500);
	}
}

function setUpdate(){
	allowUpdate = true;
}

/**
*	KeyEvent when key is being pressed, check whether the key pressed is correct or not
*	If the key pressed is correct, call updateXspeed () to increase the Xspeed
*
*/
function keyDown(event) {
	if(DS_simulator && gameMode ==1){
		if (!event) {  event = window.event;  }
		curKey = event.keyCode;
		if (curKey == 37 && lastKey == 39){
			updateXspeed();
		} else if(curKey == 39 && lastKey == 37){
			updateXspeed();
		}
		lastKey = curKey;
	}
}

/**
*	This function continous sending out information to update the display panel
*	which show the udpatest game info of the game
*	If it is Sport Band mode, it will check whether the total distance travel is larger than actual distance travel
*		if the result is true, the game will end immediately
*	@param {number} pastDistance: The total distance that gameObj passed in google Earth
*/
function updateDisplay(){


	var pastDistance= round(DS_simulator.totalDistance/1000,2);

	if (DS_simulator) {
		$('#status').html(
		'<strong>Time:</strong> ' +
		DS_formatTime(playTime/2) + '<br/>' +
		'<strong>Distance:</strong> ' +
		(
		pastDistance +
		' km' + '<br/>' +
		'<strong>Current Speed:</strong> ' +
		Math.round(DS_simulator.currentSpeed  ) ) +' * '  +DS_simulator.options.speed+
		'<br/>');
	}

	var runnerSpeed = Math.round(DS_simulator.currentSpeed   *DS_simulator.options.speed) ;
	thisMovie('gameInfo').updateInfo(DS_formatTime(playTime/2), pastDistance, runnerSpeed);

	if(gameMode == 0)
	if(maxDistance <= DS_simulator.totalDistance){
		//alert('gameEnd becuase of maxDistance');
		DS_simulator.endFlag = true;
	}

}

function onCompleteTest(opt_cb){
	var onComplete = false;
	while(!onComplete){
		onComplete = setTimeout("onCompleteTest()", 1000);
	}
}
