/**
 * @author thombomb
 * eric: l33tosaurus
 */
var indexController = {
	initialize: function(){
		window.onload = this.loadHandler.bind(this); 
	},
	loadHandler: function(){
		this.indexView = new IndexView(this);
		this.indexModel = new IndexModel(this);
	},
	sendMove: function(x,y){
		this.indexModel.sendMove(x,y);
	},
	updateView: function(boardArray){
		this.indexView.updateView(boardArray);
	}
}
var http_request;
var IndexView = Class.create({
	initialize: function(indexController){
		this.indexController = indexController;
		
		var tableWidth = 10;
		var tableHeight = 10;
		var table = document.createElement("table");
			for(var i=0, iFinal = tableHeight;i<iFinal;i++){
				var row = document.createElement("tr");
				for(var j=0, jFinal = tableWidth;j<jFinal;j++){
					var cell = document.createElement("td");
					cell.setAttribute("id","cell_"+j+"_"+i);
					cell.setAttribute("x",j);
					cell.setAttribute("y",i);
					cell.setAttribute("class","board_cell");
					cell.onclick = this.handle_click.bindAsEventListener(this,j,i);
					row.appendChild(cell);
				}
				table.appendChild(row);
			}
		document.getElementsByTagName("body")[0].appendChild(table);
		var div = document.createElement("div");
		div.setAttribute("id","status");
		document.getElementsByTagName("body")[0].appendChild(div);
	},
	handle_click: function(event,x,y){
		this.indexController.sendMove(x,y);
	},
	updateView: function(board){
		var reds = 0;
		for(var i=0,iFinal=board.length;i<iFinal;i++){
			for(var j=0,jFinal=board[i].length;j<jFinal;j++){
				if (board[i][j]==1) reds++;
				var className = (board[i][j] == 0) ? "board_cell board_self" : "board_cell board_enemy";
				document.getElementById("cell_"+j+"_"+i).setAttribute("class",className);
			}
		}
		document.getElementById("status").innerHTML = "<span style=\"color: red; font-weight: bold; font-size: 35px;\">" + reds + "</span><br/>\n" +
														"<span style=\"color: blue; font-weight: bold; font-size: 35px;\">" + ((board.length*board[0].length)-reds) + "</span>\n";
	}
});
var IndexModel = Class.create({
	fullResponse: "",
	initialize: function(indexController){
		this.indexController = indexController;
		var game_height = 10;
		var game_width = 10;
		this.length = 0;
		this.reds = game_height * game_width / 2;
		this.lightBoxes = new Array(game_height);
		for(var i=0,iFinal=game_height;i<iFinal;i++){
			this.lightBoxes[i] = new Array(game_width);
		}
		this.sendMove(-1,-1);
		//this.tempSetup();
	},
	sendMove: function(xMove,yMove){
		if (http_request)
			http_request.abort();
		http_request=this.GetXmlHttpObject();
		var url="push.php";
			url=url+"?token="+tokenID;
			url=url+"&update";
			url=url+"&y="+xMove;
			url=url+"&x="+yMove;
			url=url+"&ignore="+Math.random();
		http_request.onreadystatechange=this.handle_change.bind(this);
		http_request.open("GET",url,true);
		this.length = 0;
		http_request.send(null);
	},
	GetXmlHttpObject: function(){
		var xmlHttp = null;
		try {
			// Firefox, Opera 8.0+, Safari
			xmlHttp = new XMLHttpRequest();
		} 
		catch (e) {
			// Internet Explorer
			try {
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			} 
			catch (e) {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		return xmlHttp;
	},
	handle_change: function(){
		if (http_request.readyState == 3) {
			var response = http_request.responseText.substring(this.length);
			this.length = http_request.responseText.length;
			var lines = response.split("\n");
			for (var i=0; i<lines.length; i++)
			{
				var elts = lines[i].split(",");
				if (elts[0]!='')
				{
					//console.log(elts[0] + "," + elts[1] + "=" + elts[2]);
					this.setOwner(elts[0], elts[1], elts[2]);
				}
			}
		}
	},
	setOwner: function(x,y,owner){
		this.lightBoxes[x][y] = owner;
		this.indexController.updateView(this.lightBoxes);
	},
	tempSetup: function(){
		var self_counter = 50;
		for(var i=0,iFinal=10;i<iFinal;i++){
			for(var j=0,jFinal=10;j<jFinal;j++){
				if(self_counter>0){
					this.lightBoxes[i][j] = 0;
				}
				else{
					this.lightBoxes[i][j] = 1;
				}
				self_counter--;
			}
		}
		this.indexController.updateView(this.lightBoxes);
	}
});
indexController.initialize();

