// JavaScript Document
var active_duel = '';

function show_arena(room) {
	query = 'room='+room+'&arena_act=show&vcode='+vk;
	room = parseInt(room);
	switch (room) {
		case 1: r = 'render_bestiary'; break;
		case 2: r = 'render_duels'; break;
	}
	active_duel = '';
	ajax_query ('arena',query,r);
}

function render_duels (info) {
	info = info.split('@');
	vk = info[1];
	if (info[0]!='') { duels =  render_exist_duels(info[0]) }
	else { duels = '<b>Нет активных заявок</b>' }
	out = create_duel() + '<br>' + duels;
	if (info[2]!='') {
		butts = '';
		bs = info[2].split('&');
		for (a=0,b=bs.length;a<b;a++){
			but = bs[a].split('=');
			if (but[0]=='reduce') { butts = butts + '<input type="button" value="отказаться" onclick="reduce_duel('+but[1]+');"> ' }
			if (but[0]=='accept') { butts = butts + '<input type="button" value="принять" onclick="accept_duel('+but[1]+');"> ' }
			if (but[0]=='delete') { butts = butts + '<input type="button" value="удалить заявку" onclick="delete_duel('+but[1]+');"> ' }
			if (but[0]=='deny') { butts = butts + '<input type="button" value="отказать" onclick="deny_duel('+but[1]+');"> ' }
		}
		if (butts!='') { out = out + '<br>' + butts}
	}
	d("arena_content").innerHTML = out;
}

function reduce_duel (info) {
	query = 'room=2&arena_act=reduce_duel&info='+info+'&vcode='+vk;
	ajax_query ('arena',query,"after_new_duel");
}

function delete_duel (info) {
	query = 'room=2&arena_act=delete_duel&info='+info+'&vcode='+vk;
	ajax_query ('arena',query,"after_new_duel");
}

function deny_duel (info) {
	query = 'room=2&arena_act=deny_duel&info='+info+'&vcode='+vk;
	ajax_query ('arena',query,"after_new_duel");
}

function accept_duel (info) {
	query = 'room=2&arena_act=accept_duel&info='+info+'&vcode='+vk;
	ajax_query ('arena',query,"after_new_duel");
}

function render_exist_duels(info) {
	join_but = 0;
	info = info.split('|');
	out = '';
	for (a=0, b=info.length;a<b;a++) {
		if (out!='') {out = out + '<br>' }
		duel = info[a].split(':');
		switch (duel[4]) {
			case '120': duel[4] = '2 минуты'; break;
			case '180': duel[4] = '3 минуты'; break;
			case '240': duel[4] = '4 минуты'; break;
			case '300': duel[4] = '5 минут'; break;
		}
		if (duel[2]=='') { enemy = 'нет противника' }
		else {
			enemy = duel[2].split('&');
			enemy = enemy[0] + ' [' + enemy[1] + ']';
		}
		pers = duel[1].split('&');
		pers = pers[0]+' ['+pers[1]+']';
		if(duel[5]==1) { can_join = '<input type="radio" name="join" value="'+duel[0]+'" onclick="select_duel(this.value);">'; join_but++ }
		else { can_join = ''}
		out = out + '['+duel[3]+'%, '+duel[4]+'] <b>'+pers+'</b> vs <b>'+enemy+'</b> ' + can_join;
	}
	if (join_but>0) {
		out =  out + '<br><input type="submit" value="принять заявку" onclick="join_duel();">';
	}
	return out;
}

function select_duel(info) {
	if (info!='') { active_duel = info 	}
	else { location.href = '/main.php' }
}

function join_duel() {
	if (active_duel!='') {
		info = active_duel;
		query = 'room=2&arena_act=join_duel&info='+info+'&vcode='+vk;
		ajax_query ('arena',query,"after_new_duel");
	}
	else { location.href = '/main.php' }
}

function create_duel() {
	out = '<a href="javascript:void(0);" onclick="show_new_duel();" id="add_duel">подать новую заявку</a>'+
'<table style="display:none" border=1 cellspacing=0 id="new_duel">'+
'<tr><td colspan="2" align="center"><b>новая заявка на дуэль</b></td></tr>'+
'<tr><td>Травматичность:</td><td><select id="injury"><option value="50">50%</option><option value="80">80%</option><option value="100">100%</option></select></td></tr>'+
'<tr><td>Таймаут:</td><td><select id="timeout"><option value="120">2 минуты</option><option value="180">3 минуты</option><option value="240">4 минуты</option><option value="300">5 минут</option></select></td></tr>'+
'<tr><td colspan="2" align="center"><input type="button" value="подать заявку" onclick="create_new_duel();" /></td></tr>'+
'</table>';
	return out;
}

function create_new_duel () {
	info = d("injury").value+'|'+d("timeout").value;
	query = 'room=2&arena_act=add_duel&info='+info+'&vcode='+vk;
	ajax_query ('arena',query,"after_new_duel");
}

function after_new_duel (info) {
	if (info!='ERROR') { show_arena(2) }
	else { location.href = '/main.php' }
}

function show_new_duel() {
	d("add_duel").style.display = 'none';
	d("new_duel").style.display = '';
}

function render_bestiary (info) {
	info = info.split('&');
	vk = info[1];
	info = info[0].split('@');
	out = '<select name="bot_act" id="bot_act">';
	for (a=0,b=info.length;a<b;a++) {
		bot = info[a].split('|');
		out = out + '<option value="'+bot[0]+'">'+bot[1]+' ['+bot[2]+']</option>';
	}
	out = out + '</select> <input type="button" value="Напасть" onclick="start_botfight();">';
	d("arena_content").innerHTML = out;
}

function start_botfight() {
	query = 'room=1&arena_act=botfight&bot_id='+d("bot_act").value+'&vcode='+vk;
	ajax_query ('arena',query,'botfight');
}

function botfight(info) {
	if (info!='ERROR'){
		location.href = '/main.php';
	}
}