//Image Preloading
var hang_img = new Array;

//Letters
hang_img[hang_img.length] = "images/content/letters/_.gif";
hang_img[hang_img.length] = "images/content/letters/a.gif";
hang_img[hang_img.length] = "images/content/letters/b.gif";
hang_img[hang_img.length] = "images/content/letters/c.gif";
hang_img[hang_img.length] = "images/content/letters/d.gif";
hang_img[hang_img.length] = "images/content/letters/e.gif";
hang_img[hang_img.length] = "images/content/letters/f.gif";
hang_img[hang_img.length] = "images/content/letters/g.gif";
hang_img[hang_img.length] = "images/content/letters/h.gif";
hang_img[hang_img.length] = "images/content/letters/i.gif";
hang_img[hang_img.length] = "images/content/letters/j.gif";
hang_img[hang_img.length] = "images/content/letters/k.gif";
hang_img[hang_img.length] = "images/content/letters/l.gif";
hang_img[hang_img.length] = "images/content/letters/m.gif";
hang_img[hang_img.length] = "images/content/letters/n.gif";
hang_img[hang_img.length] = "images/content/letters/o.gif";
hang_img[hang_img.length] = "images/content/letters/p.gif";
hang_img[hang_img.length] = "images/content/letters/q.gif";
hang_img[hang_img.length] = "images/content/letters/r.gif";
hang_img[hang_img.length] = "images/content/letters/s.gif";
hang_img[hang_img.length] = "images/content/letters/t.gif";
hang_img[hang_img.length] = "images/content/letters/u.gif";
hang_img[hang_img.length] = "images/content/letters/v.gif";
hang_img[hang_img.length] = "images/content/letters/w.gif";
hang_img[hang_img.length] = "images/content/letters/x.gif";
hang_img[hang_img.length] = "images/content/letters/y.gif";
hang_img[hang_img.length] = "images/content/letters/z.gif";

//Stages/General Images
hang_img[hang_img.length] = "images/content/hangman/eqn1.gif";
hang_img[hang_img.length] = "images/content/hangman/stage1.gif";
hang_img[hang_img.length] = "images/content/hangman/stage2.gif";
hang_img[hang_img.length] = "images/content/hangman/stage3.gif";
hang_img[hang_img.length] = "images/content/hangman/stage4.gif";
hang_img[hang_img.length] = "images/content/hangman/stage5.gif";
hang_img[hang_img.length] = "images/content/hangman/stage6.gif";
hang_img[hang_img.length] = "images/content/hangman/stage7.gif";

//Styles - Blue
hang_img[hang_img.length] = "images/blue/hangman/bg.jpg";
hang_img[hang_img.length] = "images/blue/hangman/button_instruction.gif";
hang_img[hang_img.length] = "images/blue/hangman/button_instruction_over.gif";
hang_img[hang_img.length] = "images/blue/hangman/button_start.gif";
hang_img[hang_img.length] = "images/blue/hangman/button_start_over.gif";
hang_img[hang_img.length] = "images/blue/hangman/button_restart.gif";
hang_img[hang_img.length] = "images/blue/hangman/button_restart_over.gif";
hang_img[hang_img.length] = "images/blue/hangman/button_next.gif";
hang_img[hang_img.length] = "images/blue/hangman/button_next_over.gif";

//Styles - Green
hang_img[hang_img.length] = "images/green/hangman/bg.jpg";
hang_img[hang_img.length] = "images/green/hangman/button_instruction.gif";
hang_img[hang_img.length] = "images/green/hangman/button_instruction_over.gif";
hang_img[hang_img.length] = "images/green/hangman/button_start.gif";
hang_img[hang_img.length] = "images/green/hangman/button_start_over.gif";
hang_img[hang_img.length] = "images/green/hangman/button_restart.gif";
hang_img[hang_img.length] = "images/green/hangman/button_restart_over.gif";
hang_img[hang_img.length] = "images/green/hangman/button_next.gif";
hang_img[hang_img.length] = "images/green/hangman/button_next_over.gif";

