// JavaScript Document
var OD = [
[0,35,25],
[0,35,50]
];
var od_is = 100;
var od_used = 0;

function check_slot (slot,info) {
	if (slot=='weapon1') {
		if (info[2]=='') { s = '<img src="/images/slots/pob3.gif" width=62 height=91 border="0" />' }
		else { s = '<img src="/images/weapons/'+info[3]+'.png" width=62 height=91 border=0>' }
	}
	else if (slot=='weapon2') {
		if (info[2]=='') { s = '<img src="/images/slots/pob11.gif" width=62 height=91 border="0" />' }
		else { s = '<img src="/images/weapons/'+info[3]+'.png" width=62 height=91 border=0>' }
	}
	else if (slot=='ring1' || slot=='ring2') {
		if (info[2]=='') { s = '<img src="/images/slots/pob12.gif" width=31 height=31 border="0" />' }
		else { s = '<img src="/images/weapons/'+info[3]+'.png" width=31 height=31 border=0>' }
	}
	else if (slot=='amulet') {
		if (info[2]=='') { s = '<img src="/images/slots/pob2.gif" width=62 height=35 border="0" />' }
		else { s = '<img src="/images/weapons/'+info[3]+'.png" width=62 height=35 border=0>' }
	}
	else if (slot=='helmet') {
		if (info[2]=='') { s = '<img src="/images/slots/pob1.gif" width=62 height=65 border="0" />' }
		else { s = '<img src="/images/weapons/'+info[3]+'.png" width=62 height=65 border=0>' }
	}
	else if (slot=='body') {
		if (info[2]=='') { s = '<img src="/images/slots/pob14.gif" width=62 height=90 border="0" />' }
		else { s = '<img src="/images/weapons/'+info[3]+'.png" width=62 height=90 border=0>' }
	}
	else if (slot=='belt') {
		if (info[2]=='') { s = '<img src="/images/slots/pob4.gif" width=62 height=30 border="0" />' }
		else { s = '<img src="/images/weapons/'+info[3]+'.png" width=62 height=30 border=0>' }
	}
	else if (slot=='arms') {
		if (info[2]=='') { s = '<img src="/images/slots/pob9.gif" width=62 height=40 border="0" />' }
		else { s = '<img src="/images/weapons/'+info[3]+'.png" width=62 height=40 border=0>' }
	}
	else if (slot=='gloves') {
		if (info[2]=='') { s = '<img src="/images/slots/pob10.gif" width=62 height=40 border="0" />' }
		else { s = '<img src="/images/weapons/'+info[3]+'.png" width=62 height=40 border=0>' }
	}
	else if (slot=='foots') {
		if (info[2]=='') { s = '<img src="/images/slots/pob8.gif" width=62 height=60 border="0" />' }
		else { s = '<img src="/images/weapons/'+info[3]+'.png" width=62 height=60 border=0>' }
	}
	return s;
}

function character (info,hp,type) {
	out =   '<table cellpadding="0" cellspacing="0" border="0"><tr><td colspan=3 align=center><strong>'+hp[0]+' ['+hp[1]+']</strong><br>[<strong><font color="#CC0000">'+hp[2]+'/'+hp[3]+'</font> <font color="#0000CC">'+hp[4]+'/'+hp[5]+'</font> <font color="#00CC00">'+hp[6]+'/'+hp[7]+'</font></strong>]</td></tr><tr><td>' +
            '<table border="0" cellpadding="0" cellspacing="0">' +
            '<tr><td><img src="/images/slots/slot1.gif" border="0" /></td></tr>' +
            '<tr><td>'+check_slot('arms',info[8])+'</td></tr>' +
            '<tr><td>'+check_slot('gloves',info[9])+'</td></tr>' +
            '<tr><td>'+check_slot('weapon1',info[0])+'</td></tr>' +
            '<tr><td>'+check_slot('body',info[6])+'</td></tr>' +
            '<tr><td><img src="/images/slots/pob15.gif" border="0" /><img src="/images/slots/pob15.gif" border="0" /></td></tr>' +
            '</table></td><td valign="bottom" align=center>' +
            '<table border="0" cellpadding="0" cellspacing="0" width=115><tr height="280"><td valign="middle" align=center style="padding:2px;">' +
            '<img src="/images/obrazy/human_mage_male.png" border="0" />' +
            '</td></tr><tr height="31"><td align="center">'+check_slot('ring1',info[2])+''+check_slot('ring2',info[3])+'</td></tr>' +
            '</table></td><td>' +
            '<table border="0" cellpadding="0" cellspacing="0">' +
            '<tr><td>'+check_slot('helmet',info[5])+'</td></tr>' +
            '<tr><td>'+check_slot('amulet',info[4])+'</td></tr>' +
            '<tr><td>'+check_slot('weapon2',info[1])+'</td></tr>' +
            '<tr><td>'+check_slot('belt',info[7])+'</td></tr>' +
            '<tr><td>'+check_slot('foots',info[10])+'</td></tr>' +
            '<tr><td><img src="/images/slots/pob15.gif" border="0" /><img src="/images/slots/pob15.gif" border="0" /></td></tr>' +
            '</table></td></tr></table>';
	d(type).innerHTML = out;
}

