var srcback='';//sauvegarde image deplacement
var actions=0;//nb d'actions
var mouvems=0;//nb de mouvs
var px=0;var py=0;//position perso
var ramassa=0;
var ressour=0;
var antizer=0;
var armsort=0;
var armport='c';
var pietine=0;
var drefres=0;//damier à rafraichir 0|1
var srefres=0;//stats à rafraichir 0|1
var bat=new Array();
var rel=new Array();
var mdj=new Array();
var ccd=' ';
var nrs=false;

function voir(evt,posx,posy,num,pseudo,message){
	if(num!=''){
		document.actions.mat.value=num;
		gmenu(num,posx,posy);
		affiche(evt);
		n=document.getElementById("nom");
		m=document.getElementById("mdj");
		if((pseudo+' ('+num+')').length>28){
			len=(' ('+num+')').length;
			pseudo=pseudo.substr(0,25-len)+'...';
		}
		n.innerHTML=pseudo+' ('+num+')';
		m.innerHTML=message;
	}
	t=document.tepo;
	if(t){
		t.xt.value=posx;
		t.yt.value=posy;
		if(num!=0)
			t.mt.value=num;
	}
}
function siz(h){
	if(h<screen.height-620)
		h=screen.height-620;
	document.getElementById("cons").height=h;
}
function redir(page){
	window.location.href=page;
}
function conso(page){
	var cfr=document.getElementById("cons");
	cfr=cfr.contentWindow.document||cfr.contentDocument;
	cfr.location.href=page;
}
function popup(page){
	window.open(page,'',config='toolbar=no,resizable=yes,menubar=no,location=no,directories=no,status=no,scrollbars=yes');
}
function popu2(page,ha,la){
	window.open(page,'',config='height='+ha+',width='+la+',resizable=yes,toolbar=no,menubar=no,location=no,directories=no,status=no,scrollbars=yes');
}
function plusi(){
	if(document.info.mat.value)
		popu2('plusdinfo.php?mat='+document.info.mat.value,700,800);
}
function afco(balise){//transfere le contenu de la console ajax vers une balise
	var cfr=document.getElementById("coin");
	var con=document.getElementById(balise);
	cfr=cfr.contentWindow.document||cfr.contentDocument;
	con.innerHTML=cfr.documentElement.innerHTML;
}
function reda(page){//charge une page dans la console ajax
	var cfr=document.getElementById("coin");
	cfr=cfr.contentWindow.document||cfr.contentDocument;
	cfr.location.href=page;
}
function disago(){
	document.getElementById("go").disabled=true;
	return true;
}
function mmou(nb){
	mouvems-=nb;
	document.getElementById("mou2").innerHTML=mouvems;
}
function mact(nb){
	actions-=nb;
}
function setam(acti,mouv,azer){
	actions=acti;
	mouvems=mouv;
	antizer=azer;
}
function setar(port,sort,pas){
	armport=port;
	armsort=sort;
	armpas=pas
}
function setda(rama,ress,piet,x,y){
	px=x;
	py=y;
	ramassa=rama;
	ressour=ress;
	pietine=piet;
}
function bene(){
	v=document.getElementById("bene");
	if(v.value=='Activer')
		v.value='Désactiver';
	else
		v.value='Activer';
}
function rese(){
	bat=new Array();
	rel=new Array();
}
function rbat(pseu,mat,race,x,y){
	bat[bat.length]=new Array(pseu,mat,race,x,y);
}
function rrel(type,mat){
	rel[rel.length]=new Array(type,mat);
}
function menu(){//met a jour les valeurs pour le menu
	mmou(0);
	mact(0);
	if(drefres==1){
		drefres=0;
		reda('dam.php');
	}
	if(srefres==1){
		srefres=0;
		reda('jstats.php');
	}
}
function gmenu(mat,posx,posy){//fonction qui gere le menu
	s=document.actions.actio;
	s.innerHTML='';
	s.appendChild(copti(0,'Fermer le menu',''));
	s.appendChild(copti(12,'Informations','i13'));
	if(mat!=0)
		s.appendChild(copti(11,'Compétence spéciale','i11'));
	/*if(mat=='o'&&Math.max(Math.abs(px-posx),Math.abs(py-posy))==1&&mouvems>0)
		s.appendChild(copti('-'+(posx-px+12+(posy-py)*10),'Déplacement','i12'));*/
	if(Math.max(Math.abs(px-posx),Math.abs(py-posy))==1&&mat>0&&mouvems>1&&pietine>0)
		s.appendChild(copti('-'+(posx-px+12+(posy-py)*10),'Piétinement','i12'));
	if(actions>0){
		if(antizer==0){
			if(armport!='d'&&Math.max(Math.abs(px-posx),Math.abs(py-posy))==1&&mat>0 && actions>=armpas)
				s.appendChild(copti(1,'Attaque corps à corps ('+armpas+' PA)','i1'));
			if(armport!='c'&&Math.max(Math.abs(px-posx),Math.abs(py-posy))>1&&mat>0 && actions>=armpas)
				s.appendChild(copti(2,'Attaque à distance ('+armpas+' PA)','i2'));
			if(armsort==1&&mat>0)
				s.appendChild(copti(3,'Magie','i3'));
			if(Math.max(Math.abs(px-posx),Math.abs(py-posy))==1&&mat>0){
				s.appendChild(copti(5,'Vol à la tire','i5'));
				if(actions>=2) s.appendChild(copti(6,'Entrainement (2 PA)','i6'));
			}
			if(ressour==1&&px==posx&&py==posy)
				s.appendChild(copti(7,'Collecte Ressources','i7'));
			if(mat>0&&(px!=posx||py!=posy))
				s.appendChild(copti(8,'Jeter un projectile','i8'));
			if(mat<0&&Math.max(Math.abs(px-posx),Math.abs(py-posy))==1&&(px!=posx||py!=posy))
				s.appendChild(copti(9,'Pilonage','i9'));
		}
		if(Math.max(Math.abs(px-posx),Math.abs(py-posy))==1&&mat>0)
			s.appendChild(copti(10,'Donner or/objet','i10'));
		if(px==posx&&py==posy)
			for(i=0;i<rel.length;i++)
				s.appendChild(copti(rel[i][0]+'-'+rel[i][1]+'-1',rel[i][0],'i13'));
	}
	if(ramassa==1&&px==posx&&py==posy)
		s.appendChild(copti(4,'Ramasser objet','i4'));
	for(i=0;i<bat.length;i++){
		if(bat[i][3]==posx&&bat[i][4]==posy)
			if(bat[i][2]=='respawn'||bat[i][2]=='portes'){
				if(mouvems>0)
					s.appendChild(copti('-'+(posx-px+12+(posy-py)*10),bat[i][0],'i12'));
			}else
				s.appendChild(copti(bat[i][2]+bat[i][1],bat[i][0],'i12'));
	}
	s.size=s.options.length;
	s.multiple=false;
	s.options.selectedIndex=-1;
}
function smact(){//envoie le formulaire d'action
	s=document.actions;
	val=s.actio.options[s.actio.selectedIndex].value;
	if(val==0){
		masque();
		return false;
	}else if(val==1)
		s.action='attaque.php';
	else if(val==2)
		s.action='tir.php';
	else if(val==3)
		s.action='magie.php';
	else if(val==4)
		s.action='ramassage.php';
	else if(val==5)
		s.action='vol.php';
	else if(val==6)
		s.action='entrainement.php';
	else if(val==7)
		s.action='minage.php';
	else if(val==8)
		s.action='jet.php';
	else if(val==9)
		s.action='bomb.php';
	else if(val==10)
		s.action='donner.php';
	else if(val==11)
		s.action='comp.php';
	else if(val==12){
		if(s.mat.value=='o')
			s.action='infoo.php?x='+posx+'&y='+posy;
		else
			s.action='plusdinfo.php?mat='+s.mat.value;
	}else if(val<0){
		s.action='deplace.php?move='+(-1*val);
		masque();
	}else
		s.action='action.php';
	s.submit();
	masque();
}
function copti(val,htm,clas){//crée un objet option
	nopt=document.createElement("option");
	nopt.value=val;
	nopt.innerHTML=htm;
	if(clas!='')
		nopt.className=clas;
	return nopt;
}
function setDisplay(){//affichage des plans pour batiments
	f=document.actions;
	typeid=f.plan.selectedIndex;
	e=document.getElementById('plan1');
	d=document.getElementById('plan2');
	if(typeid==1){
		e.style.display='block';
		d.style.display='none';
	}else{
		e.style.display='none';
		d.style.display='block';
	}
}
function fdep(obj,depl){//affichage des fleches de direction
	var dir='';
	if(depl>20) dir='N'; else if(depl<10) dir='S';
	if(depl%10==3) dir+='E'; else if(depl%10==1) dir+='O';
	srcback=obj.src;
	obj.src="../img/"+dir+".gif";
}
function bdep(obj,bac){//masquages des fleches de direction
	obj.src=srcback;
}
function affiche(evt){//affiche menu d'action au curseur
	nrs=true;
	var yy=18;
	var s=document.actions.actio.options;
	if(evt.clientY+document.body.scrollTop>screen.availHeight-250)
		yy+=s.length*12+20;
	var feuille=document.getElementById("fper");
	feuille.style.display='block';
	feuille.style.left=(evt.clientX+document.body.scrollLeft-15)+"px";
	feuille.style.top=(evt.clientY+document.body.scrollTop-yy)+"px";
}
function masque(){//masque menu d'action
	nrs=false;
	var feuille = document.getElementById("fper");
	feuille.style.display='none';
}
function tca(){
	var cfr=document.getElementById("cons");
	cfr=cfr.contentWindow.document||cfr.contentDocument;
	ccd=cfr.documentElement.innerHTML;
}
function tuc(){
	var cfr=document.getElementById("cons");
	cfr=cfr.contentWindow.document||cfr.contentDocument;
	cfr.documentElement.innerHTML=ccd;
}
function iem(){
	if(navigator.appName!='Microsoft Internet Explorer')
		return false;
	var cfr=document.getElementById("nav");
	cfr=cfr.rows[1].cells[0];
	for(i=0;i<cfr.childNodes.length;i++){
		if(cfr.childNodes[i].src){
			cfr.childNodes[i].style.marginLeft='-3px';
			cfr.childNodes[i].style.marginRight='-3px';
		}
	}
}
function zmo(){
	if(nrs)
		return;
	var feuille=document.getElementById("fper");
	var sh=screen.height;
	var sw=screen.width/2-5;
	var sdam=0;
	if(sw<395)
		sw=395;
	var d=document.getElementById("s4").childNodes[0];
	sdam=d.rows.length-2;
	if(sdam*45+60<=sw)
		return;
	sdam=sw/(sdam*45+60);
	d.rows[0].cells[0]*=sdam;
	d.rows[0].cells[d.rows.length-1]*=sdam;
	sw=document.styleSheets[1].rules||document.styleSheets[1].cssRules;
	sw[79].style.width=Math.round(45*sdam)+'px';
	sw[80].style.width=Math.round(33*sdam)+'px';
	sw[95].style.height=Math.round(33*sdam)+'px';
	sw[96].style.height=Math.round(35*sdam)+'px';
	sw[100].style.display='none';
}
function zmi(){
	var d=document.getElementById("s4").childNodes[0];
	d.rows[0].cells[0]=30;
	d.rows[0].cells[d.rows.length-1]=30;
	sw=document.styleSheets[1].rules||document.styleSheets[1].cssRules;
	sw[79].style.width='45px';
	sw[80].style.width='33px';
	sw[95].style.height='33px';
	sw[96].style.height='35px';
	sw[100].style.display='block';
}
function dat(){
	s=document.getElementById('date').innerHTML;
	d=new Date(eval(s.substr(0,4)),eval(s.substr(5,2))-1,eval(s.substr(8,2)),eval(s.substr(11,2)),eval(s.substr(14,2)),eval(s.substr(17,2)));
	d=new Date(Date.parse(d)+1000);
	s=d.getFullYear()+'-';
	if(d.getMonth()<9)
		s=s+'0';
	s=s+(d.getMonth()+1)+'-';
	if(d.getDate()<10)
		s=s+'0';
	s=s+d.getDate()+' ';
	if(d.getHours()<10)
		s=s+'0';
	s=s+d.getHours()+':';
	if(d.getMinutes()<10)
		s=s+'0';
	s=s+d.getMinutes()+':';
	if(d.getSeconds()<10)
		s=s+'0';
	s=s+d.getSeconds();
	document.getElementById('date').innerHTML=s;
	setTimeout("dat()", 1000);
}
function index(){
	if(screen.width>=1280)
		document.getElementById("cad").style.width=1280;
	else{
		i=document.getElementById("ban");
		i.width=400;
		i.height=126;
		i=document.getElementById("imi");
		i.width=200;
		i.height=160;
	}
}
function hli(){
	var t=document.getElementById('s4');
	for(i=1;i<t.rows.length-1;i++){
		for(j=1;j<t.rows.length-1;j++){
			t.rows[i].cells[j].onmouseover=hla;
			t.rows[i].cells[j].onmouseout=hld;
		}
	}
}
function hla(){
	var y=this.parentNode.rowIndex;
	var x=this.cellIndex;
	var t=document.getElementById('s4');
	t.rows[0].cells[x].style.background='#336699';
	t.rows[0].cells[x].style.opacity=0.75;
	t.rows[0].cells[x].style.filter = 'alpha(opacity=75)';
	t.rows[t.rows.length-1].cells[x].style.background='#336699';
	t.rows[t.rows.length-1].cells[x].style.opacity=0.75;
	t.rows[t.rows.length-1].cells[x].style.filter = 'alpha(opacity=75)';
	t.rows[y].cells[0].style.background='#336699';
	t.rows[y].cells[0].style.opacity=0.75;
	t.rows[y].cells[0].style.filter = 'alpha(opacity=75)';
	t.rows[y].cells[t.rows.length-1].style.background='#336699';
	t.rows[y].cells[t.rows.length-1].style.opacity=0.75;
	t.rows[y].cells[t.rows.length-1].style.filter = 'alpha(opacity=75)';
}
function hld(){
	var y=this.parentNode.rowIndex;
	var x=this.cellIndex;
	var t=document.getElementById('s4');
	t.rows[0].cells[x].style.background='none';
	t.rows[0].cells[x].style.opacity=1;
	t.rows[0].cells[x].style.filter = 'alpha(opacity=100)';
	t.rows[t.rows.length-1].cells[x].style.background='none';
		t.rows[t.rows.length-1].cells[x].style.opacity=1;
	t.rows[t.rows.length-1].cells[x].style.filter = 'alpha(opacity=100)';
	t.rows[y].cells[0].style.background='none';
		t.rows[y].cells[0].style.opacity=1;
	t.rows[y].cells[0].style.filter = 'alpha(opacity=100)';
	t.rows[y].cells[t.rows.length-1].style.background='none';
		t.rows[y].cells[t.rows.length-1].style.opacity=1;
	t.rows[y].cells[t.rows.length-1].style.filter = 'alpha(opacity=100)';
}