//Styles - Yellow
hang_img[hang_img.length] = "images/yellow/hangman/bg.jpg";
hang_img[hang_img.length] = "images/yellow/hangman/button_instruction.gif";
hang_img[hang_img.length] = "images/yellow/hangman/button_instruction_over.gif";
hang_img[hang_img.length] = "images/yellow/hangman/button_start.gif";
hang_img[hang_img.length] = "images/yellow/hangman/button_start_over.gif";
hang_img[hang_img.length] = "images/yellow/hangman/button_restart.gif";
hang_img[hang_img.length] = "images/yellow/hangman/button_restart_over.gif";
hang_img[hang_img.length] = "images/yellow/hangman/button_next.gif";
hang_img[hang_img.length] = "images/yellow/hangman/button_next_over.gif";

preload_img(hang_img);
var letters = new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");

//Words
var text = new Array;

text[0] = new Array;
text[0][1] = "Geometry";
text[0][2] = "A branch of mathematics concerned with the properties and relationships of lines, angles, curves and shapes.";

text[1] = new Array;
text[1][1] = "Algebra";
text[1][2] = "A type of mathematics in which letters are used to represent possible quantities";

text[2] = new Array;
text[2][1] = "Algorithm";
text[2][2] = "A series of mathematical steps, especially in a computer programme, which will give you the answer to a particular kind of problem or question.";

text[3] = new Array;
text[3][1] = "Fraction";
text[3][2] = "A number that can be expressed as a proportion of two whole numbers.";

text[4] = new Array;
text[4][1] = "Equation";
text[4][2] = "A mathematical statement saying that two amounts or values are the same.";

text[5] = new Array;
text[5][1] = "Elements";
text[5][2] = "A series of books on math written by Euclid";

text[6] = new Array;
text[6][1] = "Archimedes";
text[6][2] = "A mathmatician who defined &Pi; as <img src='images/content/hangman/eqn1.gif' alt='Equation' />.";

text[7] = new Array;
text[7][1] = "Euclid";
text[7][2] = "Mathematician who wrote the Elements.";

text[8] = new Array;
text[8][1] = "Fibonacci"
text[8][2] = "An Italian mathematician who traveled widely all over the world on business trips.";

text[9] = new Array;
text[9][1] = "Babbage";
text[9][2] = 'An English mathematician who is known as "father of computing".';

text[10] = new Array;
text[10][1] = "Pythagoras";
text[10][2] = 'A Greek mathematician who is known as "father of numbers".';

text[11] = new Array;
text[11][1] = "Arithmetic";
text[11][2] = "The part of mathematics that is concerned with the addition, subtraction, multiplication, and division of numbers.";

var inGame = false;
var inWord = false;
var lifes = 0;
var currentGuess = new Array;
var gameArray, currentWord;

function startGame(){
    inWord = false;
    inGame = true;
    lifes = 7;
    //Buttons
    get_itm("hangman_butt_start").style.display = "none";
    get_itm("hangman_butt_restart").style.display = "block";
    get_itm("hangman_butt_next").style.display = "block";
    
    //DIV
    get_itm("hangman_hang").style.backgroundImage = "url(images/content/hangman/stage1.gif)";
    
    get_itm("hangman_clue").style.display = "block";
    get_itm("hangman_word").style.display = "block";
    get_itm("hangman_buttons").style.display = "block";
    
    gameArray = text;
    nextWord();
}

function nextWord(){
    if (inWord){
        if (confirm("Are you sure you want to skip this word and continue? You will lose a life!")){
            minusLife();
            if (lifes <= 1){
                return;
            }
        }
        else{
            return;
        }
    }
    inWord = true;
    currentGuess = new Array;
    if (gameArray.length == 0){
        finishGame();
    }
    else{
        var rand = Math.round(Math.random()*(gameArray.length-1));
        var subject = gameArray[rand];
        //Remove Element
        gameArray.splice(rand,1);
        get_itm("hangman_status").innerHTML = "Guess the word!";
        for (var i = 0; i <= letters.length-1; i++){
            get_itm("butt_" + letters[i]).disabled = false;
            get_itm("butt_" + letters[i]).style.display = "inline";
        }
        
        currentWord = subject[1].toLowerCase();
        get_itm("hangman_clue").innerHTML = subject[2];
        
        get_itm("hangman_word").innerHTML = "";
        var splitString = currentWord.split('');
        for (var i = 0; i <= splitString.length-1; i++){
            currentGuess[i] = false;
            get_itm("hangman_word").innerHTML += ' <img src="images/content/letters/_.gif" alt="_" /> ';
        }
    }
    
}