function fight_interface(info) {
	if (info=='') {
		out = '<form id="fight_form"><table border="0" cellpadding="1"><tr><td colspan=2 id="pers_od"></td></tr><tr><th>левая рука</th><th>правая рука</th></tr><tr>'+
		'<td><select id="l_1" onchange="count_od();"><option value="none" selected="selected">[действие не выбрано]</option><option value="hit_1">Удар [голова]</option><option value="block_1">Блок [голова]</option></select></td>'+
		'<td><select id="r_1" onchange="count_od();"><option value="none" selected="selected">[действие не выбрано]</option><option value="hit_1">Удар [голова]</option><option value="block_1">Блок [голова+торс]</option></select></td>'+
		'</tr><tr>'+
		'<td><select id="l_2" onchange="count_od();"><option value="none" selected="selected">[действие не выбрано]</option><option value="hit_1">Удар [торс]</option><option value="block_1">Блок [торс]</option></select></td>'+
		'<td><select id="r_2" onchange="count_od();"><option value="none" selected="selected">[действие не выбрано]</option><option value="hit_1">Удар [торс]</option><option value="block_1">Блок [торс+живот]</option></select></td>'+
		'</tr><tr>'+
		'<td><select id="l_3" onchange="count_od();"><option value="none" selected="selected">[действие не выбрано]</option><option value="hit_1">Удар [живот]</option><option value="block_1">Блок [живот]</option></select></td>'+
		'<td><select id="r_3" onchange="count_od();"><option value="none" selected="selected">[действие не выбрано]</option><option value="hit_1">Удар [живот]</option><option value="block_1">Блок [живот+ноги]</option></select></td>'+
		'</tr><tr>'+
		'<td><select id="l_4" onchange="count_od();"><option value="none" selected="selected">[действие не выбрано]</option><option value="hit_1">Удар [ноги]</option><option value="block_1">Блок [ноги]</option></select></td>'+
		'<td><select id="r_4" onchange="count_od();"><option value="none" selected="selected">[действие не выбрано]</option><option value="hit_1">Удар [ноги]</option><option value="block_1">Блок [ноги]</option></select></td>'+
		'</tr><tr><td align="center" colspan="2"><input type="button" value="ход" id="act_but" onclick="act();" /> <input type="button" value="сброс"  onclick="act_reset();" /></td></tr></table></form>';
		d("fight").innerHTML = out;
		my_od();
	} else {
		if (info.indexOf('preend')>=0) {
			render_preend(info);
		}
		else if (info=='finished') {
			render_just_finish();
		}
		else if (info=='wait') {
			d("fight").innerHTML = '<b>Ожидаем ход противника</b><br><a href="/main.php">обновить</a>';
		}
	}
}

function my_od() {
	if (od_used<=od_is) {
		out = '<font style="font-size:12px;"><b>Использовано: '+od_used+'/'+od_is+'</b></font>';
		d("act_but").disabled = false;
	}
	else {
		out = '<font style="font-size:12px; color: #990000"><b>Использовано: '+od_used+'/'+od_is+'</b></font>';
		d("act_but").disabled = true;
	}
	d("pers_od").innerHTML = out;
}

function count_od() {
	used = 0;
	if (d("l_1").value=='hit_1') {used = used + OD[0][1]} else if (d("l_1").value=='block_1') {used = used + OD[0][2]}
	if (d("l_2").value=='hit_1') {used = used + OD[0][1]} else if (d("l_2").value=='block_1') {used = used + OD[0][2]}
	if (d("l_3").value=='hit_1') {used = used + OD[0][1]} else if (d("l_3").value=='block_1') {used = used + OD[0][2]}
	if (d("l_4").value=='hit_1') {used = used + OD[0][1]} else if (d("l_4").value=='block_1') {used = used + OD[0][2]}
	
	if (d("r_1").value=='hit_1') {used = used + OD[1][1]} else if (d("r_1").value=='block_1') {used = used + OD[1][2]}
	if (d("r_2").value=='hit_1') {used = used + OD[1][1]} else if (d("r_2").value=='block_1') {used = used + OD[1][2]}
	if (d("r_3").value=='hit_1') {used = used + OD[1][1]} else if (d("r_3").value=='block_1') {used = used + OD[1][2]}
	if (d("r_4").value=='hit_1') {used = used + OD[1][1]} else if (d("r_4").value=='block_1') {used = used + OD[1][2]}

	od_used = used;
	my_od();
}

