/*
Script:
	TicTacToe.js, <http://files.justinmaier.com/ticTacToe>
Author:
	Justin Maier, <http://justinmaier.com>

License:
	MIT-style license.
*/
var TicTacToe = new Class({
	options: {
		onComplete: Class.empty,
		startingPlayer: 'x',
		xName: 'X',
		oName: 'O'
	},
	xName: 'X',
	oName: 'O',
	winnerName: null,
	xNumbers: [],
	oNumbers: [],
	currentPlayer: null,
	win: [[1,2,3],[1,5,9],[1,4,7],[2,5,8],[3,5,7],[3,6,9],[4,5,6],[7,8,9]],
	boxes: [],
	checked: [],
	initialize: function(options){
		this.setOptions(options);
		this.currentPlayer = this.options.startingPlayer;
		this.xName = this.options.xName;
		this.oName = this.options.oName;
		this.board = new Element('div',{'class':'tictactoe'}).injectInside(document.body);
		this.nameBox = new Element('div',{'class':'name'}).setHTML('x: '+this.xName+' | o: '+this.oName).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);
		this.resetButton = new Element('div',{'class':'resetButton','events':{
				'click':function(){
					this.resetBoard();
				}.bind(this)
			}
		}).setHTML('Reset').injectInside(this.board).setOpacity(0);
		for(var i=1;i<10;i++){
			this.boxes[i] = new Element('div',{'class':'block','id':"num"+i,'events':{'click':function(e){
						e = new Event(e);
						var num = e.target.id.split("num")[1];
						if(this.checked[num] != true){
							this.checked[num] = true;
							e.target.addClass('own'+this.currentPlayer);
							if(this.currentPlayer == 'x'){
								this.xNumbers.push(num);
								this.checkWin(this.xNumbers);
								this.currentPlayer = 'o';
							}else if(this.currentPlayer == 'o'){
								this.oNumbers.push(num);
								this.checkWin(this.oNumbers);
								this.currentPlayer = 'x';
							}
						}
					}.bind(this)
				}
			}).injectInside(this.board);
			this.checked[i] = false;
		}
	},
	checkWin: function(arrayCheck){
		arrayCheck = arrayCheck.sort();
		this.full = true;
		this.win.each(function(numberSet){
			var winner = this.array_compare(arrayCheck, numberSet, false);
			if(winner == true){
				if(arrayCheck == this.xNumbers.sort()){this.winnerName=this.xName;}
				else{this.winnerName=this.oName;}
				this.winnerLogo.setHTML('<h1>'+this.winnerName+' Wins!</h1><p>click here to close the window</p>')
				this.winnerLogo.effect('opacity').start(0.9);
			}
		}.bind(this));
		this.boxes.each(function(el,i){
			if(this.checked[i] == false)this.full=false;
		}.bind(this));
		if(this.full == true){
			this.resetButton.effect('opacity').start(0.9);
		}
	},
	resetBoard: function(){
		this.resetButton.effect('opacity').start(0);
		this.winnerLogo.effect('opacity').start(0);
		this.xNumbers = [];
		this.oNumbers = [];
		this.boxes.each(function(el,i){
			el.removeClass('ownx').removeClass('owno');
			this.checked[i] = false;
		}.bind(this));
	},
	array_compare: function(array1, array2, exact){
		if (array1.length<3) {
			return (false);
		}
		var mismatch = false;
		var len;
		
		if (exact && array1.length != array2.length) {
			return (false);
		} else {
			len = Math.min(array1.length, array2.length);
			for (i=0; i<len; i++) {
				if(!this.array_search(array2[i],array1)){
					mismatch = true;
					break;
				}
			}
		}
		if (mismatch == true) {
			return (false);
		} else {
			return (true);
		}
	
	},
	array_search: function(needle, haystack) {
		var i;
		var found=false;
		
		for (i=0; i<haystack.length; i++) {
			if (haystack[i]==needle) {
				found=true;
				break;
			}
		}
		if (found==true){
			return (true);
		}else{
			return (false);
		}
	}
});

TicTacToe.implement(new Events);
TicTacToe.implement(new Options);
