var xhr = new XMLHttpRequest();
xhr.onreadystatechange = XHRCallback;
var lastcall, time, playerId, gameId;
var THE_COLOR;
var connected = false;
var requestPlayerlist = false;
var players = [];
var PlayerAvatar = { name: "DEFAULT"};

function connectNew(name, color){	
	time = (new Date()).getTime();
	xhr.open("POST", "/connect/", true);
	xhr.send("c " + name + " " + color + " \n");	
	PlayerAvatar.name = name;
	THE_COLOR = color;
	lastcall = "connectNew";
}

function connectExists(name, color, gi){	
	time = (new Date()).getTime();
	xhr.open("POST", "/connect/", true);

	xhr.send("cc " + name + " " + color + " " + gi + " \n");
	PlayerAvatar.name = name;
	gameId = gi;
	
	lastcall = "connectExists";
}

function update(positionx, positiony, velocityx, velocityy){
	time = (new Date()).getTime();
	xhr.open("POST", "/connect/", true);
	xhr.send("u " + gameId + " " + playerId + " "
				  + positionx + " " + positiony + " "  
				  + velocityx + " " + velocityy + " \n");
				  
	lastcall = "update";
}

function requestList(){
	time = (new Date()).getTime();
	xhr.open("POST", "/connect/", true);
	xhr.send("r " + gameId + " " + playerId + " \n");
	
	lastcall = "request";
}
 
function XHRCallback(){
	if(xhr.readyState != 4){
		return;
	}
	
	document.getElementById("ping").innerHTML = "Ping: " + ((new Date()).getTime() - time) + " Room#: " + gameId;
	var commands = xhr.responseText.split(" ");
	
	if(lastcall == "connectNew"){
		lastcall = "";
		if(commands[0] == "1"){
			connected = true;
			gameId = parseInt(commands[1]);
			playerId = parseInt(commands[2]);			
			players.push(PlayerAvatar);
		}else{
			alert("Connection failed!!");
		}
	}
	
	if(lastcall == "connectExists"){
		lastcall = "";
		if(commands[0] == "1"){
			connected = true;
			playerId = parseInt(commands[1]);
			for(i = 3; i < 6 * parseInt(commands[2]); i += 6){
				if(playerId != players.length && commands[i] != PlayerAvatar.name){
					players.push(new Avatar(commands[i],
											[parseInt(commands[i+2]), //posx
											parseInt(commands[i+3]),  //posy
											130.0], 										 //posz
											parseInt(commands[i+1]),  //col
					new THREE.CubeGeometry( 2, 4, 1, 1, 1, 1, undefined, 0, undefined),	new THREE.MeshLambertMaterial( {color: 0x00FFFF} )
											));
					
					players[players.length - 1].getMesh().scale.x = 10;
					players[players.length - 1].getMesh().scale.y = 10;
					players[players.length - 1].getMesh().scale.z = 10;
					scene.addObject(players[players.length - 1].getMesh());
				}
				
			}	
			
		}else{
		}
	}	
		
	if(lastcall == "update"){	
	lastcall = "";
		if(commands[0] == "1"){
			if(parseInt(commands[1]) > players.length){
				requestPlayerlist = true;
			}
			
			var i = 2;
			var pid = 0;
			for(var j = 0; j < players.length; j++){
				if(pid != playerId){
					players[j].getMesh().position.x = parseInt(commands[i]);
					players[j].getMesh().position.y = parseInt(commands[i+1]);
					players[j].getMesh().position.z = 132;
				}
				pid++;
				i+=4;
			}
			
		}else{
			}
	}
	
	if(lastcall == "request"){
	lastcall = "";
		if(commands[0] == "1"){
			var disparity = (parseInt(commands[1]) - players.length);
			
			var i = 2 + 7 * disparity;
			for(var j = 0; j <= disparity; j++){
				if(playerId != players.length){
				players.push(new Avatar(commands[i+1],
				[parseInt(commands[i+3]),parseInt([i+4]),130.0],
							parseInt(commands[i+2]),
							new THREE.CubeGeometry( 2, 4, 1, 1, 1, 1, undefined, 0, undefined),
							new THREE.MeshLambertMaterial({color: 0x00ffff})
				));
				
					players[players.length - 1].getMesh().scale.x = 10;
					players[players.length - 1].getMesh().scale.y = 10;
					players[players.length - 1].getMesh().scale.z = 10;
					scene.addObject(players[players.length - 1].getMesh());
				}
				i += 7;				
			}			
		}		
	}
}

