/*
	jankenpon :: demo 6
	derek@monkeymountain.co.uk
	17 apr 2001
	updated 19 apr 2001	
	updated 18 may 2001	
	updated 27 may 2001 
	updated 04 june 2001 
*/

//	global variables
var strLanguage;	//	EN english, JP japanese
var intOpponentTimer,intMoney,intLevel,intMove1P,intMoveCOM,intLives;
var boolTimeUp,boolPlaying;
var timerComputer;

//	in-game text/images
var arrMoveNames_EN = new Array("paper","scissors","stone");
var arrMoveNames_JP = new Array("paa","choki","guu");
var arrMessages = new Array("win","lose","draw","timeup","endgame win","endgame lose","play","game over");
var arrMessages_EN = new Array("win","lose","draw","timeup","endgame win","endgame lose","play","game over");
var arrMessages_JP = new Array("Jwin","Jlose","Jdraw","Jtimeup","Jendgame win","Jendgame lose","Jplay","Jgame over");

//	game functions

//	init
//	initialises game. can be run more than once - acts as a reset switch
function init()
{
	strLanguage = "EN";
	intOpponentTimer = 5000;
	intMoney = 0;
	intLevel = 1;
	intMove1P = "";
	intMoveCOM = "";
	intLives = 3;
	boolTimeUp = true;
	boolPlaying=false;
	clearTimeout(timerComputer);
	//	show scores
	update_scores();
	div_grid.style.visibility = "visible";
	div_play.style.visibility = "hidden";
	div_start.style.visibility = "visible";
	div_gameover.style.visibility="hidden";
	div_debug.innerHTML = "";
}

//	lose_life
//	lose a life, update the graphics
function lose_life()
{
	intLives --;
	update_scores();
	//	update life graphic [animated?]
}

//	win_game
//	increases money, level etc
function win_game()
{
	if (intOpponentTimer > 1000) intOpponentTimer -= 100;
//	div_debug.innerHTML += "<br>" + intOpponentTimer;
	intMoney += (intLevel * 100);
	if (intLevel < 3)
	{
		 intLevel ++;
	}
	else
	{
		endgame_win();
	}
	update_scores();
	show_message(0);
//
	intMove1P = "";
	intMoveCOM = "";
	setTimeout("img_com.src = 'images/explosion.gif'",10);
	normal_state()
}

//	fail_game
//	shows fail message
function fail_game()
{
	boolTimeUp = true;
	if (intLives > 0)
	{
		lose_life();
	}
	else
	{
		endgame_lose();
	}
	show_message(3);
	div_play.style.visibility = "hidden";
//
	intMove1P = "";
	intMoveCOM = "";
	normal_state()
}

//	draw_game
//	shows draw message
function draw_game()
{
	show_message(2);
//
	intMove1P = "";
	intMoveCOM = "";
	normal_state()
}

//	lose_game
//	shows lose message
function lose_game()
{
	if (intLives > 0)
	{
		lose_life();
	}
	else
	{
		endgame_lose();
	}
	show_message(1);
//
	intMove1P = "";
	intMoveCOM = "";
	normal_state()
}

//	show_message
//	a big fat speech bubble, messages set in the array arrMessages_XX
function show_message()
{
	var arrTempArray = eval("arrMessages_" + strLanguage);
	//alert(arrTempArray[arguments[0]])
	div_debug.innerHTML += "<br>" + arrTempArray[arguments[0]];
	img_text.src = "images/text_" + arrMessages[arguments[0]] + "_" + strLanguage + ".gif"
	div_text.style.visibility="visible";
	setTimeout("div_text.style.visibility='hidden'",1000)
}

//	endgame_win
//	beat all the opponents, have lots of cash
function endgame_win()
{
	//show_message(4);
}

//	endgame_lose
//	lose 3 matches, this is the end for you!
function endgame_lose()
{
	//show_message(5);
}

