
preload_images(preload,new Array("h-1.gif","h0.gif","h1.gif","h2.gif",
									"h3.gif","h4.gif","h5.gif",
									"h6.gif","h7.gif","h8.gif"));
	
var pclasses = new Array("Equilibré","Combattant","Minier","Réparateur","Constructeur","Eclaireur","Marchand","","","","","Bot");
var names_color= new Array("white","#FAE8AA","#AFFAAA","#FEC3C3","#FF66FF");
//var names_color= new Array("white","#FA6666","#66FA66","#FEFE66");
var obj_imgs = new Array("",//0
						"empty12x12.gif",
						"batteryh.gif",
						"proph.gif",
						"weapon_eh.gif",
						"weapon_mh.gif",//5
						"weapon_th.gif",
						"weapon_ch.gif",
						"repairh.gif",
						"weapong.gif",
						"specialg.gif",//10
						"solarph.gif",
						"vis3.gif",
						"batterynh.gif",
						"solarplh.gif",
						"ressourcem.gif",//15
						"ressourceg.gif",
						"armor.gif"
						);
/*var obj_imgs = new Array("",
						"vis.gif",
						"battery.gif",
						"prop.gif",
						"weapon_e.gif",
						"weapon_m.gif",
						"weapon_t.gif",
						"weapon_c.gif",
						"weapon_r.gif",
						"weapon.gif",
						"special.gif",
						"&nbsp;");*/


function hilitecell(x,y,val)
{
	var k=0;
	var color = "";//"#111133";
	if(val)
		color = "#222255";//"#234771";
		
	for(k=0;;k++)
	{
		if(null==document.getElementById('b'+k))
			break;
		if(document.getElementById('b'+k).getAttribute('name')== 'b'+x+'.'+y)
		{
			document.getElementById('b'+k).style.backgroundColor=color;
		}
	}
}
function cellclick(x,y)
{
    //alezrt("refresh move (detec)");
	window.parent.frames['move'].location.href = 'move.php?mx='+x+'&my='+y+'&sid='+sid;
}
function hilitemapcell(b_posx,b_posy,val)
{	
	var frame=window.parent.frames["game_middle"];
	if(null!=frame)
		if(null!=frame.map_hilitecell)
			frame.map_hilitecell(b_posx,b_posy,val);
}

var things = new Array();
things[0]=new Array();
things[1]=new Array();
things[2]=new Array();
things[3]=new Array();


function lidraw(t,cid,px,py,distance,img,gimg,name,name_color,text1,text2,text3,links,pestatus,status,inert,fastmove)
{
	if(map_strings[name]=="Pucellina")name_color = 4;
	var str="";
	str+= "<tr id='b"+cid+"' name='b"+px+"."+py+"' ";
	str+= "onmouseover='hilitemapcell("+px+","+py+",1);' ";
	str+= "onmouseout='hilitemapcell("+px+","+py+",0);' ";
	str+= "><td width='29px' style='cursor: pointer;' onclick='javascript:cellclick("+px+","+py+");'><center>";
	//str+=img;
	if(t==2 || t==3)//2=obj 3=rsc
	{
		if(img != '')
			str+= yimg("./img/"+(t==2?obj_imgs[img]:map_strings[img]),16,16);
	}
	else
	{
		if(gimg != '')
			str+= yimg("/avatars/"+map_strings[gimg],16,16);
	}
	//str+= "<img src='./img/"+(t==2?obj_imgs[img]:map_strings[img])+"'>";
	str+="</center></td><td><table border=0 style='border-style:none' width=100% ><tr>";
	if(status!="" && parseInt(status)!=-1)
	{
		status=parseInt(status);
		var color=(status>70?'yellowgreen':(status>30?'#8899aa':'firebrick'));
		str+="<td align=middle style='width:4px;border-style:none;font-size:1px;padding:0px;'>"+drawVProgress(color,100-parseInt(status),100,32,'left')+"</td>";
	}
    if(/*pestatus!="" &&*/ parseInt(pestatus)!=-1)
	{
		pestatus=parseInt(pestatus);
		var color=(pestatus>50?'lightblue':'steelblue');
		str+="<td align=middle style='width:4px;border-style:none;font-size:1px;padding:0px;'>"+drawVProgress(color,100-parseInt(pestatus),100,32,'left')+"</td>";
	}
	str+="<td class=detec style='border-style:none;padding:3px;' >";
	if(text3.length>0)
	{
		var specs=text3.split("|");
		var i;
	
		str+="<div style='float:right'>";
		for(i=0;i<specs.length;i++)
		{
			//str+=yimg("./img/"+specs[i],16,16);
			str+="<img src='./img/"+specs[i]+"' width='15' height='13'>";
		}
		str+="</div>";
	}
	
	
	str+="<div style='float:left;margin:0px;padding:0px;'>";
	if(img != '' && !(t==2 || t==3))//2=obj 3=rsc
	{
		var alt="";
		if(t==0)
			alt = "title='" + pclasses[parseInt(img.slice(2,4))]+"'";//clX.gif
		str+= '<img align=top width=16 height=16 src="./img/'+(t==0?img:map_strings[img])+'" '+alt+'>&nbsp;'; // 0= player
	}
	name="<b>"+map_strings[name]+"</b>";
	name ="<font color='"+names_color[name_color]+"'>"+name+"</font>";
	str+=name;
	str+=" "+text1;
	
	str+="<br/>Position : "+px+","+py+" | Distance : "+distance;
	str+="</div>";
	str+="<div style='width:400px;float:left;margin:0px;padding:0px;color:#567;'>";
	str+=text2;
	str+="</div>";
	
	str+="</td></tr></table></td>";
	str+="<td class=detec width='120px' >";
	if(links=="")
		links="&nbsp;";
	str+=links;
	str+="</td></tr>\n";
	return str;
}

