/*
 * Some Game v1.0
 * Ryan Gilfether
 * hotrodder@rocketmail.com
 * http://rgilfether.tripod.com
 *
 * This code is covered by the GPL. See
 * the README file included with the source
 * for more information.
 */

function Game()
{
	// keep track of the positions each card is in.
	// the first array element is the blank space
   	this.cards = [9,1,2,3,4,5,6,7,8];
	this.blank = document.getElementById('0');
	this.count_text = document.getElementById('count_text');
	this.counter = 0;

	this.Click = Click;
	this.DoMove = DoMove;
	this.Shuffle = Shuffle;
	this.Random = Random;
	this.Directions = Directions;
}

// moves the card clicked on if allowed
function Click(num)
{
	if(this.DoMove(num))
	{
		// which card was clicked on?
		card = eval("document.getElementById('" + num + "')");

		card_top = card.style.top;
		card_left = card.style.left;
		blank_top = this.blank.style.top;
		blank_left = this.blank.style.left;

		card.style.top = blank_top;
		card.style.left = blank_left;
		this.blank.style.top = card_top;
		this.blank.style.left = card_left;

		this.count_text.firstChild.nodeValue = ++this.counter;
	}
}

// determine whether or not we are able to move the card
function DoMove(num)
{
	card_pos = this.cards[num];
	blank_pos = this.cards[0];

	if(card_pos-3 == blank_pos || card_pos+3 == blank_pos)
	{
		this.cards[num] = blank_pos;
		this.cards[0] = card_pos;
		return true;
  	}

	// make sure this card in not ont the right column
	if(card_pos%3 != 0)
	{
		if(card_pos+1 == blank_pos)
		{
			this.cards[num] = blank_pos;
			this.cards[0] = card_pos;
			return true;
   		}
	}

	// make sure this card is not on the left column
	if((card_pos-1)%3 != 0)
	{
		if(card_pos-1 == blank_pos)
		{
			this.cards[num] = blank_pos;
			this.cards[0] = card_pos;
			return true;
   		}
	}

	return false;
}

// shuffle all the cards up
function Shuffle()
{
	// our pool of available positions
	pool = [1,2,3,4,5,6,7,8,9];

	for(a=0; a<=8; ++a)
	{
		temp = new Array(pool.length-1);
		rnd = this.Random(pool.length);
		this.cards[a] = pool[rnd - 1];

		// now remove the number selected from the pool of numbers
		c = 0;
		for(b=0; b<pool.length; ++b)
		{
			if(pool[b] != this.cards[a])
				temp[b-c] = pool[b];
    			else
				c = 1;
		}

		// set resized array
		pool = temp;

		switch(this.cards[a])
		{
		// Internet Explorer considers 'top' and 'left' special keywords
		// they will cause problems with the script, so we renamed them 
		// with an '_'
		case 1:
			_top = 10;
			_left = 10;
			break;

   		case 2:
			_top = 10;
			_left = 90;
			break;

   		case 3:
			_top = 10;
			_left = 170;
			break;

   		case 4:
			_top = 115;
			_left = 10;
			break;

   		case 5:
			_top = 115;
			_left = 90;
			break;

   		case 6:
			_top = 115;
			_left = 170;
			break;

   		case 7:
			_top = 220;
			_left = 10;
			break;

   		case 8:
			_top = 220;
			_left = 90;
			break;

   		case 9:
			_top = 220;
			_left = 170;
		}

		// move the card to it's position
		card = eval("document.getElementById('" + a + "')");
		card.style.top = _top + "px";
		card.style.left = _left + "px";

		this.count_text.firstChild.nodeValue = this.counter = 0;
	}
}

// create a random number between 1 and number
function Random(number)
{
	today = new Date();
	seed = today.getTime();
        seed = (seed*9301+49297) % 233280;
        rand = seed/(233280.0);
        return Math.ceil(rand*number);
}

// show or hide the directions
function Directions()
{
	d = document.getElementById('directions');
	if(d.style.visibility == "hidden")
		d.style.visibility = "visible";
  	else
		d.style.visibility = "hidden";
}
