var gameServerUrl = "/server/"

var UpDownGameServer = new Class({
	 gameServerUrl: null
	,udGame: null

	,initialize: function(upDownGame){
		this.gameServerUrl = gameServerUrl;
		this.upDownGame = upDownGame;
	}
	
	,send: function(action, options) {
		action = "a="+action;
		var onComplete;
		if(options){
			action = [action, options.query].join('&');
			if(options.onComplete){
				onComplete = function(response){options.onComplete(JSON.parse(response))}.bind(this.upDownGame);
			}
		}
		var ajaxRequest =  {method: 'get'};
		if(onComplete)ajaxRequest.onComplete = onComplete;
		new Ajax(gameServerUrl+"?"+action, ajaxRequest).request();
	}
	
	,showServerStatus: function() {
		this.send('status', {onComplete: function(response){alert(response)}});
	}
	,loadGame: function(size, gameId, completeAction) {
		var queryList = [];
		if(size) queryList.push('size=' + size);
		if(gameId) queryList.push('game=' + gameId);
		if(queryList.length){
			this.send('load', {query: queryList.join('&'), onComplete: completeAction});
		}else{
			this.send('load', {onComplete: completeAction});
		}
	}
	,startGame: function() {
		this.send('start', {onComplete: function(response){
			// game started, report errors
		}});
	}
	,submitGame: function(moves, submitAction) {
		this.send('submit', {query: 'moves='+moves, onComplete: submitAction});
	}
	,voteGameImpossible: function() {
		this.send('voteimpossible', {onComplete: function(response){
			alert('Your vote has been recorded, please play one of the other sizes. There will be new games each day.');
		}});
	}
});