// CharDetails.js

var aktiv;

function sendHttpRequest(url,callbackFunc,respXml){
	var xmlobj=null;
	try{
		xmlobj=new XMLHttpRequest();
	}
	catch(e){
		try{
			xmlobj=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e){
			alert('AJAX is not supported by your browser!');
			return false;
		}
	}
	xmlobj.onreadystatechange=function(){
		if(xmlobj.readyState==4){
			if(xmlobj.status==200){
respXml?eval(callbackFunc+'(xmlobj.responseXML)'):eval(callbackFunc+'(xmlobj.responseText)');
			}
		}
	}
	// open socket connection
	xmlobj.open('GET',url,true);
	// send http header
	xmlobj.setRequestHeader('Content-Type','text/html;charset=UTF-8');
	// send http request
	xmlobj.send(null);
}
// display fill pop-up DIVS with server contents
function displayResults(response)
{
	var popupdiv=document.getElementById('popup');
	if(!popupdiv){return};
	var xmlList = response.getElementsByTagName("char");
	// var CharDetails = document.getElementById("char_"+charid);
	var content = '';
	if(xmlList){
		var id;
		var name;
		var race;
		var bildurl;
		var db;
		id=xmlList[0].childNodes[0].firstChild.nodeValue;
		name=xmlList[0].childNodes[1].firstChild.nodeValue;
		race=xmlList[0].childNodes[2].firstChild.nodeValue;
		town=xmlList[0].childNodes[3].firstChild.nodeValue;
		if(town == 'none')
		 town = '';
		db=xmlList[0].childNodes[4].firstChild.nodeValue;
		bildurl=xmlList[0].childNodes[5].firstChild.nodeValue;
		if(bildurl!='none')
			content = '<img src="'+bildurl+'" style="float: right" width="60">';
		content = content + 'Name: <b>'+name+'</b><br />Rasse: '+race+'<br />Bürgerschaft: '+town+'<br />';
		// alert('ID: '+id+'; Char: '+name+'; Race: '+race+';URL: '+bildurl);
	}
	popupdiv.innerHTML='';
	popupdiv.innerHTML=content;
	popupdiv.style.visibility='visible';
}

// display Pop Up div element
function displayPopupDiv(e){
	var posx=0;
	var posy=0;
	if(!e){var e=window.event};
	// determine target DIV
	var targ=e.target?e.target:e.srcElement;
	// calculate mouse coordinates
	if(e.pageX||e.pageY)
	{
		posx=e.pageX;
		posy=e.pageY;
	}
	else if(e.clientX||e.clientY){
		posx=e.clientX;
		posy=e.clientY;
		// check for scroll offsets in IE 6
		if(document.documentElement.scrollLeft || document.documentElement.scrollTop)
		{
			posx+=document.documentElement.scrollLeft;
			posy+=document.documentElement.scrollTop;
		}
	}
	// assign attributes to pop-up DIV element and append
	// it to web document tree
	var div=document.getElementById('popup');
	if(!div){
		var div=document.createElement('div');
		div.setAttribute('id','popup');
		div.className='popupdiv';
		div.style.visibility='hidden';
		document.getElementsByTagName('body')[0].appendChild(div);
		var charid=targ.id.substring(5);
		if(charid != '')
			if(aktiv==false)
				aktiv = window.setTimeout("sendHttpRequest('char_details.php?id="+charid+"','displayResults',true)",1000);
			// sendHttpRequest('char_details.php?id='+charid,'displayResults',true);
		else
			hidePopupDiv();
	}
	// move pop-up DIV element
	div.style.top=posy+5+'px';
	div.style.left=posx+5+'px';
}

// remove pop-up DIV element
function hidePopupDiv(){
	window.clearTimeout(aktiv);
	aktiv = false;
	var div=document.getElementById('popup');
	if(!div){return};
	div.parentNode.removeChild(div);
}

// activate pop-up DIV elements
function activatePopupDivs(){
	var divs=document.getElementsByTagName('div');
	if(!divs){return};
	for(var i=0;i<divs.length;i++){
		if(divs[i].className=='popupcontainer')
		{
			// display pop-up DIV element
			divs[i].onmousemove=displayPopupDiv;
			// hide pop-up DIV element
			divs[i].onmouseout=hidePopupDiv;
		}
	}
}

// activate pop-up DIV elements when web page has been
// loaded
window.onload=function(){
	if(document.getElementById && document.createElement && document.createTextNode)
	{
		activatePopupDivs();
	}
}

