function scrollMap(mapId, xDir, yDir) {
	map = document.getElementById(mapId);
	map.scrollLeft += xDir;
	map.scrollTop += yDir;
	//alert("Scroll "+mapId+" ("+xDir+", "+yDir+")");
}

function appendPage(targetUrl, targetId) {
	obj = document.getElementById(targetId);
	// ugly hack to prevent the cache from working on ajax-queries.
	targetUrl += "&time="+Date();
	req = new Request.HTML({
		url : targetUrl,
		method: 'get',
		onSuccess: function(par1, par2, par3) {
			//alert(obj+" - "+par1+" -  "+par2+" - "+par3);
			obj.innerHTML = obj.innerHTML + par3;
		},
		evalScripts: true
	});
	req.send();
}

function prependPage(targetUrl, targetId) {
	obj = document.getElementById(targetId);
	// ugly hack to prevent the cache from working on ajax-queries.
	targetUrl += "&time="+Date();
	//alert(targetUrl);
	req = new Request.HTML({
		url : targetUrl,
		method: 'get',
		onSuccess: function(par1, par2, par3) {
			//alert(obj+" - "+par1+" -  "+par2+" - "+par3);
			obj.innerHTML = par3 + obj.innerHTML;
		},
		evalScripts: true
	});
	req.send();
}

function launchScript(targetUrl, targetId, actionAfter) {
	// ugly hack to prevent the cache from working on ajax-queries.
	targetUrl += "&time="+Date();
	obj = document.getElementById(targetId);
	req = new Request.HTML({
		url: targetUrl,
		method: 'get',
		update: obj,
		evalScripts: true,
		onComplete: actionAfter
	});
	req.send();
}

function replacePage(targetUrl, targetId) {
	obj = document.getElementById(targetId);
	// ugly hack to prevent the cache from working on ajax-queries.
	targetUrl += "&time="+Date();
	req = new Request.HTML({
		url: targetUrl,
		method: 'get',
		update: obj,
		evalScripts: true
	});
	req.send();
}

function showContextMenu(url, event) {
	showWindow(url, "left:"+(event.clientX-5)+"px;top:"+(event.clientY-5)+"px;width:100px;height:150px;position: absolute;");
}

function showPage(url) {
	//showWindow(url, "left:210px;right:210px;top:10px;bottom:10px;");
	showWindow(url, "margin: auto; margin-top: 0px; height: "+ (window.innerHeight - 80) + "px; width: "+ Math.max(700, (Math.min(1000, window.innerWidth - 200 - 200 - 20))) + "px; max-height:" + (window.innerHeight) + "px;");
}

function showWindow(url, style) {
	win = new popWindow(url,style);
	parent = document.getElementById("windowContainer");
	parent.innerHTML = "";
	win.draw(parent);
}

function closeWindow(id) {
	document.getElementById(id).destroy();
	document.getElementById("windowContainer").style.zIndex = "-5";
}

function sendForm(formId, targetId) {
	myForm = new Form.Request(formId, targetId);
	myForm.send();
}

function submitForm(formId,targetId,getString) {
	targetUrl = getString;
	form = document.getElementById(formId);
	for(var i = 0; i < form.elements.length-1; i++) {
		targetUrl += "&"+form.elements[i].id+"="+form.elements[i].value;
	}
	targetUrl += "&time="+Date();
	//alert(url);
	new Request.HTML({
		url: targetUrl,
		method: 'get',
		update: document.getElementById(targetId),
		evalScripts: true
	}).send();
}

//--------------------------- CHAT -------------------------------------

function sendChat(txt) {
	targetUrl = "./ajax/actions.php?action=sendChat&msg="+txt;
	obj = document.getElementById("chatInput");
	req = new Request({
		url: targetUrl,
		method: 'get',
		update: obj
	});
	req.send();
}

function submitChat(event) {
	if(event.keyCode != 13)
		return false;
	obj = document.getElementById("chatInput");
	sendChat(obj.value);
	obj.value = "";
}

function getChat(lastId) {
	now = new Date();
	time = Math.round(now.getTime()/1000);
	obj = document.getElementById("chatView");
	targetUrl = "./ajax/actions.php?action=getChat&time="+time+"&last="+lastId;
	req = new Request({
		url: targetUrl,
		method: 'get',
		onComplete: function() {
			obj = document.getElementById("chatView");
			var xmlobject = (new DOMParser()).parseFromString(this.response.text, "text/xml");
			var msg = xmlobject.getElementsByTagName("message");
				for(var i=0;i<msg.length;i++) {
					lastId = Math.max(msg[i].childNodes[0].firstChild.nodeValue, lastId);
					obj.innerHTML += "<font color='#AA0000'>"+msg[i].childNodes[6].firstChild.nodeValue+"</font>: ";
					obj.innerHTML += msg[i].childNodes[3].firstChild.nodeValue+"<br>";
				}
			obj.scrollTop = obj.scrollHeight;
			window.setTimeout("getChat("+lastId+");",10000);
		}
	});
	req.send();
}

//window.addEvent('load', function() {
//	getChat(0);
//});