function rli(t,cid)
{
	var index=0;
	while(things[t][index]!=null)
	{
		if(things[t][index][0]==cid)
		{
			//alert("found elem "+cid+" tab "+t +" at index "+ index);
            things[t].splice(index,1);
			//things[t][index][14]=0;
			return true;
		}
		index++;
	}
	alert("failed to remove elem "+cid+" tab "+t);
	return false;
}	
function dli(t,index,cid,px,py,distance,img,guildimg,name,name_color,pestatus,status,links,text1,text2,text3,inert,fastmove)
{
	if(fastmove==undefined)fastmove=0;
    if(inert==undefined)inert=0;
    if(text3==undefined)text3="";
	if(text2==undefined)text2="";
	if(text1==undefined)text1="";
	if(links==undefined)links="";
	if(status==undefined)status=-1;
	if(pestatus==undefined)pestatus=-1;
	
	things[t][index]=new Array(cid,px,py,distance,img,guildimg,name,name_color,text1,text2,text3,links,pestatus,status,inert,fastmove);
}
function drawdetec(t)
{
	seltab=t;         
	var index=0;
	var nshow=0;
	var str="<table id='detec_table' border=0 cellpadding=0 cellspacing=0 width=100% class='ytab' >";
	while(things[t][index]!=null)
	{
        //str +=things[t][index][14]+"\n";
		if(things[t][index][14]==1 && disp_inert==0)
		{
            //hide inert
        }
        else
        {
			str+=lidraw(t,	things[t][index][0],things[t][index][1],
							things[t][index][2],things[t][index][3],
							things[t][index][4],things[t][index][5],
							things[t][index][6],things[t][index][7],
							things[t][index][8],things[t][index][9],
							things[t][index][10],things[t][index][11],
                            things[t][index][12],things[t][index][13],
                            things[t][index][14],things[t][index][15]);
			nshow++;
		}
		index++;
	}
	if(nshow==0)
	{
		str+="<tr class=detec><td style='padding:5px' width=350px ><center><i>(Aucun élément)</i></center></td></tr>";
	}
	str+="</table>";   
	document.getElementById("detec_div").innerHTML=ycadre_top("ycadre_center_nopad",false) + str + ycadre_bottom(true);
	                                
	var tab=drawtab(0,t,"Puces")+drawtab_b(1,t,"Batiments")+drawtab(2,t,"Objets")+drawtab(3,t,"Ressources");
	document.getElementById("detectab").innerHTML=tab;
	
}
function drawtab(index,current,name)
{
	var str='<li>';
	str+='<a ';
	if(current==index)
		str+='id="current" ';
	str+='href="javascript:drawdetec('+index+');">'+name+'</a>';
	str+='</li>';
	return str;
}

var disp_inert = 1;
function switchinert(value,index)
{
    disp_inert = value;
    drawdetec(index);
}
function drawtab_b(index,current,name)
{
    var str='<li>';
    str+='<a ';
    if(current==index)
        str+='id="current" ';
    str+='href="javascript:drawdetec('+index+');">'+name+'</a>';
    if(current==index)
    {
        str+='<a href="javascript:switchinert('+(1-disp_inert)+','+index+');">'+(disp_inert?'-':'+')+'</a>';
    }
    str+='</li>';
    return str;
}



