$('#settings_control').ajaxForm({
	dataType: 'json',
	success: function(data)
	{
		$.each(data, function(i, val)
		{
			if(i == 'e')
				bootbox.dialog('<h3 class="red">Ошибка</h3>'+val,
					[{
						"label" : "OK",
						"class" : "btn-small btn-primary",
					}]
				);

			if(i == 's')
				bootbox.dialog('<h3 class="green">Внимание</h3> Внесенные изменения сохранены.',
					[{
						"label" : "OK",
						"class" : "btn-small btn-primary",
					}]
				);

			loading(0)
		});
	}
});

function update_resources(id, go)
{
	$.getJSON(home+'control/id/'+id+'/section/scan/resources', function(data)
	{
		$.each(data, function(i, val)
		{
			if(i == 'cpu')
			{
				w = 249 / 100 * parseInt(val);

				$('#cpu_'+id).css('width', w+'px');
				$('#cpu_'+id+'_val').html(val);
			}

			if(i == 'ram')
			{
				w = 249 / 100 * parseInt(val);

				$('#ram_'+id).css('width', w+'px');
				$('#ram_'+id+'_val').html(val);
			}

			if(i == 'hdd')
			{
				w = 249 / 100 * parseInt(val);

				$('#hdd_'+id).css('width', w+'px');
				$('#hdd_'+id+'_val').html(val);
			}
		});

		if(go)
			setTimeout(function() {update_resources(id, true); update_status(id)}, 5000);
	});
}

function update_status(id)
{
	$.getJSON(home+'control/id/'+id+'/section/scan/update_status', function(data)
	{
		$.each(data, function(i, val)
		{
			if(i == 'time' && $('#time_'+id).html() != val && val != '')
				$('#time_'+id).html(val);

			if(i == 'time_end' && $('#time_end_'+id).html() != val && val != '')
				$('#time_end_'+id).html(val);

			if(i == 'status' && $('#status_'+id).html() != val)
				$('#status_'+id).html(val);

			if(i == 'buttons' && $('#buttons_'+id).html() != val)
				$('#buttons_'+id).html(val);
		});
	});
}

function update_info(id)
{
	$.getJSON(home+'control/id/'+id+'/section/scan/update_info', function(data)
	{
		$.each(data, function(i, val)
		{
			if(i == 'cpu')
				$('#cpu_info').html(val);

			if(i == 'ram')
				$('#ram_info').html(val);

			if(i == 'hdd')
				$('#hdd_info').html(val);
		});
	});
}

function ctrl_install(id)
{
	loading(1);

	$.getJSON(home+'control/id/'+id+'/section/install/game/'+$('#game').val()+'/pack/'+$('#pack').val()+'/go', function(data)
	{
		$.each(data, function(i, val)
		{
			if(i == 'e')
				bootbox.dialog('<h3 class="red">Ошибка</h3>'+val,
					[{
						"label" : "Продолжить",
					}]
				);

			if(i == 's')
				location.href = home+'control/id/'+id+'/server/'+data['id'];
		});

		loading(0);
	});
}

function extend_ctrl()
{
	loading(1);

	$.getJSON(home+'control/id/'+id+'/section/extend/time/'+$('#time').val()+'/go', function(data)
	{
		$.each(data, function(i, val)
		{
			if(i == 'e')
				bootbox.dialog('<h3 class="red">Ошибка</h3>'+val,
					[{
						"label" : "Продолжить",
					}]
				);

			if(i == 's')
				location.href = home+'control';

			loading(0);
		});
	});
}

function upd_price()
{
	$.get(home+'control/id/'+id+'/section/extend/time/'+$('#time').val()+'/get/sum', function(val)
	{
		$('#sum').html(val);
	});
}

function control_restart(id)
{
	loading(1);

	$.getJSON(home+'control/id/'+id+'/section/reboot/go', function(data)
	{
		$.each(data, function(i, val)
		{
			if(i == 'e')
				bootbox.dialog('<h3 class="red">Ошибка</h3>'+val,
					[{
						"label" : "OK",
						"class" : "btn-small btn-primary",
					}]
				);

			if(i == 's')
				update_status(id)

			loading(0);
		});
	});
}

