var JanKen = Class.create(
{
	// game configuration
	interval: 300,		// interval speed in milliseconds
	
	// game status variables
	types: [],
	type: null,
	index: 0,
	janKenYou: null,
	janKenMe: null,
	timer: null,
	target: null,
	
	initialize: function(targetElem)
	{
		this.types[0] = 'rock';
		this.types[1] = 'paper';
		this.types[2] = 'scissors';
		this.target = targetElem;
		$(this.target).update('<span></span> <span></span>');
		$(this.target).show();
		this.janKenMe = $(targetElem).firstChild.identify();
		this.janKenYou = $(targetElem).lastChild.identify();
		$(this.janKenYou).style.cursor = 'pointer';
		this.loopJanKen();
		this.timer = window.setInterval("janKenGame.loopJanKen()", this.interval);
		this.type = Math.floor(Math.random()*3);
		$(this.janKenMe).update('<img class="janken" src="images/janken_'+this.types[this.type]+'.jpg" alt="JanKenPon" width="40" height="40" />');
	},
	
	loopJanKen: function()
	{
		this.index = Math.floor(Math.random()*3);
		$(this.janKenYou).update('<img class="janken" onmousedown="janKenGame.stop('+this.index+');" src="images/janken_'+this.types[this.index]+'.jpg" alt="JanKenPon" width="40" height="40"/>');
	},
	
	stop: function(index)
	{
		soundManager.stopAll();
		if( (0 == this.type && 1 == index)		// pc rock
			|| (1 == this.type && 2 == index)	// pc paper
			|| (2 == this.type && 0 == index))
		{
			soundManager.play('puchiDone');
			$(this.target).update('<h1>中吉</h1>');
		}
		else
		{
			soundManager.play('puchiFail');
			$(this.target).update('<h1>大凶</h1>');
		}
		window.clearInterval(this.timer);
		$('restart').update('<br /><br /><br /><br /><a href="index.php" onclick="puchiGame = new PuchiGame(); return false;">試みの新しいゲーム </a>');
		$('restart').show();
	}
	
});