function pickObj(cid,oid,pinstall,minstall)
{
	if(id_act!=-1)
	{
		setTimeout("pickObj("+cid+","+oid+","+pinstall+","+minstall+")",200);//delay action
		return;
	}
	id_act = cid;
	
	msg_onsuccess=true;
	del_onsuccess=true;
	act='pick';
	setElementColor(id_act,"#222255");
	
	lajax=ajaxInit();
	if(null==lajax)
		{alert("Impossible d'instancier Ajax.");return false;}
	ajaxRequest(processRequestDetec,lajax,"pick/"+sid+"/"+oid+"/"+pinstall+"/"+minstall);
}
function harvestDeposit(cid,did)
{
	if(id_act!=-1)
	{
		setTimeout("harvestDeposit("+cid+","+did+")",200);//delay action
		return;
	}
	id_act = cid;
	
	msg_onsuccess=true;
	del_onsuccess=false;
	act='harvest';
	setElementColor(id_act,"#222255");
	
	lajax=ajaxInit();
	if(null==lajax)
		{alert("Impossible d'instancier Ajax.");return false;}
	ajaxRequest(processRequestDetec,lajax,"harvest/"+sid+"/"+did);
}
function displayResponse(str)
{
	var eMsg=document.getElementById("emsg");
	if(null==eMsg)
		return;
	var eGroup = document.getElementById("detec_group").parentNode.parentNode.parentNode.parentNode;
	eMsg.style.left=eGroup.offsetLeft + eGroup.offsetWidth/2 - eMsg.offsetWidth/2;
	eMsg.style.top=document.body.scrollTop + 50;
	eMsg.innerHTML=str;
	eMsg.style.visibility='visible';
}
function hideMsg()
{
	var eMsg=document.getElementById("emsg");
	if(null==eMsg)
		return;
	eMsg.style.visibility='hidden';
	eMsg.innerHTML="";
}
function processRequestDetec()
{
	if (lajax.readyState == 4) {
        if (lajax.status == 200) {
          parseResponseDetec();
		  id_act = -1;
        } else {
			alert ( "Serveur hors ligne." );
			document.getElementById('b'+id_act).style.backgroundColor="";
			id_act = -1;
        }
    }
	else
	{
		//displayResponse(lajax.readyState+" ");
	}
}
function removeElement(eid)
{
    //alert("rli("+seltab+","+eid+");");
    rli(seltab,eid);
	drawdetec(seltab);
}
function setElementColor(eid,clr)
{
	var el=document.getElementById('b'+eid);
	if(null==el)
		return false;
	el.style.backgroundColor=clr;
}
function parseResponseDetec() 
{
	response  = lajax.responseText;
	while(response.charCodeAt(0)==13 || response.charCodeAt(0)==10)
	{
		response = response.substr(1);
	}
	var rc=parseInt(response);
	var msg=rc;
	if(msg_onsuccess || rc<0)
	{
		if(rc>0)
		{
			if(act=='harvest')
			{
				msg="<font color=lime>Extraction effectuée avec succès ("+(rc/1000)+" kg|L).</font><br/>";
			}
			else
			{
				msg="<font color=lime>Action effectuée avec succès.</font><br/>";
			}
		}
		else
		{
			switch(rc)
			{
				case -1:
					msg="<font color=red>Erreur interne, veuillez réessayer ultérieurement.</font><br/>";
					break;
				case -2:
					msg="<font color=red>Vous n'avez pas d'emplacement libre pour installer cet objet.</font><br/>";
					break;
				case -3:
					msg="<font color=red>Vous avez atteint la charge maximale de votre moyen de locomotion.</font><br/>";
					break;
				case -4:
					msg="<font color=red>Cet objet n'est plus au sol.</font><br/>";
					break;
				case -22:
					msg="<font color=red>Ce gisement est vide.</font><br/>";
					break;
				case -23:
					msg="<font color=red>Vous n'avez pas de Container/Bombonne pour stocker les ressources.</font><br/>";
					break;
				case -10:
					msg="<font color=red>Vous n'avez pas assez d'energie.</font><br/>";
					break;
				case -6:
					msg="<font color=red>Vous n'avez pas d'emplacement libre pour transporter le metal/gaz.</font><br/>";
					break;
				case -30:
					msg="<font color=red>Vous n'avez pas assez de credits.</font><br/>";
					break;
				case -7:
					msg="<font color=firebrick>Vous ne pouvez pas effectuer cette action car vous êtes inconscient.</font><br/>";
					break;
				case -8:
					msg="<font color=firebrick>Vous ne pouvez pas effectuer cette action car vous êtes paralysé.</font><br/>";
				break;
				default:
				
					break;
			}
		}
		displayResponse(msg);
		if(timer_hidemsg!=-1)
			clearTimeout(timer_hidemsg);
        if(rc>0)
		    timer_hidemsg = setTimeout("hideMsg()",1000);
        else
            timer_hidemsg = setTimeout("hideMsg()",5000);          
	}
	if(rc<=0)
	{
		setElementColor(id_act,"#552222");
		setTimeout("setElementColor("+id_act+",'')",500);
	}
	else
	{
		setElementColor(id_act,"#225522");
		if(del_onsuccess)
		{
           // alert("del_onsuccess");
			setTimeout("removeElement("+id_act+")",100);
		}
		else
		{
			setTimeout("setElementColor("+id_act+",'')",300);
		}
		var obj=window.parent.frames['game_menu_r'];
		//refresh pager
		if(null!=obj && null!=obj.sendRequest)
			obj.sendRequest();
            
	}
}

