var IE = document.all?true:false
if (!IE) document.captureEvents(Event.MOUSEMOVE)

var tempX = 0
var tempY = 0
var isactive = 0;
var activediv;
var mouserelx = 0;
var mouserely = 0;



function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}
  document.Show.MouseX.value = tempX
  document.Show.MouseY.value = tempY
  
  if (isactive == 1) {
  	//alert (activdiv.id);
  	activdiv.style.top = tempY-mouserely;
  	activdiv.style.left = tempX-mouserelx;
  	//alert ("22");
  
  }
  
  return true
}

var lastx = 0;
var lasty = 0;


function draginit() {
	// Initialisierung der Überwachung der Events
  	document.onmousemove = getMouseXY;
  	document.onmouseup = dragstop;
}


function moveobj(subobj) {
	//document.title = 'ME ('+subobj.id+', '+playerid+'): ';
	if ((playerid > 0) && (playerid == subobj.id.substr(1,1))) {
		//if (isactive == 0) {
			isactive = 1;
			activdiv = subobj;
			mouserelx = tempX-subobj.style.left.substr(0,subobj.style.left.length-2);
			mouserely = tempY-subobj.style.top.substr(0,subobj.style.top.length-2);
			
			document.getElementById("o"+playerid+"1").style.zIndex  = 100;
			document.getElementById("o"+playerid+"2").style.zIndex  = 100;
			document.getElementById("o"+playerid+"3").style.zIndex  = 100;
			subobj.style.zIndex  = 101;
			//document.title = document.title+'Drag ';
	} else {
		//if ((subobj.id == "toplist") || (subobj.id == "chatwin") || (subobj.id == "start")) {
		//	isactive = 1;
		//	activdiv = subobj;
		//	mouserelx = tempX-subobj.style.left.substr(0,subobj.style.left.length-2);
		//	mouserely = tempY-subobj.style.top.substr(0,subobj.style.top.length-2);
		//}
	}
			
			//alert (mouserely);
		//} else {
			//document.onmouseup = dragstop(subobj);
			//isactive = 0;
			//dropcheck (subobj, tempX-mouserelx+10, tempY-mouserely+10);
			//document.title = document.title+'Drop ';
		//}
	//}	
	if (IE) document.selection.clear();
}

function dragstop() {
	if (isactive == 1) {
		isactive = 0;
		if ((activdiv.id == "toplist") || (activdiv.id == "chatwin") || (activdiv.id == "start")) {
			//alert ('lokal');
		} else dropcheck (activdiv, tempX-mouserelx+10, tempY-mouserely+10);
	}
	//document.title = document.title+'Drop ';
}


function moveobjloc(subobj) {
	if (isactive == 0) {
		if (block) {
			block = 0;
		} else {
			isactive = 1;
			activdiv = subobj;
			mouserelx = tempX-subobj.style.left.substr(0,subobj.style.left.length-2);
			mouserely = tempY-subobj.style.top.substr(0,subobj.style.top.length-2);
		}
	} else {
		isactive = 0;
	}
}

function dropcheck (subobj, posx, posy) {
    var url = "ahah-handler.php?object="+subobj.id+"&posx="+posx+"&posy="+posy;
    //document.getElementById("debug").innerHTML = url;
    ahah(url);
}

function answer (myresponse) {
	//document.getElementById("debug").innerHTML = myresponse;
	var chatandobjects = myresponse.split("###");
	var allobjects = chatandobjects[0].split("#");
	for (i=0;i<allobjects.length;i++) {
		if (allobjects[i].length > 2) {
			var data = allobjects[i].split("|");
			if (data.length > 2) {
				if ((isactive == 0) || ((isactive == 1) && (activdiv.id != data[0]))) {  //Nur aktualisieren, wenn nicht gerade gemoved wird!
					document.getElementById(data[0]).style.left = data[1]-10;
					document.getElementById(data[0]).style.top = data[2]-10;
				}
			}
		}
	}
	
	//alert (chatandobjects[1]);
	document.getElementById("chatentries").innerHTML = "";
	var datachat = chatandobjects[1].split("#");
	for (ichat=0;ichat<datachat.length;ichat++) {
		if (datachat[ichat].length > 2) {
			var cinfo = datachat[ichat].split("|");
			var letzteantwort;
			if (cinfo.length > 2) {
				if (cinfo[1] == "<b>frage24") letzteantwort = cinfo[2];
				else if (cinfo[1] != "<b>frage24") document.getElementById("chatentries").innerHTML = document.getElementById("chatentries").innerHTML + "<li>" + cinfo[1] + ": " + cinfo[2] + "</li>";
			}
		}
	}	
	if (letzteantwort) document.getElementById("chatentries").innerHTML = "<b>frage24: " + letzteantwort + "<hr>" + document.getElementById("chatentries").innerHTML;
	//document.getElementById("debug").innerHTML = myresponse;

}