function minusLife(){
    lifes--;
    var stage = 7-lifes+1;
    stage += "";
    get_itm("hangman_hang").style.backgroundImage = "url(images/content/hangman/stage"+ stage +".gif)";    
    if (lifes <= 1){
        alert("You have used up all your lives! Press 'Restart' to restart a new game.");
        finishGame();
        return;
    }
}
function chooseLetter(letter){
    if (!inWord){
        return;
    }
    var correct = false;
    for (var i = 0; i <= currentGuess.length - 1; i++){
        if (letter == getLetter(currentWord,i+1)){
            correct = true;
            currentGuess[i] = true;
        }
    }
    get_itm("butt_" + letter).disabled = "disabled";
    get_itm("butt_" + letter).style.display = "none";
    if (correct){
        get_itm("hangman_word").innerHTML = "";
        var all_correct = true;
        get_itm("hangman_status").innerHTML = "'" + letter.toUpperCase() + "' is correct!";
        
        for (var i = 0; i <= currentGuess.length - 1; i++){
            if (currentGuess[i] == true){
                get_itm("hangman_word").innerHTML += ' <img src="images/content/letters/'+getLetter(currentWord,i+1)+'.gif" alt="'+getLetter(currentWord,i+1)+'" /> ';
            }
            else{
                all_correct = false;
                get_itm("hangman_word").innerHTML += ' <img src="images/content/letters/_.gif" alt="_" /> ';
            }
        }
        
        if (all_correct){
            get_itm("hangman_status").innerHTML = "You've gotten the word! Click 'Next' to continue.";
            for (var i = 0; i <= letters.length-1; i++){
                get_itm("butt_" + letters[i]).disabled = "disabled";
                get_itm("butt_" + letters[i]).style.display = "none";
            }
            inWord = false;
        }
    }
    else{
        minusLife();
        if (lifes <= 1){
            return;
        }
        get_itm("hangman_status").innerHTML = "'" + letter.toUpperCase() + "' is wrong.";
    }
}

function getLetter(word,nth){
    return word.substr(nth-1,1);
}

//Lose or win
function finishGame(){
    inGame = false;
    for (var i = 0; i <= letters.length-1; i++){
        get_itm("butt_" + letters[i]).disabled = "disabled";
        get_itm("butt_" + letters[i]).style.display = "none";
    }
    get_itm("hangman_butt_next").style.display = "none";
    if (lifes <= 1){
        get_itm("hangman_status").innerHTML = "You have used up all your lives! Press 'Restart' to restart a new game.";
        get_itm("hangman_word").innerHTML = "";
        for (var i = 0; i <= currentGuess.length - 1; i++){
            get_itm("hangman_word").innerHTML += ' <img src="images/content/letters/'+getLetter(currentWord,i+1)+'.gif" alt="'+getLetter(currentWord,i+1)+'" /> ';
        }
    }
    else{
        alert('Congratulations! You have won the game!');
        get_itm("hangman_status").innerHTML = "Congratulations! You have won the game!";
    }
}

function restartGame(){
    if (inGame){
        if(!confirm("Are you sure you want to restart your uncompleted game?")){
            return;
        }
        else{
            self.location = "hangman.htm?start=true";
        }
    }
    else{
        self.location = "hangman.htm?start=true";
    }
}

function closeInstruction(){
    get_itm("hangman_instruction").style.display = "none";
}
function showInstruction(){
    get_itm("hangman_instruction").style.display = "block";
}

/* Weird Opera Bug */
function hangmanOpera(){
    if (isOpera){
        alert("Due to a weird bug present in some versions of Opera, you might have problems playing this game. We aplogise for any problems caused.");
    }
}