<!--
//This varible ensures that if the user clicks a square while a previous click
//is being processed, nothing happens.
var computing = false;

//Initialization variable
var initialized = false;

//Game over when done is set to 1!
var done = false;

//Defend flag. Should be set using a button. Browser defends itself!
var defend = true;

//Attack flag. The browser looks for obvoius winning moves.
var attack = true;

//Strategy flag. The browser looks for best move when attack or
//defensive moves are not available. TODO.
var strategy = true;

//These variables make the browser 'O' and the human 'X'. TODO: Implement
//a way to switch sides. Probably by using a button.
//Begin Side Variables
var browserType = 'O';
var humanType   = 'X';

var brwsrImg    = "O.jpg";
var humanImg    = "X.jpg";
var blankImg    = "blank.jpg";
//End Side Variables

var gameSquares = new Array( 9 );

function reFresh() {
    initialized = false;

    initialize();
}

function browserStarts() {
    initialized = false;
    initialize();
    
    browserType = 'X';
    humanType   = 'O';
    brwsrImg    = "X.jpg";
    humanImg    = "O.jpg";
    
    computing = true;    
    browserMoves();
    computing = false;
}

//Begin object oneSquare code
function oneSquare(number, type, tname)
{
    this.square = number;
    this.type   = type;
    this.tagname   = tname;
    //Functions
    //this.hasBeenClicked;
    //this.setSquare;
}
//End object oneSquare code

function gotAWin ( anum, bnum, cnum )
{
    var asqr = gameSquares[anum];
    var bsqr = gameSquares[bnum];
    var csqr = gameSquares[cnum];

    var atyp = asqr.type;
    var btyp = bsqr.type;
    var ctyp = csqr.type;

    if ( atyp == '' )
        return false;

    if ( (atyp == btyp) && (btyp == ctyp) )
    {
        //One side won!
        var theEnd = "Got a match on [" + anum + "][" + bnum + "][" + cnum + "]!";
        var moreData = "[" + atyp + "][" + btyp + "][" +ctyp +"]. Game over!";
        theEnd += moreData;

        var img;
        if ( atyp == 'X' )
            img = 'X_win.jpg';
        else
            img = 'O_win.jpg';

        document.images[asqr.tagname].src = img;
        document.images[bsqr.tagname].src = img;
        document.images[csqr.tagname].src = img;

        done = true;
        alert(theEnd);
        return true;
    }
    return false;
}

function checkVictory()
{
    if ( gotAWin (0, 1, 2) )
        return true;
    if ( gotAWin (0, 3, 6) )
        return true;
    if ( gotAWin (3, 4, 5) )
        return true;
    if ( gotAWin (6, 7, 8) )
        return true;
    if ( gotAWin (1, 4, 7) )
        return true;
    if ( gotAWin (2, 5, 8) )
        return true;
    if ( gotAWin (0, 4, 8) )
        return true;
    if ( gotAWin (2, 4, 6) )
        return true;

    return false;
}

//Helper function
function compareNeighbors ( num1, num2, ofType )
{
    var sqA;
    var sqB;
    var typea = '';
    var typeb = '';

    sqA = gameSquares[num1];
    sqB = gameSquares[num2];
    typea = sqA.type;
    typeb = sqB.type;

    if ( ( typea == typeb ) && ( typeb == ofType ) )
        return true;

    return false;
}

//Check the neighbors!!
//If value is 1 look for winning moves, else look for defending moves
function checkLines ( move, value )
{
    //If we are attacking, all three will be browser type after the move.
    var forChk = browserType; 

    //If we are defending, all three will be human type after the move.
    if ( value != 1 )
        forChk = humanType; 
        
    switch (move)
    {
        case 0:
        {
            if ( compareNeighbors ( 1, 2, forChk) ) return true;
            if ( compareNeighbors ( 3, 6, forChk) ) return true;
            if ( compareNeighbors ( 4, 8, forChk) ) return true;
            break;
        }
        case 1:
        {
            if ( compareNeighbors ( 0, 2, forChk) ) return true;
            if ( compareNeighbors ( 4, 7, forChk) ) return true;
            break;
        }
        case 2:
        {
            if ( compareNeighbors ( 0, 1, forChk) ) return true;
            if ( compareNeighbors ( 4, 6, forChk) ) return true;
            if ( compareNeighbors ( 5, 8, forChk) ) return true;
            break;
        }
        case 3:
        {
            if ( compareNeighbors ( 0, 6, forChk) ) return true;
            if ( compareNeighbors ( 4, 5, forChk) ) return true;
            break;
        }
        case 4:
        {
            if ( compareNeighbors ( 0, 8, forChk) ) return true;
            if ( compareNeighbors ( 2, 6, forChk) ) return true;
            if ( compareNeighbors ( 3, 5, forChk) ) return true;
            if ( compareNeighbors ( 1, 7, forChk) ) return true;
            break;
        }
        case 5:
        {
            if ( compareNeighbors ( 2, 8, forChk) ) return true;
            if ( compareNeighbors ( 3, 4, forChk) ) return true;
            break;
        }
        case 6:
        {
            if ( compareNeighbors ( 0, 3, forChk) ) return true;
            if ( compareNeighbors ( 2, 4, forChk) ) return true;
            if ( compareNeighbors ( 7, 8, forChk) ) return true;
            break;
        }
        case 7:
        {
            if ( compareNeighbors ( 1, 4, forChk) ) return true;
            if ( compareNeighbors ( 6, 8, forChk) ) return true;
            break;
        }
        case 8:
        {
            if ( compareNeighbors ( 0, 4, forChk) ) return true;
            if ( compareNeighbors ( 2, 5, forChk) ) return true;
            if ( compareNeighbors ( 6, 7, forChk) ) return true;
            break;
        }
        default:
        {
            alert("Something wrong!");
            break;
        }
    }
    return false;
}

