/*
Script:
	Hangman.js, <http://files.justinmaier.com/Hangman>
Author:
	Justin Maier, <http://justinmaier.com>

License:
	MIT-style license.
*/
var Hangman = new Class({
	options: {
		onComplete: Class.empty,
		player1Name: 'player 1',
		player2Name: 'player 2'
	},
	player1Name: 'player 1',
	player2Name: 'player 2',
	winnerName: null,
	attempts: 6,
	word: '',
	wordArray: [],
	blankWordArray: [],
	guessed: [],
	initialize: function(options){
		this.setOptions(options);
		this.player1Name = this.options.player1Name;
		this.player2Name = this.options.player2Name;
		this.board = new Element('div',{'class':'hangman'}).injectInside(document.body);
		this.nameBox = new Element('div',{'class':'name'}).setHTML('Hanger: '+this.player1Name+' | Guesser: '+this.player2Name).injectInside(this.board);
		this.hangman = new Element('div',{'class':'picture'}).injectInside(this.board);
		this.lines = new Element('div',{'class':'lines'}).injectInside(this.board);
		this.guessedbox = new Element('div',{'class':'guessedBox'}).injectInside(this.board);
		this.guessbox = new Element('input',{'class':'guessBox','events':{
				'change':function(){
					if(this.guessbox.value != ''){
						var letter = this.guessbox.value;
						this.guessbox.value = this.guessbox.value.substring(1);
						this.guess(letter.split('')[0]);
					}
				}.bind(this)
			}
		}).injectInside(this.board);
		this.winnerLogo = new Element('div',{'class':'winnerLogo','events':{
				'click':function(){
					this.options.onComplete(this.winnerName);
					this.board.remove();
				}.bind(this)
			}
		}).injectInside(this.board).setOpacity(0);
		while(this.word == ''){
			this.word = prompt(this.player1Name+', please enter a word, dont let '+this.player2Name+' see.','Enter your word here');
		}
		this.wordArray = this.word.split("");
		this.wordArray.each(function(char){
			if(char != ' '){
				this.blankWordArray.push('_');
			}else{
				this.blankWordArray.push(' ');
			}
		}.bind(this));
		this.update();
	},
	update: function(){
		this.lines.setHTML(this.blankWordArray.join('&nbsp;'));
		this.hangman.removeClass('status'+this.attempts+1).addClass('status'+this.attempts);
		if(this.blankWordArray.indexOf('_') == -1){
			this.winnerName = this.player2Name;
			this.winnerLogo.setHTML('<h1>'+this.winnerName+' Wins!</h1><p>click here to close the window</p>')
			this.winnerLogo.effect('opacity').start(0.9);
		}
		if(this.attempts < 1){
			this.winnerName = this.player1Name;
			this.winnerLogo.setHTML('<h1>'+this.winnerName+' Wins!</h1><p>click here to close the window</p>')
			this.winnerLogo.effect('opacity').start(0.9);
		}
		
	},
	guess: function(letter){
		if(letter!=''&&this.guessed.indexOf(letter.toLowerCase()) == -1){
			this.guessed.push(letter.toLowerCase());
			var goodGuess = false;
			var guess = new Element('span').setHTML(letter.toLowerCase()).injectInside(this.guessedbox);
			this.wordArray.each(function(char,i){
				if(letter.toLowerCase() == char.toLowerCase()){
					goodGuess = true;
					this.blankWordArray[i] = char;
				}
			}.bind(this));
			if(goodGuess == false){
				this.attempts--;
				guess.addClass('badGuess');
			}
			this.update();
		}
		if(this.guessbox != ''){
			this.guessbox.fireEvent('change');
		}
	}
});

Hangman.implement(new Events);
Hangman.implement(new Options);
