﻿var play =
{
	gameId: 0,
	roomTableId: 0,

	quickplay: function(gameId)
	{
		$.post(rg('Cafe_QuickPlayUrl'), { g: gameId }, play.onplayrequest, 'text');
		return false;
	},
	playsolo: function(gameId)
	{
		$.post(rg('Cafe_PlaySoloUrl'), { g: gameId }, play.onplayrequest, 'text');
		return false;
	},
	joinroom: function(gameId, roomTableId)
	{
		$.post(rg('Cafe_JoinRoomUrl'), { g: gameId, r: roomTableId }, play.onplayrequest, 'text');
		return false;
	},
	onplayrequest: function(response)
	{
		document.location.href = response;
	},

	createroom: function(gameId)
	{
		play.gameId = gameId;
		$('#' + resources.get('lblCreateRoomError')).html('');
		modal.open(resources.get('modalCreateRoom'), 400);
		return false;
	},
	validcreateroom: function()
	{
		var onvalidcreateroom = function(response)
		{
			if (response.CreationStatus != 1)
			{
				$('#' + resources.get('lblCreateRoomError')).html(response.Message);
			}
			else
				play.onplayrequest(response.Url);
		};
		var roomname = $('#' + resources.get('txtRoomName')).attr('value');
		var noofplayers = $('#' + resources.get('ddlNoOfPlayers')).attr('value');
		var password = $('#' + resources.get('txtPasswordCreateRoom')).attr('value');
		$.post(rg('CreateRoomUrl'), { g: play.gameId, rn: roomname, np: noofplayers, p: password }, onvalidcreateroom, 'json');
		return false;
	},

	joinprivateroom: function(gameId, roomTableId)
	{
		$('#' + resources.get('lblWrongPassword')).html('');
		play.gameId = gameId;
		play.roomTableId = roomTableId;
		modal.open(resources.get('modalJoinPrivateRoom'), 400);
		return false;
	},
	authroom: function()
	{
		var onauthroom = function(response)
		{
			if (!response.Success)
			{
				$('#' + resources.get('lblWrongPassword')).html(resources.get('WrongPassword'));
				$('#' + resources.get('txtPasswordJoinRoom')).focus();
			}
			else
				play.onplayrequest(response.Url);
		};
		var password = $('#' + resources.get('txtPasswordJoinRoom')).attr('value');
		$.post(rg('JoinPrivateRoomUrl'), { g: play.gameId, r: play.roomTableId, p: password }, onauthroom, 'json');
		return false;
	}
}