//Function to look for obvious winning moves for the browser
//Returns true if a move is found.
function findObvious ( openSquares )
{
    var foundMove = false;
    var tag = "";
    var num = -1;

    if ( !attack )
        return foundMove;

    for ( var i = 0; i < openSquares.length; i++ )
    {
        var tile = openSquares[i];
        tag = tile.tagname;
        num = tile.square;

        foundMove = checkLines ( num, 1);    

        if ( foundMove )
        {
            //Set the winninng move.
            tile.type = browserType;
            gameSquares[num] = tile;
            document.images[tile.tagname].src = brwsrImg;
            break;
        }
    }

    return foundMove;
}

//Function to look for blocking moves for the browser
//Returns true if a move is found.
function findDefense ( openSquares )
{
    var foundMove = false;
    var tag = "";
    var num = -1;

    if ( !defend )
        return foundMove;

    for ( var i = 0; i < openSquares.length; i++ )
    {
        var tile = openSquares[i];
        tag = tile.tagname;
        num = tile.square;

        foundMove = checkLines ( num, 0);    

        if ( foundMove )
        {
            //Set the winninng move.
            tile.type = browserType;
            gameSquares[num] = tile;
            document.images[tile.tagname].src = brwsrImg;
            break;
        }
    }

    return foundMove;
}

//Select a random move :-)
//TODO: Put better AI in here
function browserMoves ( )
{
    var freeSquares = new Array();
    var j = 0;

    for ( var i = 0; i < gameSquares.length; i++)
    {
        var checkFree = gameSquares[i];

        if ( checkFree.type == '' )
        {
            freeSquares[j] = checkFree;
            j++;
        }
    }
        
    //No free squares, game over, exit stage ...
    var moves = freeSquares.length;
    if ( moves == 0 )
    {
        done = true;
        return;
    }

    //Look for obvious winning moves
    if ( findObvious( freeSquares ) )
    {
        checkVictory();
        return;
    }

    //Look for defensive moves
    if ( findDefense( freeSquares ) )
    {
        checkVictory();
        return;
    }

    //TODO: Find a way for the browser to hatch plots here. Else just choose
    //random moves :-( This level is good enough for now.

    //Compute a random move!
    var select = Math.round(Math.random()*(moves - 1));
    while ( ( select < 0 ) || ( select > (moves - 1) ) )
    {
        var mess = "Selected " + select + " for " + moves;
        alert(mess);
        select = Math.round(Math.random()*(moves - 1));
    }
        
    //Set image to browser type and update matrix
    var brwsrSqr = freeSquares[select];
    brwsrSqr.type = browserType;
    gameSquares[brwsrSqr.square] = brwsrSqr;
    document.images[brwsrSqr.tagname].src = brwsrImg;

    checkVictory();
}

//Initialize the game matrix
function initialize()
{
    if ( initialized )
        return;

    initialized = true;

    done = false;
    computing = false;

    browserType = 'O';
    humanType   = 'X';
    brwsrImg    = "O.jpg";
    humanImg    = "X.jpg";

    gameSquares[0] = new oneSquare(0, '', 'image1');
    gameSquares[1] = new oneSquare(1, '', 'image2');
    gameSquares[2] = new oneSquare(2, '', 'image3');
    gameSquares[3] = new oneSquare(3, '', 'image4');
    gameSquares[4] = new oneSquare(4, '', 'image5');
    gameSquares[5] = new oneSquare(5, '', 'image6');
    gameSquares[6] = new oneSquare(6, '', 'image7');
    gameSquares[7] = new oneSquare(7, '', 'image8');
    gameSquares[8] = new oneSquare(8, '', 'image9');

    for ( var i = 0; i < gameSquares.length; i++ )
    {
        var makeBlank = gameSquares[i];
        document.images[makeBlank.tagname].src = blankImg;
    }
}

//Process user click on a move
function userMove (num, imageTag)
{
    //Either previous square click has not processed, or game is over
    if ( computing || done )
    {
        var over = "Got computing = " + computing + " and done = " + done;
        alert(over);
        return;
    }

    computing = true;
    initialize();

    //The square has already been selected
    sqr = gameSquares[num];
    if ( sqr.type != '' )
    {
        computing = false;
        return;
    }

    //Set the display and matrix to 'X' or human type :-)
    document.images[imageTag].src = humanImg;
    sqr.type = humanType;
    gameSquares[num] = sqr;

    //Check to see if the game is over
    if ( checkVictory() )
        return;

    //Browser computes a move :-)
    browserMoves();

    computing = false;
}

function flipImage (imageTag, newSrc)
{
    document.images[imageTag].src = newSrc;
}
// -->