//	normal_state
//	put the graphics back to normal
function normal_state()
{
	//setTimeout("img_1p.src = \"images/1p_neutral.gif\"",2000)
	//setTimeout("img_com.src = \"images/1p_neutral.gif\"",2000)
	setTimeout("img_1p.src = 'images/game_player_anim_'+intLives+'.gif'",2000);
	setTimeout("img_com.src = 'images/game_opponent'+intLevel+'_anim.gif'",2000);
	setTimeout("img_robot_1p.src='images/game_robot.gif';",2000);
	setTimeout("img_robot_com.src='images/game_robot.gif';",2000);
	if (intLives > 0)
	{
		timerComputer = setTimeout("computer_play()",3000);
	}
	else
	{
		setTimeout("game_over();",2000);
	}
}

//	computer_play
//	choose random move and start timer
function computer_play()
{
	//show_message(6);
	var intTempRandom = Math.round(Math.random()*(2));
	var arrTempMoves = eval("arrMoveNames_" + strLanguage);
	var strTempCOM = arrTempMoves[intTempRandom];
	intMoveCOM = intTempRandom;
//	div_debug.innerHTML += "<br>" + strTempCOM;
	timerComputer = setTimeout("fail_game()",intOpponentTimer);
	boolTimeUp = false;
	div_grid.style.visibility = "hidden";
	div_play.style.visibility = "visible";
	div_start.style.visibility = "hidden";
}

//	player_play
//	play a paper / scissor / stone
//		arg. index of arrMoveNames_xx array
function player_play()
{
	if (!boolTimeUp)
	{
		div_play.style.visibility="hidden";
		//div_play.style.visibility = "hidden";
		clearTimeout(timerComputer);
		intMove1P = arguments[0];
		img_com.src = "images/game_opponent" + intLevel + "_move_" + intMoveCOM + ".gif";
		img_1p.src = "images/game_player_move_" + arguments[0] + ".gif";
		img_robot_1p.src="images/game_robot_move_" + intMove1P + ".gif";
		img_robot_com.src="images/game_robot_move_" + intMoveCOM + ".gif";
		who_won();
		boolTimeUp = true;
	}
}

//	who_won
//	this is the rule book
function who_won()
{
	if (intMove1P == 0)
	{
		if (intMoveCOM == 0)
		{
			draw_game();
		}
		else if (intMoveCOM == 1)
		{
			lose_game();
		}
		else if (intMoveCOM == 2)
		{
			win_game();		
		}
	}
	else if (intMove1P == 1)
	{
		if (intMoveCOM == 0)
		{
			win_game();
		}
		else if (intMoveCOM == 1)
		{
			draw_game();
		}
		else if (intMoveCOM == 2)
		{
			lose_game();		
		}
	}
	else if (intMove1P == 2)
	{
		if (intMoveCOM == 0)
		{
			lose_game();
		}
		else if (intMoveCOM == 1)
		{
			win_game();
		}
		else if (intMoveCOM == 2)
		{
			draw_game();		
		}
	}
}

//	update_scores
//	updates the yen and level counters
function update_scores()
{
	div_score.innerHTML = intMoney;
	div_level.innerHTML = intLevel;
	//div_lives.innerHTML = "lives&nbsp;" + intLives;
	img_lives.src = "images/game_lives_" + intLives + ".gif";
}

//	switch_language
//	toggle between ENglish and JaPanese display
function switch_language()
{
	strLanguage = (strLanguage != "EN")?"EN":"JP";
	//	now switch the relevant graphics
//	img_title.src = "images/dummy_language_" + strLanguage + ".gif"
	img_lang.src = "images/game_lang_" + strLanguage + ".gif";
	img_start.src = "images/msg_start_" + strLanguage + ".gif"
	img_play.src = "images/game_play_" + strLanguage + ".gif"
	img_gameover.src = "images/msg_gameover_" + strLanguage + ".gif"
	img_win.src = "images/msg_win_" + strLanguage + ".gif"
}

//	game_over
//
function game_over()
{
	boolPlaying=false;
	div_play.style.visibility="hidden";
//	show_message(7);
	div_grid.style.visibility = "visible";
	div_gameover.style.visibility="visible";
}

//	reload page
//
function reload()
{
	window.location=window.location;
}