function act_reset () {
	d("fight_form").reset();
	od_used = 0;
	count_od();
}

function act() {
	if (od_used<=od_is) {
		info = d("l_1").value+'|'+d("l_2").value+'|'+d("l_3").value+'|'+d("l_4").value+'@'+d("r_1").value+'|'+d("r_2").value+'|'+d("r_3").value+'|'+d("r_4").value;
		query = 'act=hode&info='+info+'&vcode='+vk;
		act_reset();
		ajax_query ('fight',query,'after_act');
	}
}

function render_preend(info) {
	info = info.split('|');
	info = info[1].split('&');
	if (info.length==1) {
		info1 = info[0].split(':');
		d("fight").innerHTML = '<table width=400 border=1><tr><th>участники</th><th>опыт</th></tr><tr><td>'+info1[0]+' ['+info1[1]+']</td><td align=right>'+info1[2]+'</td></tr><tr><td colspan=2 align=center><input type="button" value="Завершить бой" onclick="fight_finish();"></td></tr></table>';
	}
	else if (info.length==2) {
		info1 = info[0].split(':');
		info2 = info[1].split(':');
		d("fight").innerHTML = '<table width=400 border=1><tr><th>участники</th><th>опыт</th></tr><tr><td>'+info1[0]+' ['+info1[1]+']</td><td align=right>'+info1[2]+'</td></tr><tr><td>'+info2[0]+' ['+info2[1]+']</td><td align=right>'+info2[2]+'</td></tr><tr><td colspan=2 align=center><input type="button" value="Завершить бой" onclick="fight_finish();"></td></tr></table>';
	}
}

function render_just_finish() {
	d("fight").innerHTML = '<input type="button" value="Завершить" onclick="fight_finish();">';
}

function after_act(info) {
	if (info!='ERROR' && info!='WAIT') {
		info = info.split('{log}');
		f_log = info[1];
		info = info[0].split('@');
		vk = info[4];
		pers_wears = info[0].split('&');
		pers_wears[0] = pers_wears[0].split('|');
		pers_wears[1] = pers_wears[1].split('|');
		pers_wears[2] = pers_wears[2].split('|');
		pers_wears[3] = pers_wears[3].split('|');
		pers_wears[4] = pers_wears[4].split('|');
		pers_wears[5] = pers_wears[5].split('|');
		pers_wears[6] = pers_wears[6].split('|');
		pers_wears[7] = pers_wears[7].split('|');
		pers_wears[8] = pers_wears[8].split('|');
		pers_wears[9] = pers_wears[9].split('|');
		pers_wears[10] = pers_wears[10].split('|');
		pers_hp = info[1].split('|');
		enemy_wears = info[2].split('&');
		enemy_wears[0] = enemy_wears[0].split('|');
		enemy_wears[1] = enemy_wears[1].split('|');
		enemy_wears[2] = enemy_wears[2].split('|');
		enemy_wears[3] = enemy_wears[3].split('|');
		enemy_wears[4] = enemy_wears[4].split('|');
		enemy_wears[5] = enemy_wears[5].split('|');
		enemy_wears[6] = enemy_wears[6].split('|');
		enemy_wears[7] = enemy_wears[7].split('|');
		enemy_wears[8] = enemy_wears[8].split('|');
		enemy_wears[9] = enemy_wears[9].split('|');
		enemy_wears[10] = enemy_wears[10].split('|');
		enemy_hp = info[3].split('|');
		
		character([pers_wears[0],pers_wears[1],pers_wears[2],pers_wears[3],pers_wears[4],pers_wears[5],pers_wears[6],pers_wears[7],pers_wears[8],pers_wears[9],pers_wears[10]],pers_hp,'pers');
		character([enemy_wears[0],enemy_wears[1],enemy_wears[2],enemy_wears[3],enemy_wears[4],enemy_wears[5],enemy_wears[6],enemy_wears[7],enemy_wears[8],enemy_wears[9],enemy_wears[10]],enemy_hp,'enemy');
		if (f_log!='' && f_log!='@'){
			fight_log(f_log);
		}
		if (info[5].indexOf('preend')==0) {
			render_preend(info[5]);
		}
		else if (info[5].indexOf('finished')==0) {
			render_just_finish();
		}
	}
	else if (info=='WAIT') {
		d("fight").innerHTML = '<b>Ожидаем ход противника</b><br><a href="/main.php">обновить</a>';
	}
	else {
		location.href = '/main.php';
	}
}

function fight_finish() {
	query = 'act=finish&vcode='+vk;
	ajax_query ('fight',query,'after_finish');
}

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