/** overlay info -- show the information for  toolbuttons
*  Creates a http request object for ajax interface to defininition of information
*  from a php file. Makes a div to information show
*  insert in index (important! after <body>-tag for Safari-conpatibility):
*    var akaInfo = new aka_overlay(PHP file get Ajax variables);
*  to use: 			akaInfo.show(key); akaInfo.hide();
*  
*  @author Andrey Karachev <a.karachev@baum-systems.de>
*
*/
aka_x = 0;
aka_y = 0;

aka_all = new Object();
http_overlay = createRequestObject();

function aka_overlay(file) {
	this.show = aka_show;
	this.hide = aka_hide;
	this.tshow = aka_show_text;
	this.space_x = 10;
	this.space_y = 10;
	this.show_time = 2000;
	document.onmousemove = aka_position;
	
	this.error_text = 'The information for "%key%" doesn\'t exist!';
	aka_all['test'] = 'It is a test for the overlay';
	aka_all['aka_error'] = this.error_text;
	
	http_overlay.open('get', file);
	http_overlay.onreadystatechange = handleOverlay;
	http_overlay.send(null);
	
	aka_area =	'<div id="aka_info" class="aka_overlay"></div>';
	
	document.write(aka_area);
}
function createRequestObject() {
	var ro;   
	if(navigator.appName.search("Microsoft") > -1) {
		if(ro = new ActiveXObject("Microsoft.XMLHTTP"));
		else if (ro = new ActiveXObject("MSXML2.XMLHTTP"));
	} else {
		ro = new XMLHttpRequest();
	}
	return ro;
}
function aka_position(e) {
	if(!e) e = window.event || arguments.callee.caller.arguments[0];
	if(document.all) {
		aka_x = e.clientX;
		aka_y = e.clientY;
	} else if(document.getElementById) {
		aka_x = e.pageX;
		aka_y = e.pageY;
	}
}
function aka_win_width () {
	if (window.innerWidth) {
   		return window.innerWidth;
	} else if(document.documentElement.offsetWidth) {
		return document.documentElement.offsetWidth;
	} else if (document.body && document.body.offsetWidth) {
   		return document.body.offsetWidth;
	} else {
   		return 0;
	}
}
function aka_win_height () {
	if (window.innerHeight) {
  		return window.innerHeight;
	} else if(document.documentElement.offsetHeight) {
		return document.documentElement.offsetHeight;
	} else if (document.body && document.body.offsetHeight) {
		return document.body.offsetHeight;
	} else {
  		return 0;
	}
}
function aka_show(id) {
	document.onmousemove = aka_position;
	document.onmouseover = aka_position;
	/*
	e = window.event || arguments.callee.caller.arguments[0];
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
	alert(targ.id);
	*/
	info = "";
	if(aka_all[id])
		info = aka_all[id];
	else
		info = this.error_text.replace(/%key%/, id);
		
	document.getElementById('aka_info').style.left = aka_x + "px";
	document.getElementById('aka_info').style.top = aka_y + "px";	
	document.getElementById('aka_info').innerHTML = info;
	document.getElementById('aka_info').style.display = "block";
	
	l = document.getElementById('aka_info').offsetWidth;
	h = document.getElementById('aka_info').offsetHeight;
	
	size = get_window_size();
	x = aka_x + this.space_x;
	y = aka_y + this.space_y;
	
	if(x > (size[0] - l))
		x -= l;
	if(y > (size[1] - h))
		y -= h;
		
	if(aka_y < (y + h) && aka_y > y) y = aka_y - h - 3;
	
	document.getElementById('aka_info').style.left = x + "px";
	document.getElementById('aka_info').style.top = y + "px";
	
	aktiv = window.setTimeout("aka_hide()", this.show_time);
}
function aka_show_text(info) {
	document.onmousemove = aka_position;
	document.onmouseover = aka_position;
	
	document.getElementById('aka_info').style.left = aka_x + "px";
	document.getElementById('aka_info').style.top = aka_y + "px";
	document.getElementById('aka_info').innerHTML = info;
	document.getElementById('aka_info').style.display = "block";
	
	l = document.getElementById('aka_info').offsetWidth;
	h = document.getElementById('aka_info').offsetHeight;
	
	size = get_window_size();
	x = aka_x + this.space_x;
	y = aka_y + this.space_y;
	
	if(x > (size[0] - l))
		x -= l;
	if(y > (size[1] - h))
		y -= h;
	
	if(aka_y < (y + h) && aka_y > y) y = aka_y - h - 3;
	
	document.getElementById('aka_info').style.left = x + "px";
	document.getElementById('aka_info').style.top = y + "px";
	
	aktiv = window.setTimeout("aka_hide()", this.show_time);
}
function aka_hide() {
	document.onmousemove = aka_position;
	window.clearTimeout(aktiv);
	document.getElementById('aka_info').style.display = "none";
	document.getElementById('aka_info').innerHTML = "";
}
function handleOverlay() {
	if((http_overlay.readyState == 4) && (http_overlay.status == 200)) {
		var response = http_overlay.responseText;
		if(response.indexOf('|') != -1) {
			var update = new Array();
			update = response.split('|');
			for(i = 0; i < update.length; i+=2) {
				if(update[i + 1] || update[i + 1] == "") {
					aka_all[update[i]] = update[i + 1];
				}
			}
		}
	}
}
function get_window_size() {
	res = new Array(0,0);
	if(navigator.userAgent.search(/MSIE 6/) != -1) {
		res[0] = document.documentElement.scrollLeft + document.documentElement.clientWidth;
		res[1] = document.documentElement.scrollTop + document.documentElement.clientHeight;
	} else {
		res[0] = window.pageXOffset + window.innerWidth;
		res[1] = window.pageYOffset + window.innerHeight;
	}
	return res;
}
