var isdrag=false;
var isretour=false;
var x,y; var id2; // id2 est l'identifiant dans la base de donnÃ©e
var dobj, cible; // cible est l'icône en déplacement

function selectmouse(identifiant,e) {
	id2=identifiant;
	if (isretour) {dobj.style.left=tx; dobj.style.top=ty; isretour=false; } // on remet l'ancien span Ã  la place initiale (s'il est encore en mouvement)	
	var fobj = e.target; cible= fobj.id;
	while (fobj.tagName != "HTML" && fobj.className.substr(0,6)!="dragme") { fobj = fobj.parentNode; } // on remonte les parents jusqu'Ã  rencontrer le span contenant l'Ã©lÃ©ment cliquÃ©
	if (fobj.className.substr(0,6)=="dragme") {
		if (e.altKey) invisibilite(); 
		else if (e.shiftKey){
		isdrag = true; dobj = fobj;
		dobj.style.zIndex = 1;
		dobj.style.cursor='move';
		tx = parseInt(dobj.style.left+0); // sauvegarde emplacement initial
		ty = parseInt(dobj.style.top+0);
		x = e.pageX; y = e.pageY;
		document.onmousemove=movemouse;
		return false; } }
		
}
function movemouse(e) {
	if (isdrag) {
		dobj.style.cursor='move';
		posx = e.pageX; posy = e.pageY;
		dobj.style.left = tx + posx - x;  // positions relatives
		dobj.style.top = ty + posy - y; 
		//document.getElementById("test").innerHTML=( tx + posx - x);
		if (posx<=0 || posy<=0 || posy>=(window.innerHeight+500) || posx>=(window.innerWidth-5)) {exitcadre();return false;} // si dÃ©passement feuille
		return false; }
}
function mouserelache(e) {
		if (isdrag) {
			isdrag=false; dobj.style.cursor='pointer';
			x1=Math.round(parseInt(dobj.style.left+0)/5)*5; dobj.style.left=x1;
			y1=Math.round(parseInt(dobj.style.top+0)/5)*5; dobj.style.top=y1;
			xl1=window.screen.width; yl1=window.screen.height;
			xhr_object.open("GET",racine+"/modifs/ajax_modif_pos.php?x="+x1 +"&y="+y1+"&xl="+xl1+"&yl="+yl1+"&id="+id2, false); // synchrone
			xhr_object.send(null); }
}
function invisibilite() {
		isdrag=false; dobj.style.cursor='pointer'; isretour=false;
		if (confirm("Confirmer le changement de visibilitÃ© du bloc (pour cette ville) ?")) {
				xhr_object.open("GET",racine+"/modifs/ajax_modif_pos.php?invisible&id="+id2, false); // synchrone
				xhr_object.send(null);
				window.document.location.href='';} // rafraichissement de la page
}
function exitcadre() {
		isdrag=false; isretour=true;
		dobj.style.zIndex = 6;
		dobj.style.cursor='pointer';
		retour();
}
function retour() {
	x1 = parseInt(dobj.style.left+0);
	y1 = parseInt(dobj.style.top+0);
	dobj.style.left=x1+(tx-x1)/5;
	dobj.style.top=y1+(ty-y1)/5;
	if ((Math.abs(x1-tx)+Math.abs(y1-ty))<4) {dobj.style.left=tx;dobj.style.top=ty;isretour=false;} // arrêt de la boucle de retour si suffisament prêt
	if (isretour==true) setTimeout("retour()",30);  // vitesse de retour
